Search code examples
validationsymfonyserver-side

Override / remove server-side validation in Symfony2.5


Say I have the following form builder buildForm method:

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{

$builder
    ->add(
        'travelTime',
        'datetime',
        array(
            'label' => 'Time',
            'date_widget' => 'single_text',
            'time_widget' => 'single_text',
            'invalid_message' => 'Please enter a valid date and time',
        )
    )
    ->add(
        'acmeEntity',
        'entity',
        array(
            'label' => 'Acme Entity:',
            'class' => 'AcmeBundle:MyEntity',
            'expanded' => false,
            'multiple' => false,
        )
    )
}

How can I override (or remove) validation for the 'acmeEntity' form field (and only that field) so that if I call:

$form->handleRequest($request);
$form->isValid();

in a Controller, then acmeEntity will not be included in the validation that determines whether $form->isValid() returns true? I've tried adding constraints => false, to the field options, but I'm receiving this error message:

Notice: Trying to get property of non-object in /var/www/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php line 67

Does anyone know the proper way to disable server-side validation for a Symfony form field?

EDIT:

Note that I am not looking for how to disable validation completely. This can be done by adding:

    // Disable form validation
    $builder->addEventListener(FormEvents::POST_SUBMIT, function ($event) {
        $event->stopPropagation();
    }, 900); // Always set a higher priority than ValidationListener

to the bottom of a form builder.

Rather, I want to know how to completely disable validation for a single form field. Thanks everyone and good hunting!


Solution

  • You can define a custom form type for your entity and use the 'validation_groups' => false. This should disable the validation only for this field.

    The custom form type may look like that:

    // .../Your/OwnBundle/Form/Type/AcmeEntityType.php
    namespace Acme\DemoBundle\Form\Type;
    
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
    
    class AcmeEntityType extends AbstractType
    {
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'validation_groups' => false
            ));
        }
    
        public function getParent()
        {
            return 'entity';
        }
    
        public function getName()
        {
            return 'acme_entity';
        }
    }
    

    You can then use:

    $builder
        ->add(
            'travelTime',
            'datetime',
            array(
                'label' => 'Time',
                'date_widget' => 'single_text',
                'time_widget' => 'single_text',
                'invalid_message' => 'Please enter a valid date and time',
            )
        )
        ->add(
            'acmeEntity',
            'acme_entity',
            array(
                'label' => 'Acme Entity:',
                'class' => 'AcmeBundle:MyEntity',
                'expanded' => false,
                'multiple' => false,
            )
        )
    }