我试图写一个验证,如果另一个字段(3)是选项'a',则需要一个字段(1),如果另一个3(b),则是另一个字段(2)。我将如何去做?
编辑:这是一个实体。我将发布我正在尝试的示例。
/**
*@Assert\Collection(
*fields = { aName = @Assert\NotBlank(),
* aAmount = @Assert\NotBlank() }
*/
protected $1;
/**
*@Assert\Collection(
*fields = { bName = @Assert\NotBlank(),
* bAmount = @Assert\NotBlank() }
*/
protected $2;
/**
*@Assert\NotBlank()
*/
protected $3;
如果$ 3 =='a',则需要$ 1;如果$ 3 =='b',则需要$ 2。
您可以使用验证约束:表达式
例:
/**
* @Assert\Expression(
* "not (this.getThird() == 'a' and this.getFirst() == null)",
* message="If third = 'a', first should be not null"
* )
*/
protected $first;
/**
* @Assert\Expression(
* "not (this.getThird() == 'b' and this.getSecond() == null)",
* message="If third = 'b', second should be not null"
* )
*/
protected $second;
protected $third;
public function getFirst()
{
return $this->first;
}
public function getSecond()
{
return $this->second;
}
public function getThird()
{
return $this->third;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句