Codeigniter radio buttons callback validation error? - codeigniter

I want to display error message if no gender is selected using Codeigniter callback validation error...
My HTML Page
<label class="control-label labeller" for="gender">Gender</label>
<input type="radio" id="gender" name="gender" value="Male">Male
<input type="radio" id="gender" name="gender" value="Female">Female
My Controller
$this->form_validation->set_rules('gender', 'Gender', 'callback_gender');
public function gender($str)
{
if ($str == '')
{
$this->form_validation->set_message( 'gender', 'Please Choose gender' );
return FALSE;
}
else
{
return TRUE;
}
}
I tried a lot of times... Please help....

Try using $this->input->post('gender') instead of $str
<?php
class Welcome extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
}
public function index() {
$this->form_validation->set_rules('gender', 'Gender', 'callback_gender');
if ($this->form_validation->run() == FALSE) {
// Load view
} else {
// Success Stuff
}
}
public function gender() {
if ($this->input->post('gender')) {
return TRUE;
} else {
$this->form_validation->set_message( 'gender', 'Please Choose gender' );
return FALSE;
}
}
}
Or on callback $this->form_validation->set_rules('gender', 'Gender',
'callback_gender[gender]');
<?php
class Welcome extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
}
public function index() {
$this->form_validation->set_rules('gender', 'Gender', 'callback_gender[gender]');
if ($this->form_validation->run() == FALSE) {
// Load view
} else {
// Success Stuff
}
}
public function gender($str) {
if ($str == '') {
$this->form_validation->set_message( 'gender', 'Please Choose gender' );
return FALSE;
} else {
return TRUE;
}
}
}

Related

How to differentiate the multiple panels with login and session?

It create the session but does not go to index2 and index3 always redirect with else and go to index method but i want to go index2 and index3 to handle other panels also.
Session is created successfully for all just comming else condition all the time.
My form data and array is also showing when i using the print_r for my code to view if the data is comming or not.
Problem is it is showing no any error just redirect with file of index method.
My Controller
class Main extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Main_Model');
$this->load->helper('url');
$this->load->library('session');
$method = $this->router->fetch_method();
$methods = array('index','index2','index3');
if(in_array($method,$methods))
{
if(!$this->session->has_userdata('signup_email'))
{
redirect(base_url('Main/login'));
}
}
}
public function index()
{
if($this->session->has_userdata('signup_email'))
{
$this->load->view('BKO/index');
}
}
public function index2()
{
if($this->session->has_userdata('signup_email'))
{
$this->load->view('Admin/index');
}
}
public function index3()
{
if($this->session->has_userdata('signup_email'))
{
$this->load->view('Owner/index');
}
}
public function login()
{
//$data['select'] = $this->Main_Model->get_select();
$this->load->view('login');
}
public function login_process()
{
//$roll = $this->input->post('select');
echo $email = $this->input->post('email');
echo $pass = $this->input->post('upass');
$query = $this->Main_Model->login_process($email,$pass);
if($query == TRUE)
{
$this->session->set_userdata('signup_email');
$session = array(
'signup_email' => $email
);
$this->session->set_userdata($session);
redirect(base_url('Main/check_login'));
}
else
{
$this->session->set_flashdata('error','Invalid Email or Password');
redirect(base_url('Main/login'));
}
}
public function check_login()
{
if($this->session->userdata() == 'admin#gmail.com')
{
echo "Welcome - <h2>".$this->session->userdata('username')."</h2>";
redirect(base_url('Main/index2'));
}
elseif($this->session->userdata() == 'owner#gmail.com')
{
echo "Welcome - <h2>".$this->session->userdata('username')."</h2>";
redirect(base_url('Main/index3'));
}
else
{
echo "Welcome - <h2>".$this->session->userdata('username')."</h2>";
redirect(base_url('Main/index'));
}
}
public function logout()
{
$this->session->sess_destroy();
redirect(base_url());
}
My Model
public function login_process($email,$pass)
{
//$this->db->select('*');
//$this->db->where('roll_id',$roll);
$this->db->where('signup_email',$email);
$this->db->where('signup_password',$pass);
$query = $this->db->get('signup');
if($query->num_rows() > 0)
{
$this->session->set_flashdata('signup_email');
return true;
}
else
{
return false;
}
}
You missed the parameter here
if($this->session->userdata() == 'admin#gmail.com')
instead it should be
if($this->session->userdata('signup_email') == 'admin#gmail.com')

validate combo box in CodeIgniter

