【问题标题】:Symfony2 Form make one field required true between 2 fieldsSymfony2 表单使一个字段在 2 个字段之间为真
【发布时间】:2015-11-19 09:29:58
【问题描述】:

我有一个学院的席位分配实体,有3个字段,总席位,男性席位,女性席位。场景是所有座位都是男性或女性,或者可以在男性和女性之间分配。

最后,我的问题是:我想检查用户是否至少选择了所需的两个字段之一(maleSeat 和 femaleSeat)

 ->add('maxSeats', 'integer', array(
                'label' => ucwords('max Seats'),
                'required' => true, // Choose if it's required or not
                'constraints' => $constraints['maxSeats'],
                'invalid_message' => 'You entered an invalid value, it should include %num%',
                'sonata_help' => 'Here some help text!!!',
                'attr' => array(
                    'class' => 'form-control',
                    'help' => 'My Help Message',
                ),
                'label_attr' => array(
                    'class' => 'control-label'
                )
            ))
            ->add('maleSeats', 'integer', array(
                'label' => ucwords('male Seats'),
                'required' => true, // Choose if it's required or not
                'constraints' => $constraints['maleSeats'],
                'invalid_message' => 'You entered an invalid value, it should include %num%',
                'attr' => array(
                    'class' => 'form-control'
                ),
                'label_attr' => array(
                    'class' => 'control-label'
                )
            ))
            ->add('femaleSeats', 'integer', array(
                'label' => ucwords('female Seats'),
                'required' => true, // Choose if it's required or not
                'constraints' => $constraints['femaleSeats'],
                'invalid_message' => 'You entered an invalid value, it should include %num%',
                'attr' => array(
                    'class' => 'form-control'
                ),
                'label_attr' => array(
                    'class' => 'control-label'
                )
            ))

$constraints = array(
            'maxSeats' => array(
                new Assert\NotBlank(),
                new Assert\Range(array(
                    'min'        => 1,
                    'minMessage' => 'Min Seats is 1'
                )),
                new Assert\Regex(array(
                    'pattern' => '/[0-9]/',
                    'message' => 'Total Seats Should Only be a Positive Number',
                )),
            ),
            'maleSeats' => array(
                new Assert\NotBlank(),
                new Assert\Range(array(
                    'min'        => 1,
                    'minMessage' => 'Min Seats is 1'
                )),
                new Assert\Length(array(
                    'min' => 1,
                    'max' => 30,
                    'minMessage' => 'The min Seats must be more than 6 and less than 1 characters long',
                    'maxMessage' => 'The min Seats must be more than 6 and less than 30 characters long',
                )),
                new Assert\Regex(array(
                    'pattern' => '/[0-9]/',
                    'message' => 'Total Seats Should Only be a Positive Number',
                )),
            ),
            'femaleSeats' => array(
                new Assert\NotBlank(),
                new Assert\Range(array(
                    'min'        => 1,
                    'minMessage' => 'Min Seats is 1'
                )),
                new Assert\Length(array(
                    'min' => 1,
                    'max' => 30,
                    'minMessage' => 'The min Seats must be more than 6 and less than 1 characters long',
                    'maxMessage' => 'The min Seats must be more than 6 and less than 30 characters long',
                )),
                new Assert\Regex(array(
                    'pattern' => '/[0-9]/',
                    'message' => 'Total Seats Should Only be a Positive Number',
                )),
            ),

        );

我知道它可以通过 jquery 的东西来处理,但我不想在这里涉及 jquery 和其他东西,我如何使用 symfony 内置组件来实现这一点。

【问题讨论】:

    标签: php validation symfony symfony-forms symfony-validator


    【解决方案1】:

    您可以通过在 Seat 实体中使用回调来做到这一点:

    use Symfony\Component\Validator\Constraints as Assert;
    use Symfony\Component\Validator\Context\ExecutionContextInterface;
    // if you're using the older 2.4 validation API, you'll need this instead
    // use Symfony\Component\Validator\ExecutionContextInterface;
    
    class Seat
    {
        /**
         * @Assert\Callback
         */
        public function validate(ExecutionContextInterface $context)
        {
            if ($this->getFemaleSeats() == 0 && $this->getMaleSeats == 0) {
                // If you're using the new 2.5 validation API (you probably are!)
                $context->buildViolation('Female and male seat could not be empty at the same time')
                        ->atPath('maleSeats')
                        ->addViolation();
                // If you're using the old 2.4 validation API
                /*
                $context->addViolationAt(
                    'maleSeats',
                    'Female and male seat could not be empty at the same time'
                );
                */
            }
    
    }
    

    如果你想了解更多配置信息,我把symfony callback doc的链接发给你

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      相关资源
      最近更新 更多