Skip to content

[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

Merged
merged 1 commit into from
Jul 3, 2019
Merged

[Validator] Add a Length::$allowEmptyString option to reject empty strings #31528

merged 1 commit into from
Jul 3, 2019

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
@ogizanagi
Copy link
Contributor Author

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.
@ogizanagi ogizanagi requested a review from fabpot July 1, 2019 08:00
@fabpot
Copy link
Member

fabpot commented Jul 3, 2019

Thank you @ogizanagi.

@fabpot fabpot merged commit e113e7f into symfony:4.4 Jul 3, 2019
fabpot added a commit that referenced this pull request Jul 3, 2019
…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
@ogizanagi ogizanagi deleted the feat/validator-length-reject-empty-string branch July 3, 2019 15:08
fabpot added a commit that referenced this pull request Jul 5, 2019
…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
nicolas-grekas added a commit that referenced this pull request Oct 23, 2019
…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
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.4 Oct 27, 2019
This was referenced Nov 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants