0

我有一个学院的席位分配实体,有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 内置组件来实现这一点。

4

1 回答 1

4

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 回调文档的链接

于 2015-11-19T09:53:37.960 回答