Skip to content

[Validator] Allow empty string on LengthValidator #45993

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

Closed
wants to merge 1 commit into from

Conversation

neufman
Copy link

@neufman neufman commented Apr 11, 2022

Allow empty string to match documentation "As with most of the other constraints, null and empty strings are considered valid values".

Q A
Branch? 6.0
Bug fix? yes (?)
New feature? no
Deprecations? no
Tickets -
License MIT
Doc PR See note at the bottom of the "Basic usage" on LengthValidator documentation. To prevent empty string, Length constraint should be associated with NotBlank.

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 6.1 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@neufman neufman changed the title [Validator] Allow empty string [Validator] Allow empty string on LengthValidator Apr 11, 2022
@neufman neufman force-pushed the fix/LengthValidator branch from 9c07484 to ab67e21 Compare April 11, 2022 12:21
@nicolas-grekas
Copy link
Member

In 5.2, we deprecated the allowEmptyString, see #36818 by @xabbuh
I think the behavior on 6.0 is expected now, isn't it?

@neufman
Copy link
Author

neufman commented Apr 11, 2022

I just looked at the ticket but: there are about thirty validators that authorize an empty string? Why a different behavior for this one?

Allow empty string to match documentation "As with most of the other constraints, null and empty strings are considered valid values".
@neufman neufman force-pushed the fix/LengthValidator branch from 4afe14e to 07f30ec Compare April 11, 2022 15:28
@ogizanagi
Copy link
Contributor

ogizanagi commented Apr 11, 2022

I just looked at the ticket but: there are about thirty validators that authorize an empty string? Why a different behavior for this one?

This decision was made in #31528 because the semantic of Length(min: 1) wasn't fulfilled and the result unexpected to me since it allowed empty strings.
This is different from any other constraints, since the Length one specifically is about the string length. Other constraints are conveniently ignoring empty strings, because they cannot perform any validation on it.

Also, the DX for a nullable string property requiring at least 1 char (hence reject empty strings) — which is a common use-case for clean data types — was bad, requiring to write:

#[Length(min: 1)] <- was actually completely useless and unintuitive, since it was allowing empty strings despite min: 1
#[NotBlank(allowNull: true)] <- was too indirect to express empty strings are not allowed but the property is nullable

If you really wish to allow empty strings, you can use:

#[AtLeastOneOf([
    new Length(0),
    new Length(min: 6),
])]

which would be explicit about this use-case.

@nicolas-grekas
Copy link
Member

Closing as explained. Thanks for asking and the answers :)

javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Apr 29, 2022
…rjohnson)

This PR was merged into the 6.0 branch.

Discussion
----------

[Validator] Update Length Validator Empty String Docs

Empty strings are no longer valid if a min is passed, null values still
validate.

Changed in 6.0 along with dropping the deprecated `allowEmptyString` symfony/symfony#36818 option. There is some discussion that this is the intended behavior in symfony/symfony#45993

Commits
-------

9fa967e Update Length Validator Empty String Docs
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