Skip to content

Commit 499eeb4

Browse files
committed
[Validator] Made it possible to store the cause of a constraint violation
1 parent 181e460 commit 499eeb4

6 files changed

+68
-11
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ CHANGELOG
1414
* deprecated `ClassMetadata::addMemberMetadata()`
1515
* [BC BREAK] added `Mapping\MetadataInterface::getConstraints()`
1616
* added generic "payload" option to all constraints for attaching domain-specific data
17+
* [BC BREAK] added `ConstraintViolationBuilderInterface::setCause()`
1718

1819
2.5.0
1920
-----

src/Symfony/Component/Validator/ConstraintViolation.php

+21-4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ class ConstraintViolation implements ConstraintViolationInterface
6363
*/
6464
private $code;
6565

66+
/**
67+
* @var mixed
68+
*/
69+
private $cause;
70+
6671
/**
6772
* Creates a new constraint violation.
6873
*
@@ -79,10 +84,11 @@ class ConstraintViolation implements ConstraintViolationInterface
7984
* @param int|null $plural The number for determining the plural
8085
* form when translating the message
8186
* @param mixed $code The error code of the violation
82-
* @param Constraint|null $constraint The constraint that caused the
83-
* violation
87+
* @param Constraint|null $constraint The constraint whose validation
88+
* caused the violation
89+
* @param mixed $cause The cause of the violation
8490
*/
85-
public function __construct($message, $messageTemplate, array $parameters, $root, $propertyPath, $invalidValue, $plural = null, $code = null, Constraint $constraint = null)
91+
public function __construct($message, $messageTemplate, array $parameters, $root, $propertyPath, $invalidValue, $plural = null, $code = null, Constraint $constraint = null, $cause = null)
8692
{
8793
$this->message = $message;
8894
$this->messageTemplate = $messageTemplate;
@@ -93,6 +99,7 @@ public function __construct($message, $messageTemplate, array $parameters, $root
9399
$this->invalidValue = $invalidValue;
94100
$this->constraint = $constraint;
95101
$this->code = $code;
102+
$this->cause = $cause;
96103
}
97104

98105
/**
@@ -197,7 +204,7 @@ public function getInvalidValue()
197204
}
198205

199206
/**
200-
* Returns the constraint that caused the violation.
207+
* Returns the constraint whose validation caused the violation.
201208
*
202209
* @return Constraint|null The constraint or null if it is not known
203210
*/
@@ -206,6 +213,16 @@ public function getConstraint()
206213
return $this->constraint;
207214
}
208215

216+
/**
217+
* Returns the cause of the violation.
218+
*
219+
* @return mixed
220+
*/
221+
public function getCause()
222+
{
223+
return $this->cause;
224+
}
225+
209226
/**
210227
* {@inheritdoc}
211228
*/

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ class ConstraintViolationAssertion
428428
private $plural;
429429
private $code;
430430
private $constraint;
431+
private $cause;
431432

432433
public function __construct(LegacyExecutionContextInterface $context, $message, Constraint $constraint = null, array $assertions = array())
433434
{
@@ -486,6 +487,13 @@ public function setCode($code)
486487
return $this;
487488
}
488489

490+
public function setCause($cause)
491+
{
492+
$this->cause = $cause;
493+
494+
return $this;
495+
}
496+
489497
public function buildNextViolation($message)
490498
{
491499
$assertions = $this->assertions;
@@ -525,7 +533,8 @@ private function getViolation()
525533
$this->invalidValue,
526534
$this->plural,
527535
$this->code,
528-
$this->constraint
536+
$this->constraint,
537+
$this->cause
529538
);
530539
}
531540
}

src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface
8383
*/
8484
private $code;
8585

86+
/**
87+
* @var mixed
88+
*/
89+
private $cause;
90+
8691
public function __construct(ConstraintViolationList $violations, Constraint $constraint, $message, array $parameters, $root, $propertyPath, $invalidValue, TranslatorInterface $translator, $translationDomain = null)
8792
{
8893
$this->violations = $violations;
@@ -166,6 +171,16 @@ public function setCode($code)
166171
return $this;
167172
}
168173

174+
/**
175+
* {@inheritdoc}
176+
*/
177+
public function setCause($cause)
178+
{
179+
$this->cause = $cause;
180+
181+
return $this;
182+
}
183+
169184
/**
170185
* {@inheritdoc}
171186
*/
@@ -203,7 +218,8 @@ public function addViolation()
203218
$this->invalidValue,
204219
$this->plural,
205220
$this->code,
206-
$this->constraint
221+
$this->constraint,
222+
$this->cause
207223
));
208224
}
209225
}

src/Symfony/Component/Validator/Violation/ConstraintViolationBuilderInterface.php

+9
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ public function setPlural($number);
101101
*/
102102
public function setCode($code);
103103

104+
/**
105+
* Sets the cause of the violation.
106+
*
107+
* @param mixed $cause The cause of the violation
108+
*
109+
* @return ConstraintViolationBuilderInterface This builder
110+
*/
111+
public function setCause($cause);
112+
104113
/**
105114
* Adds the violation to the current execution context.
106115
*/

src/Symfony/Component/Validator/Violation/LegacyConstraintViolationBuilder.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Violation;
1313

14-
use Symfony\Component\Translation\TranslatorInterface;
15-
use Symfony\Component\Validator\Constraint;
16-
use Symfony\Component\Validator\ConstraintViolation;
17-
use Symfony\Component\Validator\ConstraintViolationList;
1814
use Symfony\Component\Validator\ExecutionContextInterface;
19-
use Symfony\Component\Validator\Util\PropertyPath;
2015

2116
/**
2217
* Backwards-compatible implementation of {@link ConstraintViolationBuilderInterface}.
@@ -149,6 +144,16 @@ public function setCode($code)
149144
return $this;
150145
}
151146

147+
/**
148+
* {@inheritdoc}
149+
*/
150+
public function setCause($cause)
151+
{
152+
// do nothing - we can't save the cause through the old API
153+
154+
return $this;
155+
}
156+
152157
/**
153158
* {@inheritdoc}
154159
*/

0 commit comments

Comments
 (0)