Welcome Guest, Not a member yet? Register   Sign In
Form Validation Error
#1

[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;
    }
}
#2

[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
#3

[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.
#4

[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
#5

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

[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.
#7

[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-gui...ml#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.

#8

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

[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()

#10

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




Theme © iAndrew 2016 - Forum software by © MyBB