Welcome Guest, Not a member yet? Register   Sign In
Running validation multiple times on single request
#1

[eluser]feel[/eluser]
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:
Code:
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):
Code:
$this->form_validation = new CI_Form_validation();

Any ideas?
#2

[eluser]ontguy[/eluser]
You could dynamically create a validation rule each row
Code:
$this->form_validation->set_rules('row'.$i, 'Row', 'required');
#3

[eluser]feel[/eluser]
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?
#4

[eluser]ontguy[/eluser]
Does unsetting the $_POST work?
Code:
unset($_POST);

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.
#5

[eluser]feel[/eluser]
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.
#6

[eluser]ontguy[/eluser]
If after step 2. you unset $_POST? Do the error messages still persist?

Could try one of these:
Code:
unset($_POST);
or
$_POST = array();
or
unset($this->form_validation);
#7

[eluser]feel[/eluser]
Yep, I tried all that with no luck
#8

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




Theme © iAndrew 2016 - Forum software by © MyBB