The sfValidatorPropelUnique in symfony

Nov
17

Today, I wanted to do a signup form but I wanted distinct email addresses to be recorded. After some googlisations, I’ve spotted two problems.

As my field was an email address I thought something like “damn easy, I just have to use something like this: ”

$this->validatorSchema['email_1'] = new sfValidatorAnd(array(
  1.     new sfValidatorEmail(); ,
  2.     new sfValidatorPropelUnique(array(
  3.             'model' => $this->getModelName(),
  4.             'column' => 'email_1'
  5.          ),
  6.          array(
  7.              'invalid' => 'This email address is already assigned to an account.',
  8.          )
  9.     )
  10. ));

Though, after few tries I had an error that I’ve never seen before, saying that it has to be used as a postValidator. Before I understood how to deal with that, I’ve figured out something: I should simply add in my schema

  1. index: unique

in my email field… Damn… But since I’m definitly 2 cool, here is the correct syntax to use in your UserForm.class.php (basically, it shouldn’t be used if you defined correctly your schema.yml though!) :

$this->validatorSchema->setPostValidator(
  1.             new sfValidatorPropelUnique(array(
  2.                 'model' => 'User',
  3.                 'column' => array('email_1')
  4.             ),
  5.             array(
  6.                 'invalid' => 'An account is already registered with this email address.',
  7.             )
  8.         ));

Once again, thanks symfony to allow us escaping from so much troubles !

No related posts.

 

Leave a Reply