Skip to content

Symfony forms convenience method: addPostValidator

by Matthias Noback on July 8th, 2011

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);

From → Forms, Symfony 1.4

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS