In class AppModel:
class AppModel extends Model{
function checkUnique($data, $fieldName) {
$valid = false;
if(isset($fieldName) && $this->hasField($fieldName))
{
$valid = $this->isUnique(array($fieldName => $data));
}
return $valid;
}
}
Then for example in the User model:
class User extends AppModel {
var $validate = array(
array('rule'=>array('checkUnique', 'username'), 'message' => 'Username
taken. Please use another one.' )
);
}
It gives the correct behavior for me (check uniqueness when adding,
don't check uniqueness when editing unless the username was changed to
another existing username during edit).
On Dec 1, 3:30 pm, AngeloZanetti <angelo...@gmail.com> wrote:
> I have got a similiar problem that the validation works for adding must it
> mustn't validate when editing the record as its going to have the same name
> in DB as the form when editing.
>
> how can we disable this for the edit function? or must it be done in the
> controller?
>
> jonknee wrote:
>
> >> In fact, it seems that this built-in rule doesn't work!
> >> Why is this not yet fixed? We allways need to check unicity of a field
> >> in all of our applications...
>
> > What's not working for you? It seems to be working for me. I had a
> > custom method until I was told about isUnique working through
> > validation and after updating my code it seemed to keep working.
>
> --
> View this message in context:http://www.nabble.com/Check-Unique-in-a-model-tp16473557p20767209.html
> Sent from the CakePHP mailing list archive at Nabble.com.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---
No comments:
Post a Comment