CodeIgniter Forums
is_unique issue on update - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: is_unique issue on update (/showthread.php?tid=78903)

Pages: 1 2


is_unique issue on update - eleumas - 03-25-2021

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/libraries/validation.html?highlight=validation#available-rules but something doesn't works.

PHP Code:
$rules = ([
        'email'    => [
          'label'  => 'Email',
          'rules'  => 'required|min_length[3]|max_length[50]|valid_email|is_unique[tbl_users.X,X,X]',
        ],
      ]); 

Thanks for help me.


RE: is_unique issue on update - InsiteFX - 03-25-2021

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.


RE: is_unique issue on update - andre.tannus - 03-26-2021

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.


RE: is_unique issue on update - eleumas - 03-26-2021

This is the solution:
PHP Code:
$id $this->request->getPost('id');

$rules = ([
        'email'     => [
          'label'   => 'Email',
          'rules'   => 'required|valid_email|is_unique[tbl_users.email,id,{id}]',
        ],
      ]); 



RE: is_unique issue on update - llyimo1920 - 04-20-2021

(03-25-2021, 08:43 PM)InsiteFX Wrote: 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.
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      = [
        
'email' => 'permit_empty|valid_email|is_unique[contacts.email,id,{id}]',
        
'telephone' => 'permit_empty|is_unique[contacts.telephone,id,{id}]',
        
'mobile' => 'permit_empty|is_unique[contacts.mobile,id,{id}]',
        
'fax' => 'permit_empty|is_unique[contacts.fax,id,{id}]',
    ]; 

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}]' 
But I am still getting the same error. 
Any insight on how I should proceed or an alternative approach is much appreciated.


RE: is_unique issue on update - Ahyaislive - 11-12-2021

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

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.
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      = [
 
'email' => 'permit_empty|valid_email|is_unique[contacts.email,id,{id}]',
 
'telephone' => 'permit_empty|is_unique[contacts.telephone,id,{id}]',
 
'mobile' => 'permit_empty|is_unique[contacts.mobile,id,{id}]',
 
'fax' => 'permit_empty|is_unique[contacts.fax,id,{id}]',
 ]; 

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}]' 
But I am still getting the same error. 
Any insight on how I should proceed or an alternative approach is much appreciated.

try this
'email' => 'permit_empty|valid_email|is_unique[contacts.email,contacts.id, users.id, '.$your_value_of_id.']'


RE: is_unique issue on update - charleyskira - 12-16-2021

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.


RE: is_unique issue on update - kenjis - 12-16-2021

See https://codeigniter4.github.io/CodeIgniter4/libraries/validation.html#creating-custom-rules


RE: is_unique issue on update - oneloop - 01-20-2022

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


RE: is_unique issue on update - BilltheCat - 01-20-2022

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}]