CodeIgniter Form Validation - callback not bound to a field
Is there a way to set a validation callback that is not b开发者_JAVA技巧ound to a particular field, but is necessary to pass validation?
Yes you can and here is a cruddy example:
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'lang:lang_username', 'required|callback_check_login');
Then for the callback:
public function check_login($username)
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$remember = $this->input->post('remember');
$login = $this->users_auth->login($username, $password, $remember);
if ($login !== TRUE)
{
$this->form_validation->set_message('check_login', $login);
return FALSE;
}
else
{
return TRUE;
}
}
Add a hidden field to your form and assign the callback to that a field. The callback doesn't have to relate to the contents of the hidden field.
Edit: Or, for that matter, you could assign the callback to any of your fields along other validation rules. No need for a hidden field.
精彩评论