Hi, I have created a custom validation rule, it's like 'is_unique', but with an extra value...
So I can not only check whether a name already exists in the database, but I can check if a user already exists with a specific company_id.
I have added this rule to a model like this:
For INSERTS this works great... But UPDATES always get through, even on duplicates.
I noticed that when the 'company_id' doesn't change (but the name does), the value of 'company_name' is '{company_name}'.
The user entity has a company_id, but it's not passed through to the validation rule because it's an unchanged value.
Resulting a query which checks where 'company_id = {company_id}' instead of 'company_id = 4' for example.
How can I include the data of the unchanged fields as well?
So I can not only check whether a name already exists in the database, but I can check if a user already exists with a specific company_id.
I have added this rule to a model like this:
PHP Code:
protected $validationRules = [
'name' => 'is_double_unique[users,name,company_id,{company_id},id,{id}]',
'id' => 'permit_empty',
'company_id' => 'permit_empty'
];
For INSERTS this works great... But UPDATES always get through, even on duplicates.
I noticed that when the 'company_id' doesn't change (but the name does), the value of 'company_name' is '{company_name}'.
The user entity has a company_id, but it's not passed through to the validation rule because it's an unchanged value.
Resulting a query which checks where 'company_id = {company_id}' instead of 'company_id = 4' for example.
How can I include the data of the unchanged fields as well?
![[Image: dTbxhHp.png]](http://i.imgur.com/dTbxhHp.png)