Bug. Form validation fails if form array element is empty |
[eluser]loosetops[/eluser]
Validation rules are not applied to fields that are empty and not required. However, if the field is part of an array and it is empty, the rules are applied(even if it is not required) and the validation consequently fails. For example. If my form has an array of fields called names Code: echo form_input('names[]',''); With this validation rule for the names Code: $config = array If any of the input field is left blank, the validation will fail!!!. I have looked through the form_validation class and array form fields are validated one by one recursively and with such a methodology I wouldn't have expected this bug to exist. But it does. The ideal solution would be to root out the bug from the validation class most likely in the _execute() method. Here is an alternate work around. The solution is to apply the rule to each array element individually instead of the array as a whole. This is the function that does just that Code: function validation_config_fix(&$config,$field,$label,$rules){ Use it as follows Code: validation_config_fix($config,'names','Name','alpha_dash'); I put the function in a helper file. Hope that helps anyone that is stuck up.
[eluser]Unknown[/eluser]
I was fighting with this problem, and I think I figured it out. In the Form_validation library, each field's postdata – $this->_field_data[$field]['postdata'] – is initialized to NULL when set_rules() is called. Subsequently, when _execute() is called on the field, there's a check to determine whether the field is required, so that it can bail out early for empty fields. The check assumes that blank fields will evaluate to NULL: Code: 482. // If the field is blank, but NOT required, no further tests are necessary But if a field contains an array of values, when _execute() is called recursively on each one $postdata is no longer NULL. In the case of an empty field, the $val passed to the recursive call is an empty string. As a quick fix, I extended the Form_validation library to nullify empty $postdata values before the _execute() function gets ahold of them: Code: class MY_Form_validation extends CI_Form_validation { A better fix might be to change line 484. to use empty() instead of is_null(): Code: 484. if ( ! in_array('required', $rules) AND empty($postdata)) I have no idea how to submit a patch, but I'll try to figure out how now, in case this is actually useful :-)
[eluser]loosetops[/eluser]
Very subtle bug. Nice you rooted it out. Yeah, you should submit a patch or post an issue at github with a the solution.
[eluser]Mikhail Menshinskiy[/eluser]
Thanks, alertmybanjos for sharing a code. But I think that your code need to change on following: Code: class MY_Form_validation extends CI_Form_validation { How can you see I replace a empty() function by strlen() for the string variable, because empty() function return TRUE for the next string '0' and Form_validation plugin return a FALSE despite the fact that user enter a '0'. Sorry for my bad English. Thanks. =)
[eluser]Nacho[/eluser]
Currently, how I can implement this fix? Where should I put it?
[eluser]Mikhail Menshinskiy[/eluser]
You should to: 1. create new file with name My_Form_validation in the application/libraries/ folder 2. put code above to this file
[eluser]Nacho[/eluser]
Mikhail Menshinskiy thank you very much for your response. I have a problem with a form with multiple arrays to validate. I'll show you the code to see if I can give some help View: Code: <?php for ($i=0; $i<=X; $i++): ?> Controller: Code: $this->form_validation->set_rules('selec[]', 'Seleccion', 'required'); Does not properly validate first, and second fields not populated when an error occurs. Sorry for my english! Thanks!
[eluser]Mikhail Menshinskiy[/eluser]
See: Codeigniter User Guide: Form validation Quote:If you do use an array as a field name, you must use the EXACT array name in the Helper Functions that require the field name, and as your Validation Rule field name. In this topic discussed another question. And your problem is an uncorrect working with CI library. And you have no a problem described above. You should: 1. In controller: load "form_validation" library 2. set up of validation rules. For your example Code: $this->form_validation->set_rules('selec[]', 'Seleccion', 'required'); 3. If validation is failed then load a view: Code: if ($this->form_validation->run() === FALSE) 4. In view: Code: <?php for ($i=0; $i<=3; $i++): ?> And please read a Codeigniter Guide for more information. Good luck =)
[eluser]Nacho[/eluser]
Thanks Mikhail, but now I do it in the manner shown in the documentation and as you say but when I send the form, continues to validate me wrong. I have to fill all fields. I created the file MY_Form_validation to "application / libraries / and put the code you gave me. Any ideas?
[eluser]Mikhail Menshinskiy[/eluser]
If you want you can send me an example of your code and I try to resolve your problem. Send me a sample of controller and view. What is version of CI? |
Welcome Guest, Not a member yet? Register Sign In |