it does check on "id != ownId" automatically...
On 25 Jul., 23:20, Rick <will...@gmail.com> wrote:
> Found my own answer/solution with this custom validator. I put this
> in my app_model and then can use it from any model. You use it as a
> rule as you would any core validator:
>
> [code]
> var $validate = array(
> 'partnumber' => array(
> 'rule1' => 'validateUniqueness',
> 'message' => 'This part number is not unique')
> )
> [/code]
>
> And the code is:
>
> [code]
> /**
> * Fails if given value for field is not unique for that field in the
> table.
> *
> * @param <type> $fieldData - array(fieldname => fieldvalue)
> * @return <type> - true if unique.
> */
> function validateUniqueness($fieldData) {
> $fields = array_keys($fieldData);
> $fieldName = $fields[0];
>
> $values = array_values($fieldData);
> $fieldValue = $values[0];
>
> // To determine whether we are going to insert (new) or update
> (edit)
> // get the current row ID. If empty then this is a new record
> // if not then it is an edit record.
>
> // If this is an edit then exclude the currentRow (the one we are
> // editing) from the uniqueness test by adding a condition.
>
> $currentID = $this->getID();
> if (!empty($currentID) or $currentID = 0) {
> $rowID = $this->data[$this->name][$this->primaryKey];
> $condition = ' AND ' . $this->name . '.' . "$this->primaryKey !=
> '$rowID'";
> } else {
> $condition = '';
> }
>
> if ($this->findCount("$fieldName = '$fieldValue' $condition", -1) >
> 0) {
> return false;
> } else {
> return true;
> }
> }
> [/code]
>
> Rick
--~--~---------~--~----~------------~-------~--~----~
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