根据Symfony表单的其他部分验证该表单的某些部分


Validate parts of a Symfony form based on other parts of the form

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

创建一个"huge"验证方法,然后进入验证本身,检查"selected options":如果有,检查"sub-condition"

之类的
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
    }
  }
}