Skip to content

Commit a073606

Browse files
committed
bug #29839 [Validator] fix comparisons with null values at property paths (xabbuh)
This PR was merged into the 3.4 branch. Discussion ---------- [Validator] fix comparisons with null values at property paths | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #29831 | License | MIT | Doc PR | Commits ------- ffa5db7 fix comparisons with null values at property paths
2 parents ab8841e + ffa5db7 commit a073606

13 files changed

+87
-4
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class GreaterThanOrEqualValidator extends AbstractComparisonValidator
2424
*/
2525
protected function compareValues($value1, $value2)
2626
{
27-
return $value1 >= $value2;
27+
return null === $value2 || $value1 >= $value2;
2828
}
2929

3030
/**

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class GreaterThanValidator extends AbstractComparisonValidator
2424
*/
2525
protected function compareValues($value1, $value2)
2626
{
27-
return $value1 > $value2;
27+
return null === $value2 || $value1 > $value2;
2828
}
2929

3030
/**

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class LessThanOrEqualValidator extends AbstractComparisonValidator
2424
*/
2525
protected function compareValues($value1, $value2)
2626
{
27-
return $value1 <= $value2;
27+
return null === $value2 || $value1 <= $value2;
2828
}
2929

3030
/**

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class LessThanValidator extends AbstractComparisonValidator
2424
*/
2525
protected function compareValues($value1, $value2)
2626
{
27-
return $value1 < $value2;
27+
return null === $value2 || $value1 < $value2;
2828
}
2929

3030
/**

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

+27
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,31 @@ public function throwsOnInvalidStringDatesProvider()
234234
];
235235
}
236236

237+
/**
238+
* @dataProvider provideComparisonsToNullValueAtPropertyPath
239+
*/
240+
public function testCompareWithNullValueAtPropertyAt($dirtyValue, $dirtyValueAsString, $isValid)
241+
{
242+
$constraint = $this->createConstraint(['propertyPath' => 'value']);
243+
$constraint->message = 'Constraint Message';
244+
245+
$object = new ComparisonTest_Class(null);
246+
$this->setObject($object);
247+
248+
$this->validator->validate($dirtyValue, $constraint);
249+
250+
if ($isValid) {
251+
$this->assertNoViolation();
252+
} else {
253+
$this->buildViolation('Constraint Message')
254+
->setParameter('{{ value }}', $dirtyValueAsString)
255+
->setParameter('{{ compared_value }}', 'null')
256+
->setParameter('{{ compared_value_type }}', 'NULL')
257+
->setCode($this->getErrorCode())
258+
->assertRaised();
259+
}
260+
}
261+
237262
/**
238263
* @return array
239264
*/
@@ -255,6 +280,8 @@ public function provideAllInvalidComparisons()
255280
*/
256281
abstract public function provideInvalidComparisons();
257282

283+
abstract public function provideComparisonsToNullValueAtPropertyPath();
284+
258285
/**
259286
* @param array|null $options Options for the constraint
260287
*

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

+7
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,11 @@ public function provideInvalidComparisons()
7575
[new ComparisonTest_Class(4), '4', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'],
7676
];
7777
}
78+
79+
public function provideComparisonsToNullValueAtPropertyPath()
80+
{
81+
return [
82+
[5, '5', false],
83+
];
84+
}
7885
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,11 @@ public function provideInvalidComparisons()
7878
['b', '"b"', 'c', '"c"', 'string'],
7979
];
8080
}
81+
82+
public function provideComparisonsToNullValueAtPropertyPath()
83+
{
84+
return [
85+
[5, '5', true],
86+
];
87+
}
8188
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,11 @@ public function provideInvalidComparisons()
8080
['22', '"22"', '22', '"22"', 'string'],
8181
];
8282
}
83+
84+
public function provideComparisonsToNullValueAtPropertyPath()
85+
{
86+
return [
87+
[5, '5', true],
88+
];
89+
}
8390
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,11 @@ public function provideInvalidComparisons()
9393
[new ComparisonTest_Class(4), '4', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'],
9494
];
9595
}
96+
97+
public function provideComparisonsToNullValueAtPropertyPath()
98+
{
99+
return [
100+
[5, '5', false],
101+
];
102+
}
96103
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,11 @@ public function provideInvalidComparisons()
8181
['c', '"c"', 'b', '"b"', 'string'],
8282
];
8383
}
84+
85+
public function provideComparisonsToNullValueAtPropertyPath()
86+
{
87+
return [
88+
[5, '5', true],
89+
];
90+
}
8491
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,11 @@ public function provideInvalidComparisons()
7979
['333', '"333"', '22', '"22"', 'string'],
8080
];
8181
}
82+
83+
public function provideComparisonsToNullValueAtPropertyPath()
84+
{
85+
return [
86+
[5, '5', true],
87+
];
88+
}
8289
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,11 @@ public function provideInvalidComparisons()
7575
[new ComparisonTest_Class(5), '5', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'],
7676
];
7777
}
78+
79+
public function provideComparisonsToNullValueAtPropertyPath()
80+
{
81+
return [
82+
[5, '5', true],
83+
];
84+
}
7885
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,11 @@ public function provideInvalidComparisons()
9393

9494
return $comparisons;
9595
}
96+
97+
public function provideComparisonsToNullValueAtPropertyPath()
98+
{
99+
return [
100+
[5, '5', true],
101+
];
102+
}
96103
}

0 commit comments

Comments
 (0)