Welcome Guest, Not a member yet? Register   Sign In
[CI3] Form validation callable bug
#1

I have a checkbox input field which I want to validate. The HTML is irrelevant. To set my validation rules I use the form_validation.php config file. Here's the relevant part of the array:

PHP Code:
array(
    
'field' => 'terms',
    
'label' => 'lang:reg_lbl_terms',
    
'rules' => array(array('terms_accepted', array($ci->validation_rules'terms_accepted'))) // $ci->validation_rules refers to a loaded library.
    
), 

According to the documentation (http://www.codeigniter.com/user_guide/li...-as-a-rule) this should work fine. However, if the checkbox is left empty, the validation rules are not checked at all (I've tested this by adding something like "echo 'test'; exit;" to the callback function). This obviously has to do with the fact that a checkbox value isn't posted when it's left empty. HOWEVER, by using callbacks the old-fashioned way, the function DOES get checked:

PHP Code:
array(
    
'field' => 'terms',
    
'label' => 'lang:reg_lbl_terms',
    
'rules' => 'callback_terms_accepted'
    
), 

In this case everything works just fine. Of course, this isn't an option for larger websites, since you sometimes need the same validation callbacks in multiple controllers and copy-pasting them is bad practice.

Also, adding 'required' anywhere in the rules array in the first code sample causes the 'required' rule to be checked, but any other rules to be ignored.

I would prefer not having to add a hidden input field to the form as a workaround, since this causes security issues (people can just remove the field from the DOM and ignore validation rules).

I would love it if this issue would get fixed. If anyone knows any acceptable workarounds please let me know.

Thanks!
Reply


Messages In This Thread
[CI3] Form validation callable bug - by WouterFlorijn - 09-23-2015, 08:32 AM
RE: [CI3] Form validation callable bug - by Narf - 09-23-2015, 09:58 AM



Theme © iAndrew 2016 - Forum software by © MyBB