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!