• 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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

#2
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')']’ 
Test your translation files with Translation Tester
Reply

#3
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?
Reply

#4
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.
Reply

#5
(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.
Test your translation files with Translation Tester
Reply

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

#7
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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#8
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)
Test your translation files with Translation Tester
Reply

#9
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?
Reply

#10
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]']
        ]); 
Test your translation files with Translation Tester
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.