codeigniter form validation callback, when validating forms i need to check something other than just required, or a max length, or any of the default (and very good) built-in filters. i need to check a user login, so need to: define some validation rules (config below), create a callback function, and set a validation message.
First off, create a file in application/config/ called form_validation.php
, and configure some validation rules, these are mine:
$config = array(
‘user/login’ => array(
array(
‘field’ => ’email’,
‘label’ => ‘Email Address’,
‘rules’ => ‘trim|required|xss_clean|valid_email’
),
array(
‘field’ => ‘password’,
‘label’ => ‘Password’,
‘rules’ => ‘trim|required|xss_clean|callback__checkEmailPassword’
),
),
‘user/register’ => array(
array(
‘field’ => ‘name’,
‘label’ => ‘Name’,
‘rules’ => ‘trim|required|xss_clean’
),
array(
‘field’ => ’email’,
‘label’ => ‘Email Address’,
‘rules’ => ‘trim|required|xss_clean|valid_email|is_unique[user.email]’
),
array(
‘field’ => ‘password’,
‘label’ => ‘Password’,
‘rules’ => ‘trim|required|xss_clean’
),
),
‘user/forgot’ => array(
array(
‘field’ => ’email’,
‘label’ => ‘Email Address’,
‘rules’ => ‘trim|required|xss_clean|valid_email|callback__checkEmail’
),
),
‘user/recover’ => array(
array(
‘field’ => ‘password’,
‘label’ => ‘New Password’,
‘rules’ => ‘trim|required|xss_clean|’
),
),
‘user/edit’ => array(
array(
‘field’ => ’email’,
‘label’ => ‘Email Address’,
‘rules’ => ‘trim|required|xss_clean|valid_email’
),
),
);
a callback function
In the rules above, every callback function MUST begin with callback_
, so mine is called callback__checkEmailPassword
, the actual function does not have to begin with callback_, although it could, but that would be very confusing. my function is defined like so (i’m using redbean), (also, it begins with an underscore because that’s how i identify my own callback functions):
public function _checkEmailPassword()
{
$email = $this->input->post('email');
$password = $this->input->post('password');
$u = R::findOne('user',' email = :email AND password = :password', array(':email'=>$email, ':password'=>md5($password .SALT)));
if ( !$u )
{
return false;
}
return true;
}
and a validation rule would look like so:
$this->form_validation->set_message('_checkEmailPassword', 'Email Address / Password are incorrect.');
that’s all that’s needed to achieve this simple addition of your own rules.
here’s my login function (part of the user controller):
public function login()
{
// already logged in...
if ( logged_in() )
{
redirect('user');
}
$data[‘title’] = ‘login’;
if ( $_POST )
{
$this->form_validation->set_message(‘required’, ‘%s is required.’);
$this->form_validation->set_message(‘valid_email’, ‘%s must be valid.’);
$this->form_validation->set_message(‘_checkEmailPassword’, ‘Email Address / Password are incorrect.’);
if ($this->form_validation->run(‘user/login’) == FALSE)
{
$data[‘view’] = ‘user/login’;
}
else
{
$email = $this->input->post(’email’);
$password = $this->input->post(‘password’);
$u = R::findOne(‘user’,’ email = :email AND password = :password’, array(‘:email’=>$email, ‘:password’=>md5($password .SALT)));
$this->session->set_userdata(‘logged_in’, true);
$this->session->set_userdata(‘user_id’, $u->id);
redirect(‘user’);
}
}
else // login form…
{
$data[‘view’] = ‘user/login’;
}
$this->load->view(‘layout’, $data);
}