Symfony checks if another field is a specific value
I am trying to write a confirmation for field (1), which is required if another field (3) is option "a" and another field (2) if 3 is "b". How should I do it?
EDIT: This is for Entity. I'll post a sample of what I'm trying.
/**
*@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;
I need $ 1 if $ 3 == 'a' and $ 2 if $ 3 == 'b' is required.
+3
source to share
2 answers
You can use check constraint: Expression
Example:
/**
* @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;
}
+5
source to share
You can try with callback constraint: http://symfony.com/doc/current/reference/constraints/Callback.html
0
source to share