如果另一个字段(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.
答案 0 :(得分:9)
您可以使用验证约束:Expression
示例:
/**
* @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;
}
答案 1 :(得分:1)