Skip to content

Commit e00017e

Browse files
committed
feature symfony#36818 [Validator] deprecate the "allowEmptyString" option (xabbuh)
This PR was merged into the 5.2-dev branch. Discussion ---------- [Validator] deprecate the "allowEmptyString" option | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | Deprecations? | yes | Tickets | Fix symfony#36812 | License | MIT | Doc PR | Commits ------- ee169d5 deprecate the "allowEmptyString" option
2 parents 7950851 + ee169d5 commit e00017e

File tree

12 files changed

+125
-16
lines changed

12 files changed

+125
-16
lines changed

UPGRADE-5.2.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
UPGRADE FROM 5.1 to 5.2
2+
=======================
3+
4+
Validator
5+
---------
6+
7+
* Deprecated the `allowEmptyString` option of the `Length` constraint.
8+
9+
Before:
10+
11+
```php
12+
use Symfony\Component\Validator\Constraints as Assert;
13+
14+
/**
15+
* @Assert\Length(min=5, allowEmptyString=true)
16+
*/
17+
```
18+
19+
After:
20+
21+
```php
22+
use Symfony\Component\Validator\Constraints as Assert;
23+
24+
/**
25+
* @Assert\AtLeastOneOf({
26+
* @Assert\Blank(),
27+
* @Assert\Length(min=5)
28+
* })
29+
*/
30+
```

UPGRADE-6.0.md

+28
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,34 @@ Security
115115
* Removed `DefaultLogoutSuccessHandler` in favor of `DefaultLogoutListener`.
116116
* Added a `logout(Request $request, Response $response, TokenInterface $token)` method to the `RememberMeServicesInterface`.
117117

118+
Validator
119+
---------
120+
121+
* Removed the `allowEmptyString` option from the `Length` constraint.
122+
123+
Before:
124+
125+
```php
126+
use Symfony\Component\Validator\Constraints as Assert;
127+
128+
/**
129+
* @Assert\Length(min=5, allowEmptyString=true)
130+
*/
131+
```
132+
133+
After:
134+
135+
```php
136+
use Symfony\Component\Validator\Constraints as Assert;
137+
138+
/**
139+
* @Assert\AtLeastOneOf({
140+
* @Assert\Blank(),
141+
* @Assert\Length(min=5)
142+
* })
143+
*/
144+
```
145+
118146
Yaml
119147
----
120148

src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoctrineLoaderEntity.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ class DoctrineLoaderEntity extends DoctrineLoaderParentEntity
3636

3737
/**
3838
* @ORM\Column(length=20)
39-
* @Assert\Length(min=5, allowEmptyString=true)
39+
* @Assert\Length(min=5)
4040
*/
4141
public $mergedMaxLength;
4242

4343
/**
4444
* @ORM\Column(length=20)
45-
* @Assert\Length(min=1, max=10, allowEmptyString=true)
45+
* @Assert\Length(min=1, max=10)
4646
*/
4747
public $alreadyMappedMaxLength;
4848

src/Symfony/Bridge/Doctrine/Tests/Resources/validator/BaseUser.xml

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
<option name="min">2</option>
1414
<option name="max">120</option>
1515
<option name="groups">Registration</option>
16-
<option name="allowEmptyString">true</option>
1716
</constraint>
1817
</property>
1918
</class>

src/Symfony/Component/Form/Tests/Extension/Validator/Type/FormTypeValidatorExtensionTest.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function testGroupSequenceWithConstraintsOption()
6767
->create(FormTypeTest::TESTED_TYPE, null, (['validation_groups' => new GroupSequence(['First', 'Second'])]))
6868
->add('field', TextTypeTest::TESTED_TYPE, [
6969
'constraints' => [
70-
new Length(['min' => 10, 'allowEmptyString' => true, 'groups' => ['First']]),
70+
new Length(['min' => 10, 'groups' => ['First']]),
7171
new NotBlank(['groups' => ['Second']]),
7272
],
7373
])
@@ -83,8 +83,6 @@ public function testGroupSequenceWithConstraintsOption()
8383

8484
public function testManyFieldsGroupSequenceWithConstraintsOption()
8585
{
86-
$allowEmptyString = property_exists(Length::class, 'allowEmptyString') ? ['allowEmptyString' => true] : [];
87-
8886
$formMetadata = new ClassMetadata(Form::class);
8987
$authorMetadata = (new ClassMetadata(Author::class))
9088
->addPropertyConstraint('firstName', new NotBlank(['groups' => 'Second']))
@@ -116,7 +114,7 @@ public function testManyFieldsGroupSequenceWithConstraintsOption()
116114
->add('firstName', TextTypeTest::TESTED_TYPE)
117115
->add('lastName', TextTypeTest::TESTED_TYPE, [
118116
'constraints' => [
119-
new Length(['min' => 10, 'groups' => ['First']] + $allowEmptyString),
117+
new Length(['min' => 10, 'groups' => ['First']]),
120118
],
121119
])
122120
->add('australian', TextTypeTest::TESTED_TYPE, [

src/Symfony/Component/Form/Tests/Extension/Validator/ValidatorExtensionTest.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,11 @@ public function testFieldConstraintsInvalidateFormIfFieldIsSubmitted()
9494

9595
public function testFieldsValidateInSequence()
9696
{
97-
$allowEmptyString = property_exists(Length::class, 'allowEmptyString') ? ['allowEmptyString' => true] : [];
98-
9997
$form = $this->createForm(FormType::class, null, [
10098
'validation_groups' => new GroupSequence(['group1', 'group2']),
10199
])
102100
->add('foo', TextType::class, [
103-
'constraints' => [new Length(['min' => 10, 'groups' => ['group1']] + $allowEmptyString)],
101+
'constraints' => [new Length(['min' => 10, 'groups' => ['group1']])],
104102
])
105103
->add('bar', TextType::class, [
106104
'constraints' => [new NotBlank(['groups' => ['group2']])],
@@ -117,16 +115,14 @@ public function testFieldsValidateInSequence()
117115

118116
public function testFieldsValidateInSequenceWithNestedGroupsArray()
119117
{
120-
$allowEmptyString = property_exists(Length::class, 'allowEmptyString') ? ['allowEmptyString' => true] : [];
121-
122118
$form = $this->createForm(FormType::class, null, [
123119
'validation_groups' => new GroupSequence([['group1', 'group2'], 'group3']),
124120
])
125121
->add('foo', TextType::class, [
126-
'constraints' => [new Length(['min' => 10, 'groups' => ['group1']] + $allowEmptyString)],
122+
'constraints' => [new Length(['min' => 10, 'groups' => ['group1']])],
127123
])
128124
->add('bar', TextType::class, [
129-
'constraints' => [new Length(['min' => 10, 'groups' => ['group2']] + $allowEmptyString)],
125+
'constraints' => [new Length(['min' => 10, 'groups' => ['group2']])],
130126
])
131127
->add('baz', TextType::class, [
132128
'constraints' => [new NotBlank(['groups' => ['group3']])],

src/Symfony/Component/Form/Tests/Extension/Validator/ValidatorTypeGuesserTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function guessRequiredProvider()
6666
[new NotNull(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)],
6767
[new NotBlank(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)],
6868
[new IsTrue(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)],
69-
[new Length(['min' => 10, 'max' => 10, 'allowEmptyString' => true]), new ValueGuess(false, Guess::LOW_CONFIDENCE)],
69+
[new Length(['min' => 10, 'max' => 10]), new ValueGuess(false, Guess::LOW_CONFIDENCE)],
7070
[new Range(['min' => 1, 'max' => 20]), new ValueGuess(false, Guess::LOW_CONFIDENCE)],
7171
];
7272
}
@@ -102,7 +102,7 @@ public function testGuessMaxLengthForConstraintWithMaxValue()
102102

103103
public function testGuessMaxLengthForConstraintWithMinValue()
104104
{
105-
$constraint = new Length(['min' => '2', 'allowEmptyString' => true]);
105+
$constraint = new Length(['min' => '2']);
106106

107107
$result = $this->guesser->guessMaxLengthForConstraint($constraint);
108108
$this->assertNull($result);

src/Symfony/Component/Validator/CHANGELOG.md

+28
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
11
CHANGELOG
22
=========
33

4+
5.2.0
5+
-----
6+
7+
* deprecated the `allowEmptyString` option of the `Length` constraint
8+
9+
Before:
10+
11+
```php
12+
use Symfony\Component\Validator\Constraints as Assert;
13+
14+
/**
15+
* @Assert\Length(min=5, allowEmptyString=true)
16+
*/
17+
```
18+
19+
After:
20+
21+
```php
22+
use Symfony\Component\Validator\Constraints as Assert;
23+
24+
/**
25+
* @Assert\AtLeastOneOf({
26+
* @Assert\Blank(),
27+
* @Assert\Length(min=5)
28+
* })
29+
*/
30+
```
31+
432
5.1.0
533
-----
634

src/Symfony/Component/Validator/Constraints/Length.php

+4
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,9 @@ public function __construct($options = null)
6464
if (null !== $this->normalizer && !\is_callable($this->normalizer)) {
6565
throw new InvalidArgumentException(sprintf('The "normalizer" option must be a valid callable ("%s" given).', get_debug_type($this->normalizer)));
6666
}
67+
68+
if (isset($options['allowEmptyString'])) {
69+
trigger_deprecation('symfony/validator', '5.2', sprintf('The "allowEmptyString" option of the "%s" constraint is deprecated.', self::class));
70+
}
6771
}
6872
}

src/Symfony/Component/Validator/Tests/Constraints/LengthTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
namespace Symfony\Component\Validator\Tests\Constraints;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\Validator\Constraints\Length;
1617

1718
/**
1819
* @author Renan Taranto <[email protected]>
1920
*/
2021
class LengthTest extends TestCase
2122
{
23+
use ExpectDeprecationTrait;
24+
2225
public function testNormalizerCanBeSet()
2326
{
2427
$length = new Length(['min' => 0, 'max' => 10, 'normalizer' => 'trim']);
@@ -39,4 +42,23 @@ public function testInvalidNormalizerObjectThrowsException()
3942
$this->expectExceptionMessage('The "normalizer" option must be a valid callable ("stdClass" given).');
4043
new Length(['min' => 0, 'max' => 10, 'normalizer' => new \stdClass()]);
4144
}
45+
46+
/**
47+
* @group legacy
48+
* @dataProvider allowEmptyStringOptionData
49+
*/
50+
public function testDeprecatedAllowEmptyStringOption(bool $value)
51+
{
52+
$this->expectDeprecation('Since symfony/validator 5.2: The "allowEmptyString" option of the "Symfony\Component\Validator\Constraints\Length" constraint is deprecated.');
53+
54+
new Length(['allowEmptyString' => $value, 'max' => 5]);
55+
}
56+
57+
public function allowEmptyStringOptionData()
58+
{
59+
return [
60+
[true],
61+
[false],
62+
];
63+
}
4264
}

src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public function testNullIsValid()
2929
$this->assertNoViolation();
3030
}
3131

32+
/**
33+
* @group legacy
34+
*/
3235
public function testAllowEmptyString()
3336
{
3437
$this->validator->validate('', new Length(['value' => 6, 'allowEmptyString' => true]));

src/Symfony/Component/Validator/composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
],
1818
"require": {
1919
"php": ">=7.2.5",
20+
"symfony/deprecation-contracts": "^2.1",
2021
"symfony/polyfill-ctype": "~1.8",
2122
"symfony/polyfill-mbstring": "~1.0",
2223
"symfony/polyfill-php80": "^1.15",

0 commit comments

Comments
 (0)