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
}

0 commit comments

Comments
 (0)