Skip to content
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

[Validator] Add compared value path to violation parameters #31526

Merged
merged 1 commit into from Jun 4, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
@@ -1,6 +1,12 @@
CHANGELOG
=========

4.4.0
-----

* added the `compared_value_path` parameter in violations when using any
comparison constraint with the `propertyPath` option.

4.3.0
-----

Expand Down
Expand Up @@ -77,12 +77,17 @@ public function validate($value, Constraint $constraint)
}

if (!$this->compareValues($value, $comparedValue)) {
$this->context->buildViolation($constraint->message)
$violationBuilder = $this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE))
->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE))
->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue))
->setCode($this->getErrorCode())
->addViolation();
->setCode($this->getErrorCode());

if (null !== $path) {
$violationBuilder->setParameter('{{ compared_value_path }}', $path);
}

$violationBuilder->addViolation();
}
}

Expand Down
Expand Up @@ -231,6 +231,28 @@ public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $
->assertRaised();
}

public function testInvalidComparisonToPropertyPathAddsPathAsParameter()
{
list($dirtyValue, $dirtyValueAsString, $comparedValue, $comparedValueString, $comparedValueType) = current($this->provideAllInvalidComparisons());

$constraint = $this->createConstraint(['propertyPath' => 'value']);
$constraint->message = 'Constraint Message';

$object = new ComparisonTest_Class($comparedValue);

$this->setObject($object);

$this->validator->validate($dirtyValue, $constraint);

$this->buildViolation('Constraint Message')
->setParameter('{{ value }}', $dirtyValueAsString)
->setParameter('{{ compared_value }}', $comparedValueString)
->setParameter('{{ compared_value_path }}', 'value')
->setParameter('{{ compared_value_type }}', $comparedValueType)
->setCode($this->getErrorCode())
->assertRaised();
}

/**
* @return array
*/
Expand Down
Expand Up @@ -108,4 +108,9 @@ public function testValidComparisonToPropertyPathOnArray($comparedValue)
{
$this->markTestSkipped('PropertyPath option is not used in Positive constraint');
}

public function testInvalidComparisonToPropertyPathAddsPathAsParameter()
{
$this->markTestSkipped('PropertyPath option is not used in PositiveOrZero constraint');
}
}
Expand Up @@ -111,4 +111,9 @@ public function testValidComparisonToPropertyPathOnArray($comparedValue)
{
$this->markTestSkipped('PropertyPath option is not used in Positive constraint');
}

public function testInvalidComparisonToPropertyPathAddsPathAsParameter()
{
$this->markTestSkipped('PropertyPath option is not used in Positive constraint');
}
}
Expand Up @@ -111,4 +111,9 @@ public function testValidComparisonToPropertyPathOnArray($comparedValue)
{
$this->markTestSkipped('PropertyPath option is not used in NegativeOrZero constraint');
}

public function testInvalidComparisonToPropertyPathAddsPathAsParameter()
{
$this->markTestSkipped('PropertyPath option is not used in NegativeOrZero constraint');
}
}
Expand Up @@ -111,4 +111,9 @@ public function testValidComparisonToPropertyPathOnArray($comparedValue)
{
$this->markTestSkipped('PropertyPath option is not used in Positive constraint');
}

public function testInvalidComparisonToPropertyPathAddsPathAsParameter()
{
$this->markTestSkipped('PropertyPath option is not used in Negative constraint');
}
}