is_unique issue on update |
I have an issue when update an user with is_unique rule.
I would like ignore email address if email is the same, otherwise the mail have to be check for haven't duplicates. How can i do? I have read here: https://codeigniter4.github.io/userguide...able-rules but something doesn't works. PHP Code: $rules = ([ Thanks for help me.
You would need to get the user record from the database first then compare the emails.
Build your data with or without email then update the users record. If you already have the users id etc.; on login then you could store the email in the session and check it there without the extra trip to the database. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
is_unique allows you to check directly against the database to ensure the value is unique for a certain field. This is quite simple when you are creating a record, but updating is a little bit tricky.
If you have a table Users with fields 'id', 'name' and 'email', such as: id, name, email -------------------------- 1, a, [email protected] 2, b, [email protected] and you use a rule like: 'email' => 'is_unique[Users.email]' then an entry will be invalid for if it has an email of [email protected], since it would be a duplicate. This is fine for creating new records, but when you are trying to update the record with id=1, email should still check for uniqueness (you don't want to be able to change record 1's email to [email protected] for example), but you do want to exclude [email protected] from the validation check (since updating it to that value would not be a duplicate). The way you do that is by setting the exception field in the rule, like this: 'email' => 'is_unique[Users.email,id,{id}]', which you can read like: Field 'email' must be unique except when id is "this record's id". So when you update record '1', it will make sure the email is unique against every other email except for the record with id = 1. You should not replace {id} with the record's id, though, the framework will do that for you.
This is the solution:
PHP Code: $id = $this->request->getPost('id');
(03-25-2021, 08:43 PM)InsiteFX Wrote: You would need to get the user record from the database first then compare the emails.I have a controller which updates data in 3 different models i.e. users controller updates data in both users, address and contacts tables. each validation rules are stored in their respective models but when i try to update, the validation rule fails at is_unique claiming duplicates email and phone number have been detected Contacts model: code sample PHP Code: protected $validationRules = [ i tried to change the the validation placeholder. code sample below PHP Code: 'email' => 'permit_empty|valid_email|is_unique[contacts.email,id,{users.id}]' Any insight on how I should proceed or an alternative approach is much appreciated.
(04-20-2021, 10:47 PM)llyimo1920 Wrote:(03-25-2021, 08:43 PM)InsiteFX Wrote: You would need to get the user record from the database first then compare the emails.I have a controller which updates data in 3 different models i.e. users controller updates data in both users, address and contacts tables. each validation rules are stored in their respective models but when i try to update, the validation rule fails at is_unique claiming duplicates email and phone number have been detected try this 'email' => 'permit_empty|valid_email|is_unique[contacts.email,contacts.id, users.id, '.$your_value_of_id.']'
Is there any way to extend the Validation Library or the core Validation\Rules library to modify methods like "is_unique" in order to trim the string before validation? Documentation is not clear on this at all.
(03-26-2021, 07:05 AM)andre.tannus Wrote: is_unique allows you to check directly against the database to ensure the value is unique for a certain field. This is quite simple when you are creating a record, but updating is a little bit tricky. Has anyone found a solution to this? I am still getting the same problem.
The solution is in the text you quoted.
The way you do that is by setting the exception field in the rule, like this: 'email' => 'is_unique[Users.email,id,{id}] |
Welcome Guest, Not a member yet? Register Sign In |