Welcome Guest, Not a member yet? Register   Sign In
Modify multiple fields in form - CI4
#1

Hi, I have a form to edit existing content (edit categories), a field of these, however, as validation has '' is_unique ", referring to the category name.

The form consists of two fields, title and description.

The problem is that when I update ONLY the description, the validation rightly gives me an error because the current category name is duplicated.

I would need something to identify if the title field is the same as the current one is not changed. I did some tests, but nothing.



PHP Code:
$data['validation']->setRules([
            'category_name' => ['label' => 'Category Name''rules' => 'required|min_length[3]|max_length[150]|is_unique[categories.category_name.'.$this->request->getVar('category_name').']'],
            'category_desc' => ['label' => 'Category Description''rules' => 'required|min_length[5]']
        ]);
        
        
if($this->request->getPost() && $data['validation']->withRequest($this->request)->run()) {

            // update the name if it was posted
            if($this->request->getVar('category_name') == $data['category']->category_name){
                
                $fields
['category_name'] = $this->request->getVar('category_name');
                $fields['category_slug'] = url_title(strtolower($this->request->getVar('category_name')));
                
            
} else {
                $fields['category_name'] = $data['category']->category_name;  
            
}
            
            $fields
['category_desc'] = $this->request->getVar('category_desc');
            
            $builder 
$this->db->table('categories');
            $builder->where('id'$id);
            if($builder->update($fields)){ 
Reply


Messages In This Thread
Modify multiple fields in form - CI4 - by Marcolino92 - 04-13-2020, 06:06 AM



Theme © iAndrew 2016 - Forum software by © MyBB