CodeIgniter Forums
form validation callback - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: form validation callback (/showthread.php?tid=20550)



form validation callback - El Forum - 07-14-2009

[eluser]timaksu[/eluser]
so we can use custom functions as part of the validation of a form. awesome. however, i have one issue with it. what if my validation function is, say, in a model that i've loaded. (say i've loaded the "user_model" model into my current controller. the user model has all the functions necessary to add/load/edit/etc website users. one of its features are to check if a member exists. i can call this normally by doing:

Code:
$this->user_model->exists($str);

but how would i add that function ("exists") as a validation rule? Smile


form validation callback - El Forum - 07-14-2009

[eluser]Cro_Crx[/eluser]
Hey timaksu

You can write a really short callback function to do that

In your validation rules you would have something like this

Code:
$this->form_validation->set_rules('field', 'Field Name', 'callback__check_if_exists');

Then just write a callback function. Notice how i've put another underscore, this keeps the function private so it can't be accessed externally by a user directly.

Code:
function _check_if_exists()
{
    $this->form_validation->set_rules('_check_if_exists', 'Username already exists');
    return (!$this->user_model->exists($str));
    // we return the opposite, so if the user exists we return false and true if they don't exist.
  
}



form validation callback - El Forum - 07-14-2009

[eluser]timaksu[/eluser]
yeah i was thinking something similar but thought their may have been a way of doing it without the extra functions. ah well, still works i guess. cheers cro_crx Smile


form validation callback - El Forum - 07-14-2009

[eluser]Cro_Crx[/eluser]
np. I just updated it as i spotted a problem, so yeah just use the revised version.