Skip to content

Instantly share code, notes, and snippets.

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