我有一个席位分配到学院的实体,有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内置组件实现这一点。
答案 0 :(得分: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 callback doc
的链接