Alternatively (and better because reusable in other Models), you could
insert the function _isUnique (documented below) in your own
app_model.php and use the following code in your Model.
Second argument in the 'rule' array is the field that has to be
checked for uniqueness..
var $validate = array(
'login' => array(
'rule' => array('_isUnique', 'login'),
'message' => 'Login name already taken.'
)
);
---------------Insert in app_model.php---------
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($check).'"');
} 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).'"');
}
if(!empty($results))
{
return false;
} else {
return true;
}
}
On Nov 29, 6:15 pm, francky06l <[EMAIL PROTECTED]> wrote:
> Take care when using with edit. Indeed you can edit your data, not
> modifying the field but the field will exist in the DB (the current
> record)...
>
> On Nov 29, 7:54 pm, José Pablo Orozco Marín <[EMAIL PROTECTED]>
> wrote:
>
> > Maybe this help:http://tempdocs.cakephp.org/#TOC121845
>
> > <?php
> > class User extends AppModel
> > {
> > var $name = 'User';
>
> > var $validate = array(
> > 'login' => array(
> > 'rule' => array('checkUnique'),
> > 'message' => 'Login name already taken.'
> > )
> > );
>
> > function checkUnique($data)
> > {
> > $valid = false;
> > if(isset($fieldName) && $this->hasField($fieldName))
> > {
> > $valid = $this->isUnique(array($fieldName => $value));
> > }
> > return $valid;
> > }}
>
> > ?>
>
> > sMAshdot escribió:
>
> > > On 29 Nov., 13:40, RichardAtHome <[EMAIL PROTECTED]> wrote:
>
> > >> As a side note, I suspect isUnique might not meant to be referenced in
> > >> a rule and is meant to be referenced in the controller. If this is
> > >> true can someone point me towards some examples of how / where it
> > >> should be used?
>
> > > I'm quite new to CakePHP as well, but I check for uniqueness after
> > > validating
> > > the data.
>
> > > This example comes from the IBM CakePHP tutorials
> > >http://www.ibm.com/developerworks/views/opensource/libraryview.jsp?se...
>
> > > if ( $this->User->validates($this->data) )
> > > {
> > > if ( $this->User->findByname($this->data['User']['name']) )
> > > {
> > > $this->User->invalidate('name');
> > > $this->set('name_error', 'User already exists.');
> > > }
> > > }
>
> > --
>
> > -----------------------------------------
>
> > José Pablo Orozco Marín
> > [EMAIL PROTECTED]
>
> > Tel. +506 820-7280
>
> > Por favor, visite mi pasión:http://www.SukiaLogic.com/
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---