• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
form validation callback

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:


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

Hey timaksu

You can write a really short callback function to do that

In your validation rules you would have something like this

$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.

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.

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

np. I just updated it as i spotted a problem, so yeah just use the revised version.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.