Skip to content

Latest commit

 

History

History
29 lines (24 loc) · 1.25 KB

File metadata and controls

29 lines (24 loc) · 1.25 KB

3.16 The NotBlank constraint

A textarea that contains nothing but spaces is blank, because the trim option of the field makes Symfony strip them off the submitted value before any constraint sees it. The bundle trims the same way in the browser, so nothing has to be configured for NotBlank to report such a field.

A field declared with 'trim' => false keeps its spaces, and so a value made of spaces only is not blank — on the server as much as in the browser. The Symfony way to trim for the constraint alone is its normalizer option:

$builder->add('description', TextareaType::class, [
    'trim' => false,
    'constraints' => [
        new Assert\NotBlank(normalizer: 'trim'),
    ],
]);

trim is the only normalizer the browser can apply. It is the name of a PHP function, and the bundle recognizes it as such; every other callable, including trim(...) and a closure of your own, is a value that cannot be exported, so the constraint is checked against the value as it was submitted. Where that matters, validate the field on the server as well.

The allowNull option is honoured too. A field of a form that is not trimmed submits an empty string rather than null, so it takes a transformer, or a getter, for the value to be null at all.