Using Symfony1, how could I validate one of two fields is being filled in in a form? They are but not required fields but I need the user to fill in one of the two field. This is my attempt so far, but it doesn’t work:
$this->setValidator('phone', new sfValidatorAnd(
array(
new sfValidatorSchemaCompare('email', '==', ''),
new sfValidatorSchemaCompare('phone', '==', ''),
),
array(),
array(
'invalid' => 'El e-mail no tiene un formato correcto',
'required' => 'Campo obligatorio',
)
));
When comparing two separate fields you should use a global validator: http://www.symfony-project.org/forms/1_2/en/02-Form-Validation#chapter_02_global_validators. Your current approach will always mark the phone field as invalid when an error occurs. Also, the conditions you supply to the validator should return true when the values are valid so in your case you should be using a ValidatorOr with != comparisons like this:
Hope that helps.