onPostSubmit 事件

这是一个来自 Symfony 的 Education From 用户教育详细信息。我们希望在 2 个领域,教育结束日期进行验证,目前正在学习。

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..."));
               }
            }
        }
   
    }