views:

114

answers:

1

Hi. I'm using symfony with doctrine and I make a registration form. Email field is declared unique and if I set it to already existing one, I get the message An object with the same "email" already exist.. I use the following validator:

$this->validatorSchema['email'] = new sfValidatorEmail(array('required' => true), array('invalid' => 'Does not seem to be a proper email'));

How can I change the "already exists" message? Thanks

+2  A: 

The error on unique field you're seeing occurs at post-validation, so you need to override the validator there, you can do this like so in your form:

$this->validatorSchema->setPostValidator(
  new sfValidatorDoctrineUnique(
    array(
      'model' => 'Profile',
      'column' => array('email'),
      'throw_global_error' => false
    ),
    array(
      'invalid' => 'A user with that %column% already exists'
    )
  )
);

If you need to validate multiple fields e.g. the username stored in sfGuardUser you can pass an sfValidatorAnd (which accepts a single array of multiple validators) to setPostValidator.

Additionally: If you need to retain other existing validators it's worth looking into the mergePostValidator method.

Steve