Welcome Guest, Not a member yet? Register   Sign In
is_unique issue on update
#9

(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.

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.


Has anyone found a solution to this? I am still getting the same problem.
Reply


Messages In This Thread
is_unique issue on update - by eleumas - 03-25-2021, 11:12 AM
RE: is_unique issue on update - by InsiteFX - 03-25-2021, 08:43 PM
RE: is_unique issue on update - by llyimo1920 - 04-20-2021, 10:47 PM
RE: is_unique issue on update - by Ahyaislive - 11-12-2021, 12:02 AM
RE: is_unique issue on update - by eleumas - 03-26-2021, 07:15 AM
RE: is_unique issue on update - by charleyskira - 12-16-2021, 08:30 AM
RE: is_unique issue on update - by kenjis - 12-16-2021, 09:48 PM
RE: is_unique issue on update - by BilltheCat - 01-20-2022, 03:00 AM
RE: is_unique issue on update - by Mano - 04-01-2022, 08:56 AM
RE: is_unique issue on update - by InsiteFX - 04-09-2022, 12:43 AM
RE: is_unique issue on update - by kenjis - 04-09-2022, 01:01 AM
RE: is_unique issue on update - by Mano - 04-10-2022, 03:04 AM
RE: is_unique issue on update - by ingrenee - 05-30-2022, 10:19 AM
RE: is_unique issue on update - by danger89 - 10-07-2022, 02:44 PM



Theme © iAndrew 2016 - Forum software by © MyBB