Symfony2 form validation based on two fields

前端 未结 5 2064
醉梦人生
醉梦人生 2020-12-05 05:04

I am currently developing a Website in which user may buy gift cards. I am using a three step form using the CraueFormFlow bundle and everything is concerning the steps. I a

5条回答
  •  情书的邮戳
    2020-12-05 05:54

    When you don't have a data class attached to your form you can implement dependent constraints in forms like this:

        $startRangeCallback = function ($object, ExecutionContextInterface $context) use ($form)
        {
            $data = $form->getData();
            $rangeEnd = $data['range_end'];
            if($object && $rangeEnd){
                if ($object->getTimestamp() > $rangeEnd->getTimestamp()) {
                    $context->addViolation('Start date should be before end date!', array(), null);
                }
            }
    
        };
    
        $form->add('range_start', 'bootstrap_datepicker', array(
                'format' => 'dd-MM-yyyy',
                'required' => false,
                'attr' => array('class' => "col-xs-2"),
                'calendar_weeks' => true,
                'clear_btn' => true,
                'constraints' => array(
                    new Callback(array($startRangeCallback)),
                )
            )
        );
    
        $form->add('range_end', 'bootstrap_datepicker', array(
                'format' => 'dd-MM-yyyy',
                'required' => false,
                'attr' => array('class' => "col-xs-2"),
                'calendar_weeks' => true,
                'clear_btn' => true,
    
            )
        );
    

提交回复
热议问题