If anyone else is looking for some drop in code to handle unique
validation, here's what I came up with.
This is a generic function you can drop into your app_model:
/**
* checks is the field value is unqiue in the table
* note: we are overriding the default cakephp isUnique test as the
original appears to be broken
*
* @param string $data Unused ($this->data is used instead)
* @param mnixed $fields field name (or array of field names) to
validate
* @return boolean true if combination of fields is unique
*/
function checkUnique($data, $fields) {
if (!is_array($fields)) {
$fields = array($fields);
}
foreach($fields as $key) {
$tmp[$key] = $this->data[$this->name][$key];
}
if (isset($this->data[$this->name][$this->primaryKey])) {
$tmp[$this->primaryKey] =
"<>".$this->data[$this->name][$this-
>primaryKey];
}
return $this->isUnique($tmp, false);
}
}
and is used in your model validate:
var $validate = array(
"name"=>array(
"unique"=>array(
"rule"=>array("checkUnique", array("name",
"institution_id")),
"message"=>"A contact with that name already
exists for that
institution"
)
)
);
It will handle single or multiple fields. e.g. In the example above,
the combination of name AND instution_id must be unique.
It also handles the 'edit' gotcha too.
Thanks for all your help :-)
On Nov 30, 6:35 am, RichardAtHome <[EMAIL PROTECTED]> wrote:
> Thanks for the additional feedback.
>
> I'd already discovered the 'edit' gotcha and created a workaround
> similar to Involution's.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---