I would like to apply validators on a object properties only when value is not empty ie.
Now standard symfony behavior:
class Entity
{
/**
* @ORM\Column(type="string", nullable=true)
* @Assert\Email()
*/
protected $email;
(...)
}
that object will not pass validation if an email is null, or empty string, is there a way to tell validator to assert as a valid, an empty value, and validate only if field has data?
PS I know that I can write callback validator, but writting callback for every field just to have “allowEmpty” feature isn’t so nice.
You must explicitly set
'required' => falsein yourFormBuilderclass for all optional fields. Here’s a paragraph describing field type options.Edit. Getting quite a few downvotes. By default all validators treat
nullvalues as valid, exceptNotNullandNotEmpty. Neither of the two was used in the question. The question is implicitly about how to turn off the client-siderequiredattribute that is turned on by default.