所以这是场景:我有一个单选按钮组.基于它们的价值,我应该或不应该验证其他三个字段(它们是空白的,它们是否包含数字等).
我可以以某种方式将所有这些值传递给约束,并在那里进行比较吗?
一般来说,这种情况下的最佳做法是什么?
我建议你使用
callback validator.
例如,在您的实体类中:
- <?PHP
- use Symfony\Component\Validator\Constraints as Assert;
- /**
- * @Assert\Callback(methods={"myValidation"})
- */
- class Setting {
- public function myValidation(ExecutionContextInterface $context)
- {
- if (
- $this->getRadioSelection() == '1' // RAdio SELECT EXAMPLE
- &&
- ( // CHECK OTHER PARAMS
- $this->getFiled1() == null
- )
- )
- {
- $context->addViolation('mandatory params');
- }
- // put some other validation rule here
- }
- }
让我知道您需要更多信息.
希望这可以帮助.