Skip to content

[Validator] Change Length::$allowEmptyString default to false & make it optional #32373

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

Merged
merged 1 commit into from
Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ Validator
* The `symfony/intl` component is now required for using the `Bic`, `Country`, `Currency`, `Language` and `Locale` constraints
* The `egulias/email-validator` component is now required for using the `Email` constraint in strict mode
* The `symfony/expression-language` component is now required for using the `Expression` constraint
* Changed the default value of `Length::$allowEmptyString` to `false` and made it optional

Workflow
--------
Expand Down
14 changes: 0 additions & 14 deletions src/Symfony/Bridge/Doctrine/Tests/Fixtures/BaseUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace Symfony\Bridge\Doctrine\Tests\Fixtures;

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\ClassMetadata;

/**
* Class BaseUser.
*/
Expand Down Expand Up @@ -49,15 +46,4 @@ public function getUsername()
{
return $this->username;
}

public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$allowEmptyString = property_exists(Assert\Length::class, 'allowEmptyString') ? ['allowEmptyString' => true] : [];

$metadata->addPropertyConstraint('username', new Assert\Length([
'min' => 2,
'max' => 120,
'groups' => ['Registration'],
] + $allowEmptyString));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\ClassMetadata;

/**
* @ORM\Entity
Expand All @@ -37,11 +36,13 @@ class DoctrineLoaderEntity extends DoctrineLoaderParentEntity

/**
* @ORM\Column(length=20)
* @Assert\Length(min=5, allowEmptyString=true)
*/
public $mergedMaxLength;

/**
* @ORM\Column(length=20)
* @Assert\Length(min=1, max=10, allowEmptyString=true)
*/
public $alreadyMappedMaxLength;

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

/** @ORM\Column(type="simple_array", length=100) */
public $simpleArrayField = [];

public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$allowEmptyString = property_exists(Assert\Length::class, 'allowEmptyString') ? ['allowEmptyString' => true] : [];

$metadata->addPropertyConstraint('mergedMaxLength', new Assert\Length(['min' => 5] + $allowEmptyString));
$metadata->addPropertyConstraint('alreadyMappedMaxLength', new Assert\Length(['min' => 1, 'max' => 10] + $allowEmptyString));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
<constraint name="NotBlank">
<option name="groups">Registration</option>
</constraint>
<constraint name="Length">
<option name="min">2</option>
<option name="max">120</option>
<option name="groups">Registration</option>
<option name="allowEmptyString">true</option>
</constraint>
</property>
</class>
</constraint-mapping>
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public function testLoadClassMetadata()
}

$validator = Validation::createValidatorBuilder()
->addMethodMapping('loadValidatorMetadata')
->enableAnnotationMapping()
->addLoader(new DoctrineLoader(DoctrineTestHelper::createTestEntityManager(), '{^Symfony\\\\Bridge\\\\Doctrine\\\\Tests\\\\Fixtures\\\\DoctrineLoader}'))
->getValidator()
Expand Down Expand Up @@ -143,7 +142,6 @@ public function testFieldMappingsConfiguration()
}

