• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Running validation multiple times on single request

On my page I have a regular form with some validation rules defined in form_validation.php file. There are also some custom validation callbacks defined. It all works fine.

The same input can be provided in an excel file where a single row contains the same data as the form can capture. I would like to use the same rules to validate multiple rows, one by one.

After I read a row from the spreadsheet I populate the $_POST array with the same keys as defined in my webform. Then I call the appropriate validation rule:
if($this->form_validation->run('item/add') == FALSE) ...

The problem is that this works ok only for the first row. For the second row some validation rules are not triggered (e.g. basic 'required' rule). Also, if the validation fails for a single row the error is remembered and all future calls to $this->form_validation->run('item/add') result with the same error message, even if current row is valid.

I tried resetting the validation by calling the following code (with no luck):
$this->form_validation = new CI_Form_validation();

Any ideas?

You could dynamically create a validation rule each row
$this->form_validation->set_rules('row'.$i, 'Row', 'required');

I would like to reuse the ruleset 'item/add' that I already use for my form. Also, the ruleset is quite large and it would take a lot of code to create all rules for a single row.

Also, I don't understand how setting rules manually at each iteration would be different then invoking dynamically specified ruleset (like I currently do)? How would this solve my problem with error messages being persisted between iterations?

Does unsetting the $_POST work?

I had in mind that you were creating one form, built from looping through the excel, then submitting it, but I see you're doing a submission for each row.

Not quite. I only submit the form once, when I upload the excel spreadsheet. Then in my controller I parse the excel file. For each row I read from Excel file I do the same:
1. Set $_POST with values from the row
2. Run validation for ruleset "item/add"

In step 1. I don't unset $_POST but I update the values with the data read from current row.

If after step 2. you unset $_POST? Do the error messages still persist?

Could try one of these:
$_POST = array();

Yep, I tried all that with no luck

OK, I figured it out.
I posted the solution here: http://fczaja.blogspot.com/2011/07/codei...ation.html

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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