8000 [Validator] Add a Length::$allowEmptyString option to reject empty strings by ogizanagi · Pull Request #31528 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@ogizanagi
Copy link
Contributor
@ogizanagi ogizanagi commented May 17, 2019
Q A
Branch? 4.4
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? yes
Tests pass? yes
Fixed tickets N/A
License MIT
Doc PR Todo (change the warning on top of https://symfony.com/doc/current/reference/constraints/Length.html)

which defaults to true in 4.4 but will trigger a deprecation if not set explicitly
in order to make the default false in 5.0.

While it could be solved now thanks to #29641 by using both @Length(min=1) & @NotBlank(allowNull=true) constraints,
as expressed in #27876 (comment) and following comments, the @Length(min=1) behavior doesn't match our expectations when reading it: it feels logical to invalidate empty strings, but it actually doesn't.
Hence the proposal of making the behavior of rejecting empty strings the default in 5.0.

In my opinion, the flag could even be removed later.

@ogizanagi
Copy link
Contributor Author

Status: Needs work

to only account when a min (or value, i.e min & max) value is set.

Copy link
Contributor Author
@ogizanagi ogizanagi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Status: Needs Review


public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$allowEmptyString = property_exists(Assert\Length::class, 'allowEmptyString') ? ['allowEmptyString' => true] : [];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to keep compatibility with lowest symfony/validator versions for this bridge, as the code isn't impacted, only the tests fixtures are.

Same for the Form component.

@ogizanagi ogizanagi changed the base branch from master to 4.4 May 28, 2019 16:38