Welcome Guest, Not a member yet? Register   Sign In
form validation callback
#1

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

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

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

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




Theme © iAndrew 2016 - Forum software by © MyBB