Last Updated: February 25, 2016
·
2.656K
· 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

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside