The cake php validation ‘isUnique’ gives error on edit
var $validate = array(
'name' => array(
'notempty' => array(
'rule' => array('notempty'),
),
'isUnique' => array (
'rule' => 'isUnique',
'message' => 'This person name already exists.'
)
)
);
Here when add a new person with existing name gives an error This person name already exists that is fine.
But on edit of already existing person also gives the same isUnique error.
How I can sort it out ?
Try:
From the book:
If, however, they are allowed to change the name, and you still need uniqueness from other records, you may need a custom unique validation which would check that if it is identical to the record of the id I’m editing allow that, otherwise fail.
Edit:
Do NOT use the following – cakes built-in isUnique validation handles it already
Note – untested code (its late, I will test in the morning) but it will give you the idea
Add the following to the model and change ‘rule’ => ‘isUnique’, to ‘rule’ => ‘isUniqueName’ and remove ‘on’ => ‘create’