CodeIgniter Forums
Form Validation Error - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Form Validation Error (/thread-53612.html)

Pages: 1 2


Form Validation Error - El Forum - 07-31-2012

[eluser]xtremer360[/eluser]
I'm working on my register form and I have it submitting via jQuery with having some validation checks client side and then of course on the server side but I get an error when I hit submit.

When I submit the form I get this message:

<b>Fatal error</b>: Call to private method Register::username_exists() from context 'CI_Form_validation' in <b>/home/xtremer/public_html/system/libraries/Form_validation.php</b> on line <b>593</b><br />

Code:
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length[6]|max_length[12]|alpha_numeric|strtolower|callback_username_exists');

Code:
private function username_exists($str)
{
    if ($this->users_model->is_username_available($this->input->post('username')))
    {
$this->form_validation->set_message('username', 'Testing');
return FALSE;
    }
    else
    {
        return TRUE;
    }
}



Form Validation Error - El Forum - 07-31-2012

[eluser]CroNiX[/eluser]
callbacks need to be public or the validation class can't access them.

If you want to make it "private" so it's not accessible via the URL, CI lets you prefix the method name with an underscore, and do the same when you are setting the rules.

Code:
public function _username_exists($str)
...

Code:
//2 underscores
callback__username_exists



Form Validation Error - El Forum - 07-31-2012

[eluser]CroNiX[/eluser]
As a side note, you should put any validation rules that manipulate data (instead of "testing" it), such as strtolower, AFTER any other rules that return a TRUE/FALSE.


Form Validation Error - El Forum - 07-31-2012

[eluser]xtremer360[/eluser]
Well I do already have validation rules set up. Does anything I'm doing in this controller look off?

http://pastebin.com/wRKBRqdf


Form Validation Error - El Forum - 07-31-2012

[eluser]CroNiX[/eluser]
Please read what I actually wrote.


Form Validation Error - El Forum - 07-31-2012

[eluser]xtremer360[/eluser]
Well that's just it I'm not quite understanding what your saying. Could you possibly rephrase it for me so I could understand. Thank you.


Form Validation Error - El Forum - 07-31-2012

[eluser]CroNiX[/eluser]
You can't have a private function as a validation callback. The validation class can't access the private function defined as private in the controller, that's just php.

You need to make it "private" with an underscore like the user guide states here:
http://ellislab.com/codeigniter/user-guide/general/controllers.html#private

After doing that, you will have an extra underscore in the name of your callback function, which you then need to add to where you are setting the rules.

I don't know how to make it much more clear. I think the first post was more clear. Sorry.




Form Validation Error - El Forum - 07-31-2012

[eluser]xtremer360[/eluser]
If you look at the pastebin I had make them private.


Form Validation Error - El Forum - 07-31-2012

[eluser]CroNiX[/eluser]
I did, and no, you didn't. I don't see any of your callback functions prefixed with underscores to make them private like I stated and the userguide I linked to explains.
Code:
public function is_username_available()
should be
Code:
public function _is_username_available()// prefixed with "_"
which would then also change
Code:
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length[6]|max_length[12]|alpha_numeric|strtolower|callback_username_exists');
to
Code:
//extra underscore in 'callback__username'
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length[6]|max_length[12]|alpha_numeric|strtolower|callback__username_exists');


The same needs to be done for this function and also its rules
Code:
public function is_email_available()




Form Validation Error - El Forum - 07-31-2012

[eluser]xtremer360[/eluser]
My bad. Thank you.