I have a form to create a new entity. That entity has a collection of other entities that are also entered in that form.
I want to use the validation options of the
I use this:
use Symfony\Component\Validator\ExecutionContextInterface;
class Person
{
...
/**
* @ORM\OneToMany(targetEntity="Address", mappedBy="owner", cascade={"persist", "detach"})
*/
protected $addressList;
....
/**
* @Assert\Callback
*/
public function validate(ExecutionContextInterface $context)
{
if (!$this->getAddressList()->count()) {
$context->addViolationAt(
'addressList',
'You must add at least one address',
array(),
null
);
}
}
}
http://symfony.com/doc/current/reference/constraints/Callback.html