Can anyone see what I'm missing?
I'm using Codeigniter v1.72.
In the doc:
http://codeigniter.com/user_guide/libraries/form_validation.html
It states:
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
function username_check($str)
{
if ($str == 'test')
{
$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
return FALSE;
}
else
{
return TRUE;
}
}
In my class User extends Controller
I have in function register:
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean', 'callback_username_check('.$username.')');
I have also tried
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean', 'callback_username_check');
And
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean', 'callback_username_check['.$username.']');
function username_check($str)
{
$this->load->model('Use开发者_Python百科r_model', '', TRUE);
$taken = $this->User_model->countUsername($str);
if($taken)
{
$this->form_validation->set_message('username_check', 'That username is already taken');
return FALSE;
}
else
return TRUE;
}
There are no errors at all, none of my approaches work, the code behaves like it's not there.
First of all, I'm assuming the rest of your code is correct. It might help to show the whole User
class.
You might want to check if CodeIgniter lets you invoke callback functions AND prepping/validator functions in the same rule. If it doesn't allow that, you could call trim
, require
, and xss_clean
in your callback function.
I will say, though, that if it is allowed, then this is definitely the RIGHT form:
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean', 'callback_username_check');
This is wrong:
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean', 'callback_username_check['.$username.']');
And this is wrong too:
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean', 'callback_username_check('.$username.')');
See, in that line, you shouldn't actually be CALLING the function. Rather, you're passing a string to the set_rules()
function that it will parse and figure out what function you want to use as a callback.
As the documentation states, whatever the value of username
is, will be passed as the argument to your callback function.
EDIT:
I was going to say try this:
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|callback_username_check');
But I don't think it will work. Instead, this should work:
function username_check($str)
{
$this->load->model('User_model', '', TRUE);
$taken = $this->User_model->countUsername($str);
if($taken)
{
$this->form_validation->set_message('username_check', 'That username is already taken');
return FALSE;
}
else if(!$str) {
// This is functioning as the required rule
return FALSE;
}
else {
$str = trim($str);
$str = $this->input->xss_clean($str);
return $str;
}
}
精彩评论