I have this project with an entity A that holds a property b that should be a valid B or null.
B has itself a mandatory property.
How can I assert that b is valid or null? I tried using the Valid assertion, but null is not valid, so it complains about mandatoryItem being null.
<?php
// use statements
class A
{
/**
* @ORM\OneToOne(targetEntity="B")
* @Assert\Valid
*/
private b;
}
// meanwhile, in another file...
class B
{
/**
* @Assert\NotBlank
*/
private $mandatoryItem;
}
Before doing anything below make sure you have
Assertenabled. If thats already the cause the steps below might be helpful to you.If you use a form to submit the data and got a formType for that form, but the
Validconstraint is not working, you can do this:Remove the
Validassert.Inside the
ATypeclassbuildForm()block you add it like this:Also in the
ATypeclass add this function:'cascade_validation' => true,Does the same asValidbut then within the form.