3

I'm using Symfony 1.4 and having an issue using multiple form validators.

For part of the form, I need to make sure the email address is valid AND unique. Here's the code I'm trying to use this:

$this->validatorSchema['email_address'] = new sfValidatorAnd(
    array(
        new sfValidatorEmail(),
        new sfValidatorPropelUnique(array('model' => 'Users', 'column' => 'email_address')
    ),

));

As far as I can tell, this SHOULD work. However, when I post the form, I get the following error message

You must pass an array parameter to the clean() method (this validator can only be used as a post validator).

Any ideas or suggestions?

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
Eric
  • 2,201
  • 5
  • 29
  • 35

1 Answers1

5

You can only use the unique validator as a post validator, just like it says. Meaning you can apply it directly to the field - you need to apply it to the schema through mergePostValidator. Functionally this is going to produce the same result youre looking for - eg. even if the email is valid, if it already exists you will get the validation error, but if both pass then the form will be valid.

$this->validatorSchema['email_address'] = new sfValidatorEmail();
$this->mergePostValidator(new sfValidatorPropelUnique(array('model' => 'Users', 'column' => 'email_address'));
prodigitalson
  • 60,050
  • 10
  • 100
  • 114
  • 1
    Thanks for the hint with mergePostValidator. I'm relatively new to symfony so some of the terms are as foreign to me as scientific disease names. I'll note that on the sample code you provided, calling $this->validatorSchema->mergePostValidator(..) created an unknown method error for me and the solution ended up being just $this->mergePostValidator(..) – Eric Mar 04 '12 at 05:23
  • I can confirm that the mergePostvalidator should be called on the form instace itself rather than the validator schema. – luxerama Aug 23 '12 at 10:51