Disable backend validation for choice field in Symfony 2 Type

后端 未结 3 1015
悲哀的现实
悲哀的现实 2021-01-02 01:05

Is it possible to disable backend (server-side) validation for the specified field?

Wnen Im trying to send form with dynamicly loaded options I get error \"ERROR: T

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-02 01:34

    Add this inside buildForm method in your form type class so that you can validate an input field value rather a choice from a select field value;

    $builder->addEventListener(
        FormEvents::PRE_SUBMIT,
    
        function (FormEvent $event) {
            $form = $event->getForm();
    
            if ($form->has('field')) {
                $form->remove('field');
                $form->add(
                    'field', 
                    'text', 
                    ['required' => false]
                )
            }
        }
    );
    

提交回复
热议问题