Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f065c41

Browse files
committed
feature #32373 [Validator] Change Length::$allowEmptyString default to 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
2 parents 8369270 + 26b804e commit f065c41

File tree

12 files changed

+29
-66
lines changed

12 files changed

+29
-66
lines changed

UPGRADE-5.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ Validator
490490
* The `symfony/intl` component is now required for using the `Bic`, `Country`, `Currency`, `Language` and `Locale` constraints
491491
* The `egulias/email-validator` component is now required for using the `Email` constraint in strict mode
492492
* The `symfony/expression-language` component is now required for using the `Expression` constraint
493+
* Changed the default value of `Length::$allowEmptyString` to `false` and made it optional
493494

494495
Workflow
495496
--------

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
namespace Symfony\Bridge\Doctrine\Tests\Fixtures;
44

5-
use Symfony\Component\Validator\Constraints as Assert;
6-
use Symfony\Component\Validator\Mapping\ClassMetadata;
7-
85
/**
96
* Class BaseUser.
107
*/
@@ -49,15 +46,4 @@ public function getUsername()
4946
{
5047
return $this->username;
5148
}
52-
53-
public static function loadValidatorMetadata(ClassMetadata $metadata): void
54-
{
55-
$allowEmptyString = property_exists(Assert\Length::class, 'allowEmptyString') ? ['allowEmptyString' => true] : [];
56-
57-
$metadata->addPropertyConstraint('username', new Assert\Length([
58-
'min' => 2,
59-
'max' => 120,
60-
'groups' => ['Registration'],
61-
] + $allowEmptyString));
62-
}
6349
}

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Doctrine\ORM\Mapping as ORM;
1515
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
1616
use Symfony\Component\Validator\Constraints as Assert;
17-
use Symfony\Component\Validator\Mapping\ClassMetadata;
1817

1918
/**
2019
* @ORM\Entity
@@ -37,11 +36,13 @@ class DoctrineLoaderEntity extends DoctrineLoaderParentEntity
3736

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

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

@@ -68,12 +69,4 @@ class DoctrineLoaderEntity extends DoctrineLoaderParentEntity
6869

6970
/** @ORM\Column(type="simple_array", length=100) */
7071
public $simpleArrayField = [];
71-
72-
public static function loadValidatorMetadata(ClassMetadata $metadata): void
73-
{
74-
$allowEmptyString = property_exists(Assert\Length::class, 'allowEmptyString') ? ['allowEmptyString' => true] : [];
75-
76-
$metadata->addPropertyConstraint('mergedMaxLength', new Assert\Length(['min' => 5] + $allowEmptyString));
77-
$metadata->addPropertyConstraint('alreadyMappedMaxLength', new Assert\Length(['min' => 1, 'max' => 10] + $allowEmptyString));
78-
}
7972
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
<constraint name="NotBlank">
1010
<option name="groups">Registration</option>
1111
</constraint>
12+
<constraint name="Length">
13+
<option name="min">2</option>
14+
<option name="max">120</option>
15+
<option name="groups">Registration</option>
16+
<option name="allowEmptyString">true</option>
17+
</constraint>
1218
</property>
1319
</class>
1420
</constraint-mapping>

src/Symfony/Bridge/Doctrine/Tests/Validator/DoctrineLoaderTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public function testLoadClassMetadata()
4040
}
4141

4242
$validator = Validation::createValidatorBuilder()
43-
->addMethodMapping('loadValidatorMetadata')
4443
->enableAnnotationMapping()
4544
->addLoader(new DoctrineLoader(DoctrineTestHelper::createTestEntityManager(), '{^Symfony\\\\Bridge\\\\Doctrine\\\\Tests\\\\Fixtures\\\\DoctrineLoader}'))
4645
->getValidator()
@@ -143,7 +142,6 @@ public function testFieldMappingsConfiguration()
143142
}
144143

