PHP:CodeIgniter $ this-> form_validation-> run()验证两次?

时间:2013-01-29 18:11:30

标签: forms codeigniter validation

完成

是否有机会在控制器操作中两次使用$ this-> form_validation-> run()? CodeIgniter似乎只验证了第一条规则。我想使用$ this-> form_validation-> run()两次,因为我需要第二个验证规则的验证值;)。

$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 个答案:

答案 0 :(得分:0)

尝试在加载时为form_validation库分配别名?这可能会解决问题。

$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)
    {
        //...
    }
}

答案 1 :(得分:0)

在核心文件中 - CodeIgniter_2.1.3 / system / libraries / Form_validation.php 添加一行

$this->_field_data = array();
方法run();

中的

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