CodeIgniter Forums
Modify multiple fields in form - CI4 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Modify multiple fields in form - CI4 (/showthread.php?tid=76076)

Pages: 1 2


Modify multiple fields in form - CI4 - Marcolino92 - 04-13-2020

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)){ 



RE: Modify multiple fields in form - CI4 - includebeer - 04-13-2020

PHP Code:
‘is_unique[categories.category_name.'.$this->request->getVar('category_name').']‘ 

Your is_unique rule is not correct. You should exclude the current row by passing its id. Also, the parameters should be separated by a coma not a dot.

PHP Code:
‘is_unique[categories,id,'.$this->request->getVar('category_id')']’ 



RE: Modify multiple fields in form - CI4 - Marcolino92 - 04-13-2020

Hi, thanks for the reply. I don't wish I had a bad shot, but if the field that I don't want to be duplicated is "category_name" why do I have to set category_id?


RE: Modify multiple fields in form - CI4 - Marcolino92 - 04-13-2020

I have two fields in a form, title and description. I wish I could say that if the "title" is unchanged, it does not require validation and automatically remains the same.

This happens when I am modifying only the "description" field, the title field remains the same ... and rightly I get the error that that category title already exists in the "categories" table.


RE: Modify multiple fields in form - CI4 - includebeer - 04-13-2020

(04-13-2020, 07:45 AM)Marcolino92 Wrote: Hi, thanks for the reply. I don't wish I had a bad shot, but if the field that I don't want to be duplicated is "category_name" why do I have to set category_id?

Because that’s how you can exclude the row you are currently updating from the validation.


RE: Modify multiple fields in form - CI4 - Marcolino92 - 04-13-2020

I am not able to understand, could you give me an example? Thank you


RE: Modify multiple fields in form - CI4 - InsiteFX - 04-14-2020

Simple if your updating a field you need to use the database update method.

If your adding a new database record then you need to use the database method insert.

When updating a database record you need the record id which is what @ includebeer is
telling you to use. add a hidden form field to hold the record id.

Then you can check if it's an update or new record in your model and either do the update
or a new insert.


RE: Modify multiple fields in form - CI4 - includebeer - 04-14-2020

In this case it’s not a matter of telling if it’s an insert or an update. To correctly validate a unique field, you must exclude the current record you are inserting or updating, or else it will never be unique and never be valid.

Example: You have these row:
Code:
id    category_name      category_desc
1     Action             Action movies
2     Comedy             Funny movies
3     Horror             Horror movie

When you update a row, all the field are updated. Even the ones that are not changed. If you want to update the description and you have a unique rule on the name, it will be rejected because it will try to update the name and category and it will find that there’s already a record with that name (update the row id=3 with these values: category_name=‘horror’, category_desc=‘something else’, and tell CI to make sure no rows already use ‘Horror’ as its name, it will find that ‘Horror’ is already used by row id 3)

So you need to tell CI to exclude the current row when doing the validation (update the row id=3 with these values: category_name=‘horror’, category_desc=‘something else’, and tell CI to make sure no rows already use ‘Horror’ as its name, excluding row id 3, it will not find any other row with the name ‘Horror’ and it will be valid)


RE: Modify multiple fields in form - CI4 - Marcolino92 - 04-14-2020

I did other tests, but nothing. I think I sensed the logic but I am not able to apply it. I know I ask a lot, but could you give me a quick example on how to exclude validation?


RE: Modify multiple fields in form - CI4 - includebeer - 04-14-2020

Don’t try to update only the field that were changed. Post all the fields, update all the fields, validate all the fields. Much simpler and reliable.

PHP Code:
$data['validation']->setRules([
            'category_name' => ['label' => 'Category Name''rules' => “required|min_length[3]|max_length[150]|is_unique[categories,id,{$id}]],
            'category_desc' => ['label' => 'Category Description''rules' => 'required|min_length[5]']
        ]);