Welcome Guest, Not a member yet? Register   Sign In
Bug. Form validation fails if form array element is empty
#12

[eluser]Unknown[/eluser]
[quote author="loosetops" date="1277705291"]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[]','');
echo form_input('names[]','');
echo form_input('names[]','');
echo form_input('names[]','');
echo form_input('names[]','');

With this validation rule for the names
Code:
$config =  array
          array(
            'field'   => 'names[]',
            'label'   => 'Name',
            'rules'   => 'alpha_dash'
          )    
      );
$this->load->library('form_validation');
$this->form_validation->set_rules($config);

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){
   if(!is_array($_POST["$field"])){
    return;
   }
   $count = count($_POST["$field"]);
   for($i=0; $i<$count; $i++){
    if(!empty($_POST["$field"][$i])){
        $config[] = array(
            'field'   => "{$field}[$i]",
            'label'   => $label,
            'rules'   => $rules
        );
    }
   }
}

Use it as follows
Code:
validation_config_fix($config,'names','Name','alpha_dash');
$this->load->library('form_validation');
$this->form_validation->set_rules($config[$type]);

I put the function in a helper file.

Hope that helps anyone that is stuck up.[/quote]

i have a trick. how to run other rule when validating is not included rule "required"?

search and replace this script in form_validation library

$callback = FALSE; is_null($postdata))
if ( ! in_array('required', $rules) AND is_null($postdata))
{
// Before we bail out, does the rule contain a callback?
if (preg_match("/(callback_\w+(\[.*?\])?)/", implode(' ', $rules), $match))
{
$callback = TRUE;
$rules = (array('1' => $match[1]));
}
else
{
return;
}
}

to


$callback = TRUE; // changed from FALSE and is locgical !is_null($postdata)) changed from is_null($postdata))
if ( ! in_array('required', $rules) AND !is_null($postdata))
{
// Before we bail out, does the rule contain a callback?
if (preg_match("/(callback_\w+(\[.*?\])?)/", implode(' ', $rules), $match))
{
$callback = TRUE;
$rules = (array('1' => $match[1]));
}
else
{
return;
}
}



Messages In This Thread
Bug. Form validation fails if form array element is empty - by El Forum - 09-11-2013, 09:43 PM



Theme © iAndrew 2016 - Forum software by © MyBB