doushao1087
2016-11-15 18:56
浏览 11
已采纳

CodeIgniter - 验证不会发生

I am doing a validation using Code Igniter. I have a simple form which used to create / update a product record.

public function productSave($params) {
    if (!$this->validate($params)) {
        return FALSE;
    }
    $id_col_value = $params['product_id'];

    if ($id_col_value) {
        // update
        $this->db->where("product_id", $id_col_value);
        return $result = $this->db->update("product", $params);
    } else {
        // insert
        return $result = $this->db->insert("product", $params);
    }
}

My validation function looks like this,

protected function validate($params) {
    $this->form_validation->set_rules("name", "", "trim|required");
    $this->form_validation->set_data($params);
    if ($this->form_validation->run() == FALSE){
        $errors = $this->form_validation->error_array();
        if ( $errors ){
            return FALSE;
        }
    }
    return TRUE;
}

Now the problem

When inserting (when product_id is null) it validates. But when we have a product_id it does not validates.

My payload ($params) looks like this,

Array
(
    [product_id] => 1 // this is NULL when we do inserting
    [name] => Test Name
)

I can't figure out what I am doing wrong here and why the validation not work for updating. I use the same code for both cases.

Please Help! Thanks in advance!

图片转代码服务由CSDN问答提供 功能建议

我正在使用Code Igniter进行验证。 我有一个简单的表单,用于创建/更新产品记录。

  public function productSave($ params){
 if(!$ this-> validate($ params)){
 return FALSE; 
} 
 $  id_col_value = $ params ['product_id']; 
 
 if if($ id_col_value){
 // update 
 $ this-> db-> where(“product_id”,$ id_col_value); 
返回$  result = $ this-> db-> update(“product”,$ params); 
} else {
 // insert 
 return $ result = $ this-> db-> insert(“product”  “,$ params); 
} 
} 
   
 
 

我的验证函数如下所示,

   protected function validate($ params){
 $ this-> form_validation-> set_rules(“name”,“”,“trim | required”); 
 $ this-> form_validation-> set_data($ params  ); 
 if($ this-> form_validation-> run()== FALSE){
 $ errors = $ this-> form_validation-> error_array(); 
 if($ errors){\  n返回FALSE; 
} 
} 
返回TRUE; 
} 
   
 
 

现在问题

插入(当<代码 > product_id 为null)它验证。 但是当我们有 product_id 时,它不会验证。

我的有效负载( $ params )看起来像这样,

  Array 
(
 [product_id] =&gt; 1 //当我们插入
 [name] =&gt; Test Name 
)
   
 
 

我无法弄清楚我在这里做错了什么以及为什么验证不适用于更新。 我对两种情况使用相同的代码。

请帮助! 提前致谢!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douhui9380 2016-11-17 17:23
    已采纳

    I figure it out.

    "We have to call the set_data() method before defining any validation rules."

    Which is in the Documentation of CI.

    So, basically, I call $this->form_validation->set_data($params); first and then, I call $this->form_validation->set_rules("name", "", "trim|required");.

    This will solve the problem.

    Hope this helps anyone with the same error!!!

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douluohan3403 2016-11-15 19:22
    1. Check your params array. I have checked it using $params = array("product_id" => 34, "name" => "Test Name") & it's working perfectly.

    2. If you have no inserted data with that product_id, it will not work. Suppose you have passed product_id=3 & this id is not available in table. Then how should it work?

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题