Got it figured out! I kept using the debug(); function until my
variable values made sense.
I hope this helps people who are trying to validate against the
database while editing a user.
It makes sure the value isUnique. Just adjust the fieldname to what
you need to validate against.
function checkUnique()
{
if($this->find('count', array('conditions' =>
array('User.username'
=> $this->data['User']['username']))) && $this->id > 0 )
{
$match_user = $this->find('first',
array('conditions' =>
array('User.username' => $this->data['User']['username'])));
if($this->id !=
$match_user['User']['id'])
{
return false;
}
}
return true;
}
On Sep 10, 2:16 pm, Lance <[email protected]> wrote:
> I don't know why I thought that was working but 'user.id' isn't right.
> I don't know. Can anyone help!
>
> Thanks,
>
> Lance
>
> On Sep 10, 1:55 pm, Lance <[email protected]> wrote:
>
> > I think I figured it out. I got this to work for me. Thanks so much
> > for the help.
>
> > var $validate = array(
>
> > 'rule' => array('checkUnique', 'username'),
> > 'on' => 'update'
> > );
>
> > function checkUnique()
> > {
> > if($this->find('count', array('conditions' =>
> > array('User.username'
> > => $this->data['User']['username']))) && $this->id > 0 )
> > {
> > if($this->id != 'user.id')
> > {
> > return false;
> > }
> > }
> > return true;
> > }
>
> > On Sep 10, 12:56 pm, Lance <[email protected]> wrote:
>
> > > Thanks for all the good advice. I'm a newbie and could use a little
> > > more specific help if you have an extra minute.
> > > So if I created a method in the User model like. I obviously am not
> > > very good at programming. Can you guide me in a better direction.
> > > Sorry for taking more of your time.
>
> > > Lance
>
> > > var $validate = array(
> > > 'rule' => array('checkUnique', 'username'),
> > > 'on' => 'update'
> > > );
>
> > > function checkUnique()
> > > {
> > > if($this->find('all', array('conditions' =>
> > > array('User.username' =>
> > > $this->data['User']['username'], array('User.id' => $this->data['User']
> > > ['id'])))) )
> > > {
> > > $this->invalidate('username_unique');
> > > return false;
> > > }
> > > return true;
> > > }
>
> > > On Sep 10, 12:23 pm, cricket <[email protected]> wrote:
>
> > > > On Fri, Sep 10, 2010 at 3:21 PM, cricket <[email protected]> wrote:
> > > > > On Fri, Sep 10, 2010 at 3:06 PM, Lance <[email protected]> wrote:
> > > > >> I actually want to check isUnique if they edit their username or
> > > > >> email
> > > > >> address.
> > > > >> I want it to check their changed username or email against the
> > > > >> database to make sure it already doesn't exist.
> > > > >> Sorry for the confusion. Thanks again!
>
> > > > > My fault, I didn't read that carefully.
>
> > > > > In this case, you could create a validation method that fetches both
> > > > > the ID and the value of whichever column you're checking. If it exists
> > > > > AND the ID is not the current model ID then return false.
>
> > > > > During validation, you can check $this->id for the current model's ID.
>
> > > > Sorry, I meant to add that you can keep the built-in isUnique with
> > > > 'on' => 'create' and then add your second, custom rule with 'on' =>
> > > > 'update'.
>
>
Check out the new CakePHP Questions site http://cakeqs.org and help others with
their CakePHP related questions.
You received this message because you are subscribed to the Google Groups
"CakePHP" 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