• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[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

#2
Is this on version 3.0.0 or 3.0.1? I'm pretty sure there was a patch for this in 3.0.1.
Reply

#3
Does is_callable(array($ci->validation_rules, 'terms_accepted')) return true in the context of the form_validation library's _execute() method? Can you call it from an anonymous function as a workaround?
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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