-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Add a Length::$allowEmptyString option to reject empty strings #31528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Validator] Add a Length::$allowEmptyString option to reject empty strings #31528
Conversation
Status: Needs work to only account when a |
There was a problem hiding this 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] : []; |
There was a problem hiding this comment.
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.
Rebased & comments fixed Status: Needs Review |
…rings 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.
Thank you @ogizanagi. |
…reject empty strings (ogizanagi) This PR was merged into the 4.4 branch. Discussion ---------- [Validator] Add a Length::$allowEmptyString option to reject empty strings | Q | A | ------------- | --- | Branch? | 4.4 <!-- see below --> | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | yes <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | N/A <!-- #-prefixed issue number(s), if any --> | 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. Commits ------- e113e7f [Validator] Add a Length::$allowEmptyString option to reject empty strings
…o false & make it optional (ogizanagi) This PR was merged into the 5.0-dev branch. Discussion ---------- [Validator] Change Length::$allowEmptyString default to false & make it optional | Q | A | ------------- | --- | Branch? | master <!-- see below --> | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | BC breaks? | yes <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | should pass after #32372 <!-- please add some, will be required by reviewers --> | Fixed tickets | #31528 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | ~TODO: change default value and mention it's optional~ Done. Thx @javiereguiluz Commits ------- 26b804e [Validator] Change Length::$allowEmptyString default to false & make it optional
…NotBlank contraint is defined (nicolas-grekas) This PR was merged into the 4.4 branch. Discussion ---------- [Validator] Set Length::$allowEmptyString to false when a NotBlank contraint is defined | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Since #31528, we are told to do this kind of changes to our entities: ```diff /** * @Assert\NotBlank() - * @Assert\Length(min="10") + * @Assert\Length(min="10", allowEmptyString=false) */ public $description; ``` But the `NotBlank` already says it - this is just boilerplate. More critically, this also means we cannot write annotations that are compatible with 3.4, 4.4 and 5.0 at the same time, making FC/BC hard. By setting `Length::$allowEmptyString` to `false` when a `NotBlank` contraint is defined, we fix both issues. /cc @ogizanagi Commits ------- 840f7e7 [Validator] Set Length::$allowEmptyString to false when a NotBlank contraint is defined
which defaults to
true
in 4.4 but will trigger a deprecation if not set explicitlyin 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.