symfony-forms Example of Symfony Form Events onPostSubmit Event


Example

This is an Education From in Symfony to take user education details. We wanted to apply validation on 2 fields, education end date and is currently studying.

On Post Submit Event, We will check two things
1 -  if the user checks the checkbox of is_currently studying then end date should be empty 
2 -  On the other side, we have to make sure, if end date is not empty, then is currently studying check box should be unchecked.

 



    
    
    /**
     * Class QualificationFormType
     * @package UsersBundle\Form\Type
     */
    class QualificationFormType extends AbstractType
    {

        public function buildForm(FormBuilderInterface $builder, array $options)
        {
    
            $builder
                ->add('title')
                ->add('institution')
             ->add('startDate', 'date', [
                'label' => 'Start Date',
                'widget' => 'single_text',
                'format' => 'dd-MM-yyyy',
                'required' => true,
                'constraints' =>  [
                    new Assert\NotBlank(),
                    new Assert\LessThan("today"),
                ],
                'trim'    => true,
                'attr' => [
                    'maxlength' => '12',
                    'minlength' => '10',
                    'placeholder' => 'when did you start this education?',
                    'class' => 'form-control input-inline datepicker datePicker',
                    'data-provide' => 'datepicker',
                    'data-date-format' => 'dd-mm-yyyy',
                    'minViewMode' => '1'
                ],
                'label_attr' => [
                    'class' => 'control-label',
                ],
            ])
            ->add('endDate', 'date', [
                'label' => 'End Date',
                'widget' => 'single_text',
                'format' => 'dd-MM-yyyy',
                'required' => false,
                'attr' => [
                    'placeholder' => 'when did you end this education?',
                    'class' => 'form-control input-inline datepicker datePicker',
                    'data-provide' => 'datepicker',
                    'data-date-format' => 'dd-mm-yyyy',
                    'minViewMode' => '1'
                ],
                'label_attr' => [
                    'class' => 'control-label',
                ],
            ])
            ->add('current', null, [
                'label' => ucfirst('I am currently studying'),
                'label_attr' => [
                    'class' => 'control-label',
                ],
            ])
            ->add('save', 'submit')
                
            ;
    
    
            $builder->addEventListener(FormEvents::POST_SUBMIT, [$this, 'onPostSubmit']);
        }
    

        function onPostSubmit(FormEvent $event) {
            $form = $event->getForm();
            $endDate = $form->get('endDate')->getData();
            $current = $form->get('current')->getData();
            If(!$current){
               if ($startDate>$endDate ) {
                   $form['startDate']->addError(new FormError("Start Date cannot be greater than end date..."));
               }
            }
        }
   
    }