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

Skip to content

Commit 840f7e7

Browse files
[Validator] Set Length::$allowEmptyString to false when a NotBlank contraint is defined
1 parent bfd308f commit 840f7e7

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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/Constraints/LengthValidator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ public function validate($value, Constraint $constraint)
3030
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Length');
3131
}
3232

33-
if (null === $value || ('' === $value && $constraint->allowEmptyString)) {
33+
if (null !== $constraint->min && null === $constraint->allowEmptyString) {
34+
@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.', Length::class), E_USER_DEPRECATED);
35+
}
36+
37+
if (null === $value || ('' === $value && ($constraint->allowEmptyString ?? true))) {
3438
return;
3539
}
3640

src/Symfony/Component/Validator/Mapping/GenericMetadata.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Validator\Mapping;
1313

1414
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\Constraints\Length;
16+
use Symfony\Component\Validator\Constraints\NotBlank;
1517
use Symfony\Component\Validator\Constraints\Traverse;
1618
use Symfony\Component\Validator\Constraints\Valid;
1719
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
@@ -167,6 +169,8 @@ public function addConstraints(array $constraints)
167169
*/
168170
public function getConstraints()
169171
{
172+
$this->configureLengthConstraints($this->constraints);
173+
170174
return $this->constraints;
171175
}
172176

@@ -187,9 +191,10 @@ public function hasConstraints()
187191
*/
188192
public function findConstraints($group)
189193
{
190-
return isset($this->constraintsByGroup[$group])
191-
? $this->constraintsByGroup[$group]
192-
: [];
194+
$constraints = $this->constraintsByGroup[$group] ?? [];
195+
$this->configureLengthConstraints($constraints);
196+
197+
return $constraints;
193198
}
194199

195200
/**
@@ -207,4 +212,26 @@ public function getTraversalStrategy()
207212
{
208213
return $this->traversalStrategy;
209214
}
215+
216+
private function configureLengthConstraints(array $constraints): void
217+
{
218+
$allowEmptyString = true;
219+
220+
foreach ($constraints as $constraint) {
221+
if ($constraint instanceof NotBlank) {
222+
$allowEmptyString = false;
223+
break;
224+
}
225+
}
226+
227+
if ($allowEmptyString) {
228+
return;
229+
}
230+
231+
foreach ($constraints as $constraint) {
232+
if ($constraint instanceof Length && null === $constraint->allowEmptyString) {
233+
$constraint->allowEmptyString = false;
234+
}
235+
}
236+
}
210237
}

0 commit comments

Comments
 (0)