For anyone that may still care, whilst a callback validator is perfectly acceptable for simpler dependencies an expression validator is shorter to implement.
For example if you've got a field called "Want a drink?" then if yes (true) "How many?" (integer), you could simplify this with:
/**
* @var bool|null $wantDrink
* @ORM\Column(name="want_drink", type="boolean", nullable=true)
* @Assert\NotNull()
* @Assert\Type(type="boolean")
*/
private $wantDrink;
/**
* @var int|null $howManyDrinks
* @ORM\Column(name="how_many_drinks", type="integer", nullable=true)
* @Assert\Type(type="int")
* @Assert\Expression(
* "true !== this.getWantDrink() or (null !== this.getHowManyDrinks() and this.getHowManyDrinks() >= 1)",
* message="This value should not be null."
* )
*/
private $howManyDrinks;
You write the expression in PASS context, so the above is saying that $howManyDrinks
must be a non-null integer at least 1 if $wantDrink
is true, otherwise we don't care about $howManyDrinks
. Make use of the expression syntax, which is sufficient for a broad range of scenarios.
Another scenario I find myself frequently using a expression validator are when I've got two fields to the effect of "date start" and "date end", so that they can each ensure that they are the right way around (so that the start date is before or equal to the end date and the end date is greater or equal to the start date).