【发布时间】:2016-11-15 18:56:24
【问题描述】:
我正在使用 Code Igniter 进行验证。我有一个用于创建/更新产品记录的简单表格。
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);
}
}
我的验证函数是这样的,
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;
}
现在的问题
在插入时(当product_id 为空时)它会验证。但是当我们有 product_id 时,它不会验证。
我的有效载荷 ($params) 看起来像这样,
Array
(
[product_id] => 1 // this is NULL when we do inserting
[name] => Test Name
)
我无法弄清楚我在这里做错了什么以及为什么验证不适用于更新。我对这两种情况都使用相同的代码。
请帮忙!提前致谢!
【问题讨论】:
标签: php codeigniter validation