根据表单的其他部分验证Symfony表单的各个部分

时间:2014-07-02 14:31:38

标签: php symfony symfony-forms symfony-2.5

我有一个相当大的表单来构建,并且在选择特定选项时,只有选择了该选项时,才需要验证表单的certian部分。如果我不需要,我怎样才能确保跳过这些验证呢?

1 个答案:

答案 0 :(得分: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
    }
  }
}
相关问题