145144
$validator = Validation::createValidatorBuilder()
146-
->addMethodMapping('loadValidatorMetadata')
147145
->enableAnnotationMapping()
148146
->addXmlMappings([__DIR__.'/../Resources/validator/BaseUser.xml'])
149147
->addLoader(

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

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

5858
public function testGroupSequenceWithConstraintsOption()
5959
{
60-
$allowEmptyString = property_exists(Length::class, 'allowEmptyString') ? ['allowEmptyString' => true] : [];
61-
6260
$form = Forms::createFormFactoryBuilder()
6361
->addExtension(new ValidatorExtension(Validation::createValidator()))
6462
->getFormFactory()
6563
->create(FormTypeTest::TESTED_TYPE, null, (['validation_groups' => new GroupSequence(['First', 'Second'])]))
6664
->add('field', TextTypeTest::TESTED_TYPE, [
6765
'constraints' => [
68-
new Length(['min' => 10, 'groups' => ['First']] + $allowEmptyString),
66+
new Length(['min' => 10, 'allowEmptyString' => true, 'groups' => ['First']]),
6967
new Email(['groups' => ['Second']]),
7068
],
7169
])

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

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

6262
public function guessRequiredProvider()
6363
{
64-
$allowEmptyString = property_exists(Length::class, 'allowEmptyString') ? ['allowEmptyString' => true] : [];
65-
6664
return [
6765
[new NotNull(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)],
6866
[new NotBlank(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)],
6967
[new IsTrue(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)],
70-
[new Length(['min' => 10, 'max' => 10] + $allowEmptyString), new ValueGuess(false, Guess::LOW_CONFIDENCE)],
68+
[new Length(['min' => 10, 'max' => 10, 'allowEmptyString' => true]), new ValueGuess(false, Guess::LOW_CONFIDENCE)],
7169
[new Range(['min' => 1, 'max' => 20]), new ValueGuess(false, Guess::LOW_CONFIDENCE)],
7270
];
7371
}
@@ -103,9 +101,7 @@ public function testGuessMaxLengthForConstraintWithMaxValue()
103101

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

110106
$result = $this->guesser->guessMaxLengthForConstraint($constraint);
111107
$this->assertNull($result);

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ CHANGELOG
1414
* changed default value of `canonicalize` option of `Locale` constraint to `true`
1515
* removed `ValidatorBuilderInterface`
1616
* passing a null message when instantiating a `ConstraintViolation` is not allowed
17+
* changed the default value of `Length::$allowEmptyString` to `false` and made it optional
1718

1819
4.4.0
1920
-----

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Length extends Constraint
4141
public $min;
4242
public $charset = 'UTF-8';
4343
public $normalizer;
44-
public $allowEmptyString;
44+
public $allowEmptyString = false;
4545

4646
public function __construct($options = null)
4747
{
@@ -57,13 +57,6 @@ public function __construct($options = null)
5757

5858
parent::__construct($options);
5959

60-
if (null === $this->allowEmptyString) {
61-
$this->allowEmptyString = true;
62-
if (null !== $this->min) {
63-
@trigger_error(sprintf('Using the "%s" constraint with the "min" option without setting the "allowEmptyString" one is deprecated and defaults to true. In 5.0, it will become optional and default to false.', self::class), E_USER_DEPRECATED);
64-
}
65-
}
66-
6760
if (null === $this->min && null === $this->max) {
6861
throw new MissingOptionsException(sprintf('Either option "min" or "max" must be given for constraint %s', __CLASS__), ['min', 'max']);
6962
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class LengthTest extends TestCase
2121
{
2222
public function testNormalizerCanBeSet()
2323
{
24-
$length = new Length(['min' => 0, 'max' => 10, 'normalizer' => 'trim', 'allowEmptyString' => false]);
24+
$length = new Length(['min' => 0, 'max' => 10, 'normalizer' => 'trim']);
2525

2626
$this->assertEquals('trim', $length->normalizer);
2727
}
@@ -32,7 +32,7 @@ public function testNormalizerCanBeSet()
3232
*/
3333
public function testInvalidNormalizerThrowsException()
3434
{
35-
new Length(['min' => 0, 'max' => 10, 'normalizer' => 'Unknown Callable', 'allowEmptyString' => false]);
35+
new Length(['min' => 0, 'max' => 10, 'normalizer' => 'Unknown Callable']);
3636
}
3737

3838
/**
@@ -41,6 +41,6 @@ public function testInvalidNormalizerThrowsException()
4141
*/
4242
public function testInvalidNormalizerObjectThrowsException()
4343
{
44-
new Length(['min' => 0, 'max' => 10, 'normalizer' => new \stdClass(), 'allowEmptyString' => false]);
44+
new Length(['min' => 0, 'max' => 10, 'normalizer' => new \stdClass()]);
4545
}
4646
}

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

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,16 @@ protected function createValidator()
2222
return new LengthValidator();
2323
}
2424

25-
public function testLegacyNullIsValid()
25+
public function testNullIsValid()
2626
{
27-
$this->validator->validate(null, new Length(['value' => 6, 'allowEmptyString' => false]));
27+
$this->validator->validate(null, new Length(['value' => 6]));
2828

2929
$this->assertNoViolation();
3030
}
3131

32-
/**
33-
* @group legacy
34-
* @expectedDeprecation Using the "Symfony\Component\Validator\Constraints\Length" constraint with the "min" option without setting the "allowEmptyString" one is deprecated and defaults to true. In 5.0, it will become optional and default to false.
35-
*/
36-
public function testLegacyEmptyStringIsValid()
32+
public function testAllowEmptyString()
3733
{
38-
$this->validator->validate('', new Length(6));
34+
$this->validator->validate('', new Length(['value' => 6, 'allowEmptyString' => true]));
3935

4036
$this->assertNoViolation();
4137
}
@@ -44,7 +40,6 @@ public function testEmptyStringIsInvalid()
4440
{
4541
$this->validator->validate('', new Length([
4642
'value' => $limit = 6,
47-
'allowEmptyString' => false,
4843
'exactMessage' => 'myMessage',
4944
]));
5045

@@ -62,7 +57,7 @@ public function testEmptyStringIsInvalid()
6257
*/
6358
public function testExpectsStringCompatibleType()
6459
{
65-
$this->validator->validate(new \stdClass(), new Length(['value' => 5, 'allowEmptyString' => false]));
60+
$this->validator->validate(new \stdClass(), new Length(['value' => 5]));
6661
}
6762

6863
public function getThreeOrLessCharacters()
@@ -130,7 +125,7 @@ public function getThreeCharactersWithWhitespaces()
130125
*/
131126
public function testValidValuesMin($value)
132127
{
133-
$constraint = new Length(['min' => 5, 'allowEmptyString' => false]);
128+
$constraint = new Length(['min' => 5]);
134129
$this->validator->validate($value, $constraint);
135130

136131
$this->assertNoViolation();
@@ -152,7 +147,7 @@ public function testValidValuesMax($value)
152147
*/
153148
public function testValidValuesExact($value)
154149
{
155-
$constraint = new Length(['value' => 4, 'allowEmptyString' => false]);
150+
$constraint = new Length(4);
156151
$this->validator->validate($value, $constraint);
157152

158153
$this->assertNoViolation();
@@ -163,7 +158,7 @@ public function testValidValuesExact($value)
163158
*/
164159
public function testValidNormalizedValues($value)
165160
{
166-
$constraint = new Length(['min' => 3, 'max' => 3, 'normalizer' => 'trim', 'allowEmptyString' => false]);
161+
$constraint = new Length(['min' => 3, 'max' => 3, 'normalizer' => 'trim']);
167162
$this->validator->validate($value, $constraint);
168163

169164
$this->assertNoViolation();
@@ -177,7 +172,6 @@ public function testInvalidValuesMin($value)
177172
$constraint = new Length([
178173
'min' => 4,
179174
'minMessage' => 'myMessage',
180-
'allowEmptyString' => false,
181175
]);
182176

183177
$this->validator->validate($value, $constraint);
@@ -221,7 +215,6 @@ public function testInvalidValuesExactLessThanFour($value)
221215
'min' => 4,
222216
'max' => 4,
223217
'exactMessage' => 'myMessage',
224-
'allowEmptyString' => false,
225218
]);
226219

227220
$this->validator->validate($value, $constraint);
@@ -244,7 +237,6 @@ public function testInvalidValuesExactMoreThanFour($value)
244237
'min' => 4,
245238
'max' => 4,
246239
'exactMessage' => 'myMessage',
247-
'allowEmptyString' => false,
248240
]);
249241

250242
$this->validator->validate($value, $constraint);
@@ -268,7 +260,6 @@ public function testOneCharset($value, $charset, $isValid)
268260
'max' => 1,
269261
'charset' => $charset,
270262
'charsetMessage' => 'myMessage',
271-
'allowEmptyString' => false,
272263
]);
273264

274265
$this->validator->validate($value, $constraint);
@@ -287,15 +278,15 @@ public function testOneCharset($value, $charset, $isValid)
287278

288279
public function testConstraintDefaultOption()
289280
{
290-
$constraint = new Length(['value' => 5, 'allowEmptyString' => false]);
281+
$constraint = new Length(5);
291282

292283
$this->assertEquals(5, $constraint->min);
293284
$this->assertEquals(5, $constraint->max);
294285
}
295286

296287
public function testConstraintAnnotationDefaultOption()
297288
{
298-
$constraint = new Length(['value' => 5, 'exactMessage' => 'message', 'allowEmptyString' => false]);
289+
$constraint = new Length(['value' => 5, 'exactMessage' => 'message']);
299290

300291
$this->assertEquals(5, $constraint->min);
301292
$this->assertEquals(5, $constraint->max);

src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function testRelationBetweenChildAAndChildB()
103103
public function testCollectionConstraintValidateAllGroupsForNestedConstraints()
104104
{
105105
$this->metadata->addPropertyConstraint('data', new Collection(['fields' => [
106-
'one' => [new NotBlank(['groups' => 'one']), new Length(['min' => 2, 'groups' => 'two', 'allowEmptyString' => false])],
106+
'one' => [new NotBlank(['groups' => 'one']), new Length(['min' => 2, 'groups' => 'two'])],
107107
'two' => [new NotBlank(['groups' => 'two'])],
108108
]]));
109109

@@ -121,7 +121,7 @@ public function testAllConstraintValidateAllGroupsForNestedConstraints()
121121
{
122122
$this->metadata->addPropertyConstraint('data', new All(['constraints' => [
123123
new NotBlank(['groups' => 'one']),
124-
new Length(['min' => 2, 'groups' => 'two', 'allowEmptyString' => false]),
124+
new Length(['min' => 2, 'groups' => 'two']),
125125
]]));
126126

127127
$entity = new Entity();

0 commit comments

Comments
 (0)