0

我有一个相当大的表单要构建,并且在选择特定选项时需要验证表单的某些部分,并且只有在选择该选项时才需要验证。如果我不需要它们,如何确保跳​​过这些验证?

4

1 回答 1

0

做一个“巨大”的验证方法,然后进入验证本身,检查“选择的选项”:如果有,检查“子条件”

就像是

use Symfony\Component\Validator\Constraints as Assert; 
/**
 *
 * @Assert\Callback(methods={"isValid"})
 */
class ObjectRelatedToYourForm
{
[...]
  public function isValid(ExecutionContext $context)
  {
    if ($this->optionOneSelected) {
      //perform controls and add violation in case of failure
    }
    if ($this->optionTwoSelected) {
      //perform controls and add violation in case of failure
    }
  }
}
于 2014-07-02T15:09:16.880 回答