Skip to content

Instantly share code, notes, and snippets.

@smottt
Created November 12, 2012 12:46
Show Gist options
  • Save smottt/4059213 to your computer and use it in GitHub Desktop.
Save smottt/4059213 to your computer and use it in GitHub Desktop.
CallbackValidator example
<?php
namespace Acme\DemoBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\CallbackValidator;
class MyType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addValidator(new CallbackValidator(function (FormInterface $form) {
if (!$form['foo']->getData()) {
$form['foo']->addError(new FormError('foo.error'));
}
}));
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment