Symfony News

New in Symfony 4.3: Improved the NotBlank Validator

Kévin Dunglas

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;
}

Be trained by Symfony experts - 2019-02-7 Clichy - 2019-02-11 Clichy - 2019-02-11 Clichy


About us

What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...

Find us on Twitter

Find us on Facebook

ADS