Sindar
Sindar

Reputation: 10839

Having trouble with the CakePHP data validation

i'm currently having some issues by using the cakePHP data validation.

I've tried to use the rule with the argument 'on' => 'created' but it didn't seems to work. My objective is to check during the creation of the user, to check if the mail is not already existing in my database.

But when it's a edit, i don't want to check this, i mean, if the user don't change the mail, obviously this mail is already existing in the database, but it's just the same then before...

So i don't want to check if the mail, have not been changed.

'mail' => array(
                'valid_mail'    => array(
                    'rule'      => array('email', true),
                    'message'   => 'Veuillez insérer un email valide.'
                ),
                'isUnique_mail' => array(
                    'rule'      => 'isUnique',
                    'on'        => 'create',
                    'message'   => 'Ce mail est déjà utilisé.'
                )
            )

Hope you guys understand my problem.

Upvotes: 1

Views: 127

Answers (1)

Francois Deschenes
Francois Deschenes

Reputation: 24969

Part of your problem might be that you're not specifying the ID of the record that needs to be saved using $this->Model->id = $id so when you call the save (or saveAll) function, it's trying to insert a record.

The isUnique validation function actually will filter out the ID of the current record when updating if it's been set using $this->Model->id. You can see this at the bottom of the function (line 2446).

Personally, I'd leave the isUnique validation on all the time to prevent users from changing their emails to one that already exists once they've registered.

Upvotes: 2

Related Questions