$validator = Validation::createValidatorBuilder()
->addMethodMapping('loadValidatorMetadata')
->enableAnnotationMapping()
->addXmlMappings([__DIR__.'/../Resources/validator/BaseUser.xml'])
->addLoader(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,13 @@ public function testValidConstraint()

public function testGroupSequenceWithConstraintsOption()
{
$allowEmptyString = property_exists(Length::class, 'allowEmptyString') ? ['allowEmptyString' => true] : [];

$form = Forms::createFormFactoryBuilder()
->addExtension(new ValidatorExtension(Validation::createValidator()))
->getFormFactory()
->create(FormTypeTest::TESTED_TYPE, null, (['validation_groups' => new GroupSequence(['First', 'Second'])]))
->add('field', TextTypeTest::TESTED_TYPE, [
'constraints' => [
new Length(['min' => 10, 'groups' => ['First']] + $allowEmptyString),
new Length(['min' => 10, 'allowEmptyString' => true, 'groups' => ['First']]),
new Email(['groups' => ['Second']]),
],
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,11 @@ protected function setUp()

public function guessRequiredProvider()
{
$allowEmptyString = property_exists(Length::class, 'allowEmptyString') ? ['allowEmptyString' => true] : [];

return [
[new NotNull(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)],
[new NotBlank(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)],
[new IsTrue(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)],
[new Length(['min' => 10, 'max' => 10] + $allowEmptyString), new ValueGuess(false, Guess::LOW_CONFIDENCE)],
[new Length(['min' => 10, 'max' => 10, 'allowEmptyString' => true]), new ValueGuess(false, Guess::LOW_CONFIDENCE)],
[new Range(['min' => 1, 'max' => 20]), new ValueGuess(false, Guess::LOW_CONFIDENCE)],
];
}
Expand Down Expand Up @@ -103,9 +101,7 @@ public function testGuessMaxLengthForConstraintWithMaxValue()

public function testGuessMaxLengthForConstraintWithMinValue()
{
$allowEmptyString = property_exists(Length::class, 'allowEmptyString') ? ['allowEmptyString' => true] : [];

$constraint = new Length(['min' => '2'] + $allowEmptyString);
$constraint = new Length(['min' => '2', 'allowEmptyString' => true]);

$result = $this->guesser->guessMaxLengthForConstraint($constraint);
$this->assertNull($result);
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ CHANGELOG
* changed default value of `canonicalize` option of `Locale` constraint to `true`
* removed `ValidatorBuilderInterface`
* passing a null message when instantiating a `ConstraintViolation` is not allowed
* changed the default value of `Length::$allowEmptyString` to `false` and made it optional

4.4.0
-----
Expand Down
9 changes: 1 addition & 8 deletions src/Symfony/Component/Validator/Constraints/Length.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Length extends Constraint
public $min;
public $charset = 'UTF-8';
public $normalizer;
public $allowEmptyString;
public $allowEmptyString = false;

public function __construct($options = null)
{
Expand All @@ -57,13 +57,6 @@ public function __construct($options = null)

parent::__construct($options);

if (null === $this->allowEmptyString) {
$this->allowEmptyString = true;
if (null !== $this->min) {
@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);
}
}

if (null === $this->min && null === $this->max) {
throw new MissingOptionsException(sprintf('Either option "min" or "max" must be given for constraint %s', __CLASS__), ['min', 'max']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class LengthTest extends TestCase
{
public function testNormalizerCanBeSet()
{
$length = new Length(['min' => 0, 'max' => 10, 'normalizer' => 'trim', 'allowEmptyString' => false]);
$length = new Length(['min' => 0, 'max' => 10, 'normalizer' => 'trim']);

$this->assertEquals('trim', $length->normalizer);
}
Expand All @@ -32,7 +32,7 @@ public function testNormalizerCanBeSet()
*/
public function testInvalidNormalizerThrowsException()
{
new Length(['min' => 0, 'max' => 10, 'normalizer' => 'Unknown Callable', 'allowEmptyString' => false]);
new Length(['min' => 0, 'max' => 10, 'normalizer' => 'Unknown Callable']);
}

/**
Expand All @@ -41,6 +41,6 @@ public function testInvalidNormalizerThrowsException()
*/
public function testInvalidNormalizerObjectThrowsException()
{
new Length(['min' => 0, 'max' => 10, 'normalizer' => new \stdClass(), 'allowEmptyString' => false]);
new Length(['min' => 0, 'max' => 10, 'normalizer' => new \stdClass()]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,16 @@ protected function createValidator()
return new LengthValidator();
}

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

$this->assertNoViolation();
}

/**
* @group legacy
* @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.
*/
public function testLegacyEmptyStringIsValid()
public function testAllowEmptyString()
{
$this->validator->validate('', new Length(6));
$this->validator->validate('', new Length(['value' => 6, 'allowEmptyString' => true]));

$this->assertNoViolation();
}
Expand All @@ -44,7 +40,6 @@ public function testEmptyStringIsInvalid()
{
$this->validator->validate('', new Length([
'value' => $limit = 6,
'allowEmptyString' => false,
'exactMessage' => 'myMessage',
]));

Expand All @@ -62,7 +57,7 @@ public function testEmptyStringIsInvalid()
*/
public function testExpectsStringCompatibleType()
{
$this->validator->validate(new \stdClass(), new Length(['value' => 5, 'allowEmptyString' => false]));
$this->validator->validate(new \stdClass(), new Length(['value' => 5]));
}

public function getThreeOrLessCharacters()
Expand Down Expand Up @@ -130,7 +125,7 @@ public function getThreeCharactersWithWhitespaces()
*/
public function testValidValuesMin($value)
{
$constraint = new Length(['min' => 5, 'allowEmptyString' => false]);
$constraint = new Length(['min' => 5]);
$this->validator->validate($value, $constraint);

$this->assertNoViolation();
Expand All @@ -152,7 +147,7 @@ public function testValidValuesMax($value)
*/
public function testValidValuesExact($value)
{
$constraint = new Length(['value' => 4, 'allowEmptyString' => false]);
$constraint = new Length(4);
$this->validator->validate($value, $constraint);

$this->assertNoViolation();
Expand All @@ -163,7 +158,7 @@ public function testValidValuesExact($value)
*/
public function testValidNormalizedValues($value)
{
$constraint = new Length(['min' => 3, 'max' => 3, 'normalizer' => 'trim', 'allowEmptyString' => false]);
$constraint = new Length(['min' => 3, 'max' => 3, 'normalizer' => 'trim']);
$this->validator->validate($value, $constraint);

$this->assertNoViolation();
Expand All @@ -177,7 +172,6 @@ public function testInvalidValuesMin($value)
$constraint = new Length([
'min' => 4,
'minMessage' => 'myMessage',
'allowEmptyString' => false,
]);

$this->validator->validate($value, $constraint);
Expand Down Expand Up @@ -221,7 +215,6 @@ public function testInvalidValuesExactLessThanFour($value)
'min' => 4,
'max' => 4,
'exactMessage' => 'myMessage',
'allowEmptyString' => false,
]);

$this->validator->validate($value, $constraint);
Expand All @@ -244,7 +237,6 @@ public function testInvalidValuesExactMoreThanFour($value)
'min' => 4,
'max' => 4,
'exactMessage' => 'myMessage',
'allowEmptyString' => false,
]);

$this->validator->validate($value, $constraint);
Expand All @@ -268,7 +260,6 @@ public function testOneCharset($value, $charset, $isValid)
'max' => 1,
'charset' => $charset,
'charsetMessage' => 'myMessage',
'allowEmptyString' => false,
]);

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

public function testConstraintDefaultOption()
{
$constraint = new Length(['value' => 5, 'allowEmptyString' => false]);
$constraint = new Length(5);

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

public function testConstraintAnnotationDefaultOption()
{
$constraint = new Length(['value' => 5, 'exactMessage' => 'message', 'allowEmptyString' => false]);
$constraint = new Length(['value' => 5, 'exactMessage' => 'message']);

$this->assertEquals(5, $constraint->min);
$this->assertEquals(5, $constraint->max);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function testRelationBetweenChildAAndChildB()
public function testCollectionConstraintValidateAllGroupsForNestedConstraints()
{
$this->metadata->addPropertyConstraint('data', new Collection(['fields' => [
'one' => [new NotBlank(['groups' => 'one']), new Length(['min' => 2, 'groups' => 'two', 'allowEmptyString' => false])],
'one' => [new NotBlank(['groups' => 'one']), new Length(['min' => 2, 'groups' => 'two'])],
'two' => [new NotBlank(['groups' => 'two'])],
]]));

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

$entity = new Entity();
Expand Down