Monday, December 1, 2008

Re: validation in model SOLVED

I solved this problem by using the following function in my model, hope this
can help anybody in the future

function _isUnique($check, $field) {

if(isset($this->data[$this->name]['id']))
{
//make sure that there isn't one on another id that is same as
this field
$results =
$this->find($this->name.'.id<>"'.addslashes($this->data[$this->name]['id']).'"
AND '.$this->name.'.'.$field.'="'.addslashes($value).'"');

} else {
//make sure it doesn't currently exist in the db (we're
creating a new one)
$results =
$this->find($this->name.'.'.$field.'="'.addslashes($check[$field]).'"');
}

if(!empty($results))
{
return false;
} else {
return true;
}
}


AngeloZanetti wrote:
>
> thanks, yes i just discovered that myself and now I realise that the
> validation has to be different for create and update.
>
> create: needs to check that the name is unique,
> update: is unique except for the edited record.
>
> Is the best way to accomplish this by creating another function for
> validation on 'update'?
>
> or change the current validation to validate depending on what the action
> is?
>
> thanks
>
>
> David C. Zentgraf wrote:
>>
>>
>> What you're looking for is this:
>> http://book.cakephp.org/view/131/on
>>
>> On 1 Dec 2008, at 16:06, AngeloZanetti wrote:
>>
>>>
>>>
>>> Hi all,
>>>
>>> Is it possible to have model validation only be called when there is
>>> an add
>>> action and not edit for example ?
>>>
>>> I have added unique name validation but now with the edit of a
>>> record it
>>> still gives a validation error as that name of that record already
>>> exists.
>>>
>>> My code as follows:
>>>
>>> var $validate = array(
>>> 'question' => array(
>>> 'Question already exists, please enter a
>>> unique
>>> question' => array(
>>> 'rule' => array(
>>>
>>> 'isUnique',
>>> 'question'
>>> )
>>> ),
>>> 'This field cannot be left blank' =>
>>> VALID_NOT_EMPTY
>>> ),
>>>
>>> function isUnique($field, $value) {
>>>
>>> $fields[$this->name.'.'.$value] = $field[$value];
>>> $this->recursive = -1;
>>>
>>> if ($this->hasAny($fields)) {
>>> $this->invalidate('unique_'.$field);
>>> return false;
>>> } else {
>>>
>>> return true;
>>> }
>>> }
>>>
>>> thanks in advance.
>>>
>>> --
>>> View this message in context:
>>> http://www.nabble.com/validation-in-model-tp20767005p20767005.html
>>> Sent from the CakePHP mailing list archive at Nabble.com.
>>>
>>>
>>> >
>>
>>
>> >>
>>
>>
>
>

--
View this message in context: http://www.nabble.com/validation-in-model-tp20767005p20768175.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: