8000 [Validator] Add compared value path to violation parameters by ogizanagi · Pull Request #31526 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

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

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
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
8000
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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');
}
}
< 8000 thead class="sr-only">
Original file line number Diff line number Diff line change
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');
}
}
Original file line number Diff line number Diff line change
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');
}
}
Original file line number Diff line number Diff line change
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