1
votes

DONE

Isn't there any chance to use $this->form_validation->run() in a controller action twice? CodeIgniter seems to validate only the first rules. I want to use $this->form_validation->run() twice, because I need a validated value for a second validation rule ;).

$this->form_validation->set_rules(...);
if($this->form_validation->run() === true)
{
    $value = $this->..._model->get( ...);  //use of validated value
    $this->form_validation->set_rules(...); //use of $value
    if($this->form_validation->run() === true)
    {
        //...
    }
}
2
Have you actually tried it, I don't see any reason it wouldn't work. - Rick Calder
Yes - the second validation rule contains "required" and "greater_than" and the Post Var is empty. But form_validation->run() returns true... o.O - Mike
It's possible it's saving the value from the first run through? Although that makes no sense at all. See my answer below, see if this works. - Rick Calder
Hi, thanks for your help - I found the mistake. My rule was: 'required|greater_than['. $val-0.01 .']'. That doesn't work while CodeIgniter get the label value -0.01 and not the real result out of $val-0.01. Now the validation works also twice :). Codeigniter hasn't any rule that match >= so I have to make a callback for check this? - Mike
Yeah that would be a callback function, glad it works. - Rick Calder

2 Answers

0
votes

Try assigning aliases to the form_validation library when loading it? That might solve the issue.

$this->load->library('form_validation','','validation1');
$this->validation1->set_rules(...);
if($this->validation1->run() === true)
{
    $value = $this->..._model->get( ...);  //use of validated value
    $this->load->library('form_validation','','validation2');
    $this->validation2->set_rules(...); //use of $value
    if($this->validation2->run() === true)
    {
        //...
    }
}
0
votes

In core file - CodeIgniter_2.1.3/system/libraries/Form_validation.php add one line

$this->_field_data = array();

in method run();

public function run($group = '')
    {
        // add this line for clear field data to use validation many times
        $this->_field_data = array();