New in Symfony 4.3: Improved the NotBlank Validator
February 1, 2019 • Published by Javier Eguiluz
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
Contributed by
Kévin Dunglas
in #29641.
The NotBlank constraint of the Validator component checks that a value is
not false
, null
, an empty array or an empty string. Most of the other
constraints ignore null
values, but NotBlank
validates them. This causes
issues in scenarios such as APIs called from front-end code, where is easier
to include null
fields instead of removing those fields when making requests.
In Symfony 4.3 we've improved the NotBlank
constraint adding a new
allowNull
option to it. By default this option is false
, to keep the
current behavior. If you set it to true
, then null
values will be
considered valid instead of triggering a constraint violation:
1 2 3 4 5 6 7 8 9 10 11
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class SomeEntity
{
/**
* @Assert\NotBlank(allowNull = true)
*/
protected $someProperty;
}
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
Then you'll have to process (guess) this in the serializer where you'll have to inject the RequestStack to have some context. It is a thousand times easier and less error prone to handle this in the frontend, even by means of an interceptor.
Just my two cents but I fail to see how this is useful.