Form validation bug while validating an array of input #1473

Closed
v1r opened this Issue Jun 13, 2012 · 5 comments

2 participants

@v1r

Hi there,

this problem occurred while i was trying to import an excel file.


name
foo1 -> col[1][0]
foo2 -> col[2][0]
foo3 -> col[3][0]
"" -> col[4][0] empty input

My validation config :

'foo_form' => array(
array(
'field' => 'name',
'label' => 'name',
'rules' => 'required',
)
)

// Run validation within a loop
// a bit of bad practice

$post = $_POST;
unset($_POST);
$errors = array();

for ($i = 0; $i < count($post['col'][0]); $i++) {

$this->config->load('foo/form_validation');

$_POST['name'] = $post['col'][$i][0];

// Contain foo1, foo2, foo3 and empty string

if ($this->form_validation->run('foo_form') !== FALSE) {

    //success
} else {

    $errors[$post['col'][$i][0] . $i] = $this->form_validation->get_error();
    // The array of error for each row
}

}

// if one fail, all the other field will fail too.
///The problem is caused by the _error_array variable not being reinitialized in the form_validation class.
var_dump($errors);

// Output

//array(3) {
//
// [" 0"]=>
//
// array(1) {
//
// ["name"]=>
//
// string(27) "The name field is required."
//
// }
// [" 1"]=>
//
// array(1) {
//
// ["name"]=> string(27) "The name field is required."
//
// }
// [" 2"]=>
// array(1) {
// ["name"]=> string(27) "The name field is required."
// }
// ["3"]=>
// array(1) {
// ["name"]=> string(27) "The name field is required."
// }
// [" 4"]=>
//
// array(1) {
//
// ["name"]=> string(27) "The name field is required."
// }

// I added this line to core/librarie/form_validation.php
$this->_error_array = array();

Cheers.

@narfbg

Isn't that the same as those #113?
Which CI version are you using?

@v1r

Codeigniter 2.1.0 and not the same issue

@narfbg

Okay, can you try with the latest code from the develop branch here? A lot has changed in it.

@v1r

same problem.

@narfbg

Okay, turned out it's a duplicate of #184 instead. See this comment: #184 (comment)

@narfbg narfbg closed this Jun 21, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment