Last Updated: February 25, 2016
·
2.439K
· limonazzo

Symfony2 custom field & validator

Uses:

use Symfony\Component\Form\CallbackValidator;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormError; 

... some code

$builder ->add('custom_field', 'text', array(
                'mapped' => false,
            ));

$callback = function(FormInterface $form) {
                if ($form['custom_field']->getData() == 'something' )
                {
                    $form->addError(new FormError('Oops ! Error Message'));
                }
            };

$builder->addValidator(new CallbackValidator($callback));

some code...

symfony v. 2.1