Skip to content

Commit

Permalink
feature #31526 [Validator] Add compared value path to violation param…
Browse files Browse the repository at this point in the history
…eters (ogizanagi)

This PR was merged into the 4.4 branch.

Discussion
----------

[Validator] Add compared value path to violation parameters

| Q             | A
| ------------- | ---
| Branch?       | 4.4 <!-- see below -->
| Bug fix?      | no
| New feature?  | yes <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | N/A   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | N/A

While it's not really useful to use as a placeholder in the violation message template (compared to hard-coding it into the message. Nor it is really user-friendly),
it becomes handy in conjunction with #29130 for any mapping logic on client-side.

Commits
-------

2da226a [Validator] Add compared value path to violation parameters
  • Loading branch information
fabpot committed Jun 4, 2019
2 parents 6f9d0f0 + 2da226a commit 129bf73
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 3 deletions.
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');
}
}

0 comments on commit 129bf73

Please sign in to comment.