I am using CodeIgniter 3 where i am trying to validate my two combo boxes.
My code is like this:
view:
<tr>
<td >
<?php $extra=' class="selectCombo"';
$busVal=array();
$busVal[]="Select";
foreach($values as $val){
$busVal[$val->bus_id]=$val->bus_no;
}
echo form_dropdown('bus_id', $busVal,set_value('bus_id'),$extra);
?>
</td>
<td><?php echo form_error('bus_id');?></td>
</tr>
<tr>
<td >
<?php
$extra=' class="selectCombo"';
$route=array();
$route[]="Select";
foreach($values as $val){
$route[$val->route_id]=$val->route_name;
}
echo form_dropdown('route_id',$route,set_value('route_id'),$extra);
?>
</td>
<td><?php echo form_error('route_id');?></td>
</tr>
My controller code is like this:
public function add()
{
$this->form_validation->set_rules('bus_id', 'Bus', 'trim|required|callback_check_Bus');
$this->form_validation->set_rules('route_id', 'Route Name', 'trim|required|callback_validate_route');
if ($this->form_validation->run() == FALSE) {
echo 'Error';
}else{
}
}
public function check_Bus($val){
if($val ==0)
{
$this->form_validation->set_message('select_validate', 'Please Select Bus.');
return false;
}
else
{
return true;
}
}
public function validate_route($val){
if($val ==0)
{
$this->form_validation->set_message('select_validate', 'Please Select Route.');
return false;
}
else
{
return true;
}
}
I get following message for both fields:
Unable to access an error message corresponding to your field name Bus.(check_Bus)
Unable to access an error message corresponding to your field name
Route Name.(validate_route)
What should i do?
Use this code working fine.
callback validation rule
callback_fieldname_check
//function name callback_fieldname_check (){
}
// Set message - fieldname_check
see code
public function add()
{
$this->form_validation->set_rules('bus_id', 'Bus', 'trim|required|callback_bus_id_check');
$this->form_validation->set_rules('route_id', 'Route Name', 'trim|required|callback_route_id_check');
if ($this->form_validation->run() == FALSE) {
$this->load->view('load_your_view');
}else{
echo "Added";
}
}
public function bus_id_check($val){
if($val ==0)
{
$this->form_validation->set_message('bus_id_check', 'Please Select Bus.');
return false;
}
else
{
return true;
}
}
public function route_id_check($val){
if($val ==0)
{
$this->form_validation->set_message('route_id_check', 'Please Select Route.');
return false;
}
else
{
return true;
}
}

Check if has beign redirected from another controller

On my login controller or view I would like to be able to see if it is possible to check if page has redirected back to login and then display bootstrap error message. I also use a MY_controller function in codeigniter
I do not want to use codeigniter session flash data message. I have my own error message as showing in code.
Is it possible to check if controller has been redirect from another controller?
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends MY_Controller {
private $error = array();
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
}
public function index() {
$data['title'] = 'Administration';
$user_id = $this->session->userdata('user_id');
if (isset($user_id)) {
$this->error['warning'] = "Working";
} else {
$this->error['warning'] = "";
}
if (isset($this->error['warning'])) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$this->form_validation->set_rules('username', 'Username', 'required|callback_validate');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run($this) == FALSE) {
$this->load->view('template/common/login.tpl', $data);
} else {
redirect('admin/dashboard');
}
}
public function validate() {
$this->load->library('user');
if ($this->user->login() == FALSE) {
$this->form_validation->set_message('validate', 'Does not match any of our database records');
return false;
} else {
return true;
}
}
}
MY Controller
<?php
class MY_Controller extends MX_Controller {
public function __construct() {
parent::__construct();
Modules::run('admin/error/permission/check');
}
}
Update I have tried This still displays message even though have not been redirect from another page.
Thanks to #AdrienXL for some great advice. $this->load->library('user_agent'); was the best method.
if ($this->agent->referrer()) {
$this->error['warning'] = "Working";
} else {
$this->error['warning'] = "";
}
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends MY_Controller {
private $error = array();
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
}
public function index() {
$data['title'] = 'Administration';
$this->load->library('user_agent');
if ($this->agent->referrer()) {
$this->error['warning'] = "Working";
} else {
$this->error['warning'] = "";
}
if (isset($this->error['warning'])) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$this->form_validation->set_rules('username', 'Username', 'required|callback_validate');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run($this) == FALSE) {
$this->load->view('template/common/login.tpl', $data);
} else {
redirect('admin/dashboard');
}
}
public function validate() {
$this->load->library('user');
if ($this->user->login() == FALSE) {
$this->form_validation->set_message('validate', 'Does not match any of our database records');
return false;
} else {
return true;
}
}
}

