Welcome Guest, Not a member yet? Register   Sign In
Validation callback: possible to use functions in helpers?
#1

[eluser]CodeIgniterNewbie[/eluser]
I've created application/helpers/form_validation_helper.php and have autoloaded form_validation in the helpers section application/config/autoload.php. Say I have a function in form_validation_helper.php called foo.

In one of my controllers, I am trying to do something like this:

Code:
$this->form_validation->set_rules('username', 'User Name', 'required|callback_foo');

When I run the validation, CI seems to be unaware of foo. It'll handle native CI validation rules (such as required), but ignore my callbacks. If I copy foo and put it in the same controller as what's calling foo, everything works well.

Any idea what I am doing wrong?
#2

[eluser]WanWizard[/eluser]
callbacks are methods in the controller that runs the validation, in this case a method called foo() is expected. If it is a normal function, just use
Code:
$this->form_validation->set_rules('username', 'User Name', 'required|foo');
#3

[eluser]CodeIgniterNewbie[/eluser]
Thanks. That solved my problem. Now, on to the new problem: my function is returning a FALSE and in it I try to set error message this way:

function foo($value)
{
// determine validation failed, then do this:
$codeigniter_instance =& get_instance();
$codeigniter_instance->form_validation->set_message('foo', 'The %s field must be foo');
return FALSE;

// validation passed
return TRUE;
}

$value contains the user input. The result of $this->form_validation->run() is TRUE even when foo($value) is returning a FALSE.

Any ideas why?
#4

[eluser]WanWizard[/eluser]
Yes.

The code in the Form Validation library assumes that if it's not a callback, and it's not a method in the library itself, it must be a native PHP function. And since they can't set custom error messages, the code loops to the next rule without setting the error message.

There are a few options:
- do not use a helper, but extend the Form validation library, and add your function to your MY_Form_validation library
- extend the form validation library, copy the _execute() method over, and fix the problem.
#5

[eluser]CodeIgniterNewbie[/eluser]
I tried extending the form validation library. Still ran into the problem with trying to set custom messages. How do I do that?

class MY_Form_validation extends CI_Form_validation
{
public function foo($value)
{
// validation failed
// how do I set custom message?
// return FALSE;

// validation passed
return TRUE;
}
}
#6

[eluser]WanWizard[/eluser]
Same way as before:
Code:
$this->set_message('foo', 'This %s is foo');

For methods, the error message is picked up by the _execute() method.

I have mine in a language file, same as for the built-in methods. If you want to do that, create a form_validation_lang.php in application/language/en, and add this
Code:
<?php
// load the original language file, instead of copy, to avoid possible merge issues later
@require(BASEPATH.'language/en/form_validation_lang.php');

// unique, exists, etc are methods in MY_Form_validation

$lang['unique']                 = 'The value of the %s field is already in use.';
$lang['exists']                 = 'The value of the %s field does not exist.';
$lang['form_nonce_issued']      = 'Form has expired. Please reload the form and start again.';
$lang['form_nonce_consumed']    = 'You can not post the same information twice. Did you use the back button?';

/* End of file form_validation_lang.php */
/* Location: ./exitecms/language/en/form_validation_lang.php */
#7

[eluser]CodeIgniterNewbie[/eluser]
Thanks. This worked for me.




Theme © iAndrew 2016 - Forum software by © MyBB