8000 [Validator] Add a Length::$allowEmptyString option to reject empty strings by ogizanagi · Pull Request #31528 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Add a Length::$allowEmptyString option to reject empty strings #31528

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 3, 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
4 changes: 4 additions & 0 deletions UPGRADE-4.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,7 @@ Validator

* Deprecated passing an `ExpressionLanguage` instance as the second argument of `ExpressionValidator::__construct()`.
Pass it as the first argument instead.
* The `Length` constraint expects the `allowEmptyString` option to be defined
when the `min` option is used.
Set it to `true` to keep the current behavior and `false` to reject empty strings.
In 5.0, it'll become optional and will default to `false`.
14 changes: 14 additions & 0 deletions src/Symfony/Bridge/Doctrine/Tests/Fixtures/BaseUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

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 @@ -46,4 +49,15 @@ 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,6 +14,7 @@
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 @@ -36,13 +37,11 @@ class DoctrineLoaderEntity extends DoctrineLoaderParentEntity

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

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

Expand All @@ -69,4 +68,12 @@ 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] : [];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to keep compatibility with lowest symfony/validator versions for this bridge, as the code isn't impacted, only the tests fixtures are.

Same for the Form component.


$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,11 +9,6 @@
<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>
</constraint>
</property>
</class>
</constraint-mapping>
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ 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 @@ -142,6 +143,7 @@ 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,13 +57,15 @@ 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']]),
new Length(['min' => 10, 'groups' => ['First']] + $allowEmptyString),
new Email(['groups' => ['Second']]),
],
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ 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(10), new ValueGuess(false, Guess::LOW_CONFIDENCE)],
[new Length(['min' => 10, 'max' => 10] + $allowEmptyString), new ValueGuess(false, Guess::LOW_CONFIDENCE)],
[new Range(['min' => 1, 'max' => 20]), new ValueGuess(false, Guess::LOW_CONFIDENCE)],
];
}
Expand Down Expand Up @@ -101,7 +103,9 @@ public function testGuessMaxLengthForConstraintWithMaxValue()

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

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

$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 @@ -8,6 +8,7 @@ CHANGELOG
* added the `compared_value_path` parameter in violations when using any
comparison constraint with the `propertyPath` option.
* added support for checking an array of types in `TypeValidator`
* added a new `allowEmptyString` option to the `Length` constraint to allow rejecting empty strings when `min` is set, by setting it to `false`.

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

public function __construct($options = null)
{
Expand All @@ -56,6 +57,13 @@ 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 @@ -30,7 +30,7 @@ public function validate($value, Constraint $constraint)
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Length');
}

if (null === $value || '' === $value) {
if (null === $value || ('' === $value && $constraint->allowEmptyString)) {
return;
}

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']);
$length = new Length(['min' => 0, 'max' => 10, 'normalizer' => 'trim', 'allowEmptyString' => false]);

$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']);
new Length(['min' => 0, 'max' => 10, 'normalizer' => 'Unknown Callable', 'allowEmptyString' => false]);
}

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

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

$this->assertNoViolation();
}

public function testEmptyStringIsValid()
/**
* @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()
{
$this->validator->validate('', new Length(6));

$this->assertNoViolation();
}

public function testEmptyStringIsInvalid()
{
$this->validator->validate('', new Length([
'value' => $limit = 6,
'allowEmptyString' => false,
'exactMessage' => 'myMessage',
]));

$this->buildViolation('myMessage')
->setParameter('{{ value }}', '""')
->setParameter('{{ limit }}', $limit)
->setInvalidValue('')
->setPlural($limit)
->setCode(Length::TOO_SHORT_ERROR)
->assertRaised();
}

/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedValueException
*/
public function testExpectsStringCompatibleType()
{
$this->validator->validate(new \stdClass(), new Length(5));
$this->validator->validate(new \stdClass(), new Length(['value' => 5, 'allowEmptyString' => false]));
}

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

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

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

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

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

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

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

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

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

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

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

$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'])],
'one' => [new NotBlank(['groups' => 'one']), new Length(['min' => 2, 'groups' => 'two', 'allowEmptyString' => false])],
'two' => [new NotBlank(['groups' => 'two'])],
]]));

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

$entity = new Entity();
$entity->data = ['one' => 't', 'two' => ''];

$violations = $this->validator->validate($entity, null, ['one', 'two']);

$this->assertCount(2, $violations);
$this->assertCount(3, $violations);
$this->assertInstanceOf(NotBlank::class, $violations->get(0)->getConstraint());
$this->assertInstanceOf(Length::class, $violations->get(1)->getConstraint());
$this->assertInstanceOf(Length::class, $violations->get(2)->getConstraint());
}
}
0