8000 feature #31528 [Validator] Add a Length::$allowEmptyString option to … · symfony/form@ce1f753 · GitHub
[go: up one dir, main page]

Skip to content

Commit ce1f753

Browse files
committed
feature #31528 [Validator] Add a Length::$allowEmptyString option to 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 symfony/symfony#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 ------- e113e7f812 [Validator] Add a Length::$allowEmptyString option to reject empty strings
2 parents 44c74e2 + 3a8323f commit ce1f753

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

Tests/Extension/Validator/Type/FormTypeValidatorExtensionTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,15 @@ public function testValidConstraint()
5757

5858
public function testGroupSequenceWithConstraintsOption()
5959
{
60+
$allowEmptyString = property_exists(Length::class, 'allowEmptyString') ? ['allowEmptyString' => true] : [];
61+
6062
$form = Forms::createFormFactoryBuilder()
6163
->addExtension(new ValidatorExtension(Validation::createValidator()))
6264
->getFormFactory()
6365
->create(FormTypeTest::TESTED_TYPE, null, (['validation_groups' => new GroupSequence(['First', 'Second'])]))
6466
->add('field', TextTypeTest::TESTED_TYPE, [
6567
'constraints' => [
66-
new Length(['min' => 10, 'groups' => ['First']]),
68+
new Length(['min' => 10, 'groups' => ['First']] + $allowEmptyString),
6769
new Email(['groups' => ['Second']]),
6870
],
6971
])

Tests/Extension/Validator/ValidatorTypeGuesserTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ protected function setUp()
6161

6262
public function guessRequiredProvider()
6363
{
64+
$allowEmptyString = property_exists(Length::class, 'allowEmptyString') ? ['allowEmptyString' => true] : [];
65+
6466
return [
6567
[new NotNull(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)],
6668
[new NotBlank(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)],
6769
[new IsTrue(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)],
68-
[new Length(10), new ValueGuess(false, Guess::LOW_CONFIDENCE)],
70+
[new Length(['min' => 10, 'max' => 10] + $allowEmptyString), new ValueGuess(false, Guess::LOW_CONFIDENCE)],
6971
[new Range(['min' => 1, 'max' => 20]), new ValueGuess(false, Guess::LOW_CONFIDENCE)],
7072
];
7173
}
@@ -101,7 +103,9 @@ public function testGuessMaxLengthForConstraintWithMaxValue()
101103

102104
public function testGuessMaxLengthForConstraintWithMinValue()
103105
{
104-
$constraint = new Length(['min' => '2']);
106+
$allowEmptyString = property_exists(Length::class, 'allowEmptyString') ? ['allowEmptyString' => true] : [];
107+
108+
$constraint = new Length(['min' => '2'] + $allowEmptyString);
105109

106110
$result = $this->guesser->guessMaxLengthForConstraint($constraint);
107111
$this->assertNull($result);

0 commit comments

Comments
 (0)
0