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

(This post was last modified: 03-25-2021, 11:19 AM by eleumas.)

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 = ([
        'email'    => [
          'label'  => 'Email',
          'rules'  => 'required|min_length[3]|max_length[50]|valid_email|is_unique[tbl_users.X,X,X]',
        ],
      ]); 

Thanks for help me.
Reply
#2

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

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

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}]',
        ],
      ]); 
Reply
#5

(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.
Reply
#6

(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.']'
Reply
#7

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

See https://codeigniter4.github.io/CodeIgnit...stom-rules
Reply
#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
#10

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




Theme © iAndrew 2016 - Forum software by © MyBB