User edit page not working

When i click on my user edit button <?php echo anchor('users/edit/'. $user->user_id, '<div class="btn btn-primary"><i class="fa fa-edit"></i> Edit</div>');?>
It sends me to http://localhost/codeigniter/codeigniter-blog/admin/users/edit/1 but shows error 404 Page Not Found. '1' example is the user_id
But edit function exists. How can I make my edit function on my users controller work with the user id. So if i click on a certain user it will only update that id row information.
Model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_user extends CI_Model {
protected $id;
function getAll() {
$query = $this->db->get('user');
if ($query->num_rows() > 0) {
return $query->result();
return $query->row('user_id');
return true;
} else {
return false;
}
}
public function editUser() {
}
public function getID($user_id) {
$user_query = $this->db->get('user');
if ($user_query->num_rows() == 1) {
return $user_query->row('user_id', $user_id);
return true;
} else {
return false;
}
}
}
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('user');
if ($this->session->userdata('isLogged') == TRUE) {
return true;
} else {
redirect('/');
}
}
public function index() {
$this->getList();
}
public function edit() {
$this->load->library('form_validation');
$this->load->model('users/model_user');
$this->form_validation->set_rules('name', 'Name');
$this->form_validation->set_rules('username', 'Username');
if ($this->form_validation->run() == TRUE) {
redirect('users');
} else {
$this->getForm();
}
}
function getForm() {
$data['title'] = "Users";
$data['base'] = config_item('HTTP_SERVER');
$data['isLogged'] = $this->user->isLogged();
$this->load->model('users/model_user');
$data['users'] = $this->model_user->getAll();
$data['header'] = $this->load->view('template/common/header', $data, TRUE);
$data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);
return $this->load->view('template/users/users_form', $data);
}
function getList() {
$data['title'] = "Users";
$data['base'] = config_item('HTTP_SERVER');
$data['isLogged'] = $this->user->isLogged();
$this->load->model('users/model_user');
$data['text_enabled'] = "Enabled";
$data['text_disabled'] = "Disabled";
$data['users'] = $this->model_user->getAll();
$data['header'] = $this->load->view('template/common/header', $data, TRUE);
$data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);
return $this->load->view('template/users/users_list', $data);
}
}
May be you miss to use index.php in url
(OR)
Use below code base_url('');
<?php
$url=base_url('users/edit/'. $user->user_id);
echo anchor($url, '<div class="btn btn-primary"><i class="fa fa-edit"></i> Edit</div>');?>
I can view page now to do with routes $route['users/edit/(:num)'] = "users/users/edit/$1";

Fatal error: Call to a member function in the login validation

I have the controller file:- login.php
class Login extends CI_Controller {
function __construct() {
parent::__construct();
}
function success() {
$username = $this->input->post('username');
$password = $this->input->post('password');
$errorMsg ="";
$queryResult = $this->logins->validate($username,$password);
if($queryResult == TRUE) {
redirect ('home');
}
else {
$errorMsg ="Invalid Username or Password";
$this->load->view('login',$errorMsg);
}
}
}
view:- login.php
<script type="text/javascript">
function validatelogin(){
var x=document.forms["login"]["username"].value;
var y=document.forms["login"]["passwrd"].value;
if (x==null || x=="")
{
alert("First name must be filled out");
return false;
}
if (y==null || y=="")
{
alert("Password field must be filled out");
return false;
}
/*if(x!="monisha" && y!="monisha"){
alert("Username and Password incorrect");
return false;
}*/
return true;
}
</script>
the HTML form have:-
<form name="login" id="login" action="<?php echo base_url() ?>login/success" onsubmit="return validatelogin()" method="post">
Model file logins.php is having the function, which describes function validate
class Logins extends CI_Model {
function __construct()
{
parent::__construct();
}
function validate($username,$password){
$this->db->select('username','password');
$this->db->from('logins');
$this->db->where('username', $username);
$query = $this->db->get('logins');
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$passwrd = $row->password;
if($passwrd == $password) {
return TRUE;
}
}
} else {
return FALSE;
}
}
}
but showing the error:-
Fatal error: Call to a member function validate() on a non-object in this line:-
$queryResult = $this->logins->validate($username,$password);
You aren't loading class "logins"
$this->load->library('Logins');
Try like this in your model
public function __construct() {
// Connecting Database
parent::__construct();
$this->load->database();
}
Please Add your model Class name into config\autoload.php file
$autoload['model'] = array('logins');
Its working fine for me,Please try it.
Load the model named Logins before you use it
$this->load->model('logins', '', TRUE);
or in autoload.php

Resources