Welcome Guest, Not a member yet? Register   Sign In
Validation function in helper
#1

[eluser]Unknown[/eluser]
Hi there,

I'm actually doing a form library so i can easily build some forms in CI. I'm having different type of fields (ie: date, select, checkbox etc.) and i want to automatically do some validation using the CI library on these types of field. For example if i have a date field i want to automatically validate the field to check if it's a true date.

As i'm doing that in a library i thought it would be nice to have these validation functions (is_true_date($date) or whatever) in a validation helper. What do you think of that idea ?

Anyway i was trying to do that but couldn't find a way to make it work. I've been using the validation form from the documentation and just moved the username_check($str) function in a helper. I also removed the "callback_" part in the validation string as it's gonna be used as a standard function (such as is_numeric).

I had to change a little bit the username_check($str) function in the helper (to get the instance into the function). Here is how it looks like now :
Code:
function username_check($str)
{
  if ($str == 'test')
  {
    if (!isset($this->CI))
      $this->CI =& get_instance();
    $this->CI->validation->set_message('username_check', 'The %s field can not be the word "test"');
    return FALSE;
  }
  else
  {
    return TRUE;
  }
}

The problem i'm having now is that if it try to validate the form and an error occurs the username field is filled up with what's returned by username_check($str) (1 or 0) instead of its original value. Do you have any idea why ? Shouldn't it work as if i was using is_numeric or any other function ?

Thanks for your time.




Theme © iAndrew 2016 - Forum software by © MyBB