CodeIgniter Forums
Custom form validation callback error message not working right - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Custom form validation callback error message not working right (/showthread.php?tid=75336)



Custom form validation callback error message not working right - dwlamb - 01-27-2020

Validating a form, I have set-up a callback on a validation rule.  The validation is working properly but for reasons not understood Codeigniter is not returning the error message set in the callback function. Here is the code:

The rule set in the Controller:
PHP Code:
$this->form_validation->set_rules('group_id''Group ID''callback_group_id_check'); 

The callback function:
PHP Code:
public function group_id_check($group_id) {
    if(!
ctype_digit($group_id)) {
        
$this->form_validation->set_message('group_id''Group id is not acceptable.');
        return 
FALSE;
    } else {
        return 
TRUE;
    } 

I have run the code with and without a debugger.  The syntax above runs without a problem.  If I purposely set 'group_id' with a value that will trigger a false, Codeigniter returns an error message: "Unable to access an error message corresponding to your field name Group ID.(group_id_check)"

I followed the tutorial within Form Validation documentation for CI 3.1.6

Can someone show what I am doing wrong?


RE: Custom form validation callback error message not working right - jreklund - 01-28-2020

It where I while since I tried this without "Callable: Use anything as a rule" functionality. But according to the manual you have named your message string incorrectly.
https://codeigniter.com/user_guide/libraries/form_validation.html#callbacks-your-own-validation-methods

Code:
$this->form_validation->set_message('group_id_check', 'Group id is not acceptable.');



RE: Custom form validation callback error message not working right - dwlamb - 01-28-2020

Yes, indeed I have. Thanks for pointing that out. It is fixed and now working properly.