Symfony forms convenience method: addPostValidator
In your Symfony form, only one post validator can be set, by using $this->getValidatorSchema()->setPostvalidator(). But sometimes you extend a form and a post validator was already set in the parent class, and you don’t want to replace it with your own post validator, but instead, add your own validator to the validatorschema. I wrote a convenience method for this task. Add this method to your form class:
public function addPostValidator(sfValidatorBase $post_validator)
{
if ($existing_post_validator = $this->getValidatorSchema()->getPostValidator())
{
$this->getValidatorSchema()->setPostValidator(new sfValidatorAnd(array($existing_post_validator, $post_validator)));
}
else
{
$this->getValidatorSchema()->setPostValidator($post_validator);
}
}
Use this method for example in your form’s configure or setup method:
$post_validator = new sfValidatorCallback(array('callback' => array($this, 'postValidator'));
$this->addPostValidator($post_validator);