psi*_*ion 4 php validation symfony
如果另一个字段(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;
Run Code Online (Sandbox Code Playgroud)
如果$ 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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3431 次 |
| 最近记录: |