Skip to content

[Validator] Made it possible to store the cause of a constraint violation #12052

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
Sep 30, 2014
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ CHANGELOG
* deprecated `ClassMetadata::addMemberMetadata()`
* [BC BREAK] added `Mapping\MetadataInterface::getConstraints()`
* added generic "payload" option to all constraints for attaching domain-specific data
* [BC BREAK] added `ConstraintViolationBuilderInterface::setCause()`

2.5.0
-----
Expand Down
25 changes: 21 additions & 4 deletions src/Symfony/Component/Validator/ConstraintViolation.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ class ConstraintViolation implements ConstraintViolationInterface
*/
private $code;

/**
* @var mixed
*/
private $cause;

/**
* Creates a new constraint violation.
*
Expand All @@ -79,10 +84,11 @@ class ConstraintViolation implements ConstraintViolationInterface
* @param int|null $plural The number for determining the plural
* form when translating the message
* @param mixed $code The error code of the violation
* @param Constraint|null $constraint The constraint that caused the
* violation
* @param Constraint|null $constraint The constraint whose validation
* caused the violation
* @param mixed $cause The cause of the violation
*/
public function __construct($message, $messageTemplate, array $parameters, $root, $propertyPath, $invalidValue, $plural = null, $code = null, Constraint $constraint = null)
public function __construct($message, $messageTemplate, array $parameters, $root, $propertyPath, $invalidValue, $plural = null, $code = null, Constraint $constraint = null, $cause = null)
{
$this->message = $message;
$this->messageTemplate = $messageTemplate;
Expand All @@ -93,6 +99,7 @@ public function __construct($message, $messageTemplate, array $parameters, $root
$this->invalidValue = $invalidValue;
$this->constraint = $constraint;
$this->code = $code;
$this->cause = $cause;
}

/**
Expand Down Expand Up @@ -197,7 +204,7 @@ public function getInvalidValue()
}

/**
* Returns the constraint that caused the violation.
* Returns the constraint whose validation caused the violation.
*
* @return Constraint|null The constraint or null if it is not known
*/
Expand All @@ -206,6 +213,16 @@ public function getConstraint()
return $this->constraint;
}

/**
* Returns the cause of the violation.
*
* @return mixed
*/
public function getCause()
{
return $this->cause;
}

/**
* {@inheritdoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ class ConstraintViolationAssertion
private $plural;
private $code;
private $constraint;
private $cause;

public function __construct(LegacyExecutionContextInterface $context, $message, Constraint $constraint = null, array $assertions = array())
{
Expand Down Expand Up @@ -486,6 +487,13 @@ public function setCode($code)
return $this;
}

public function setCause($cause)
{
$this->cause = $cause;

return $this;
}

public function buildNextViolation($message)
{
$assertions = $this->assertions;
Expand Down Expand Up @@ -525,7 +533,8 @@ private function getViolation()
$this->invalidValue,
$this->plural,
$this->code,
$this->constraint
$this->constraint,
$this->cause
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface
*/
private $code;

/**
* @var mixed
*/
private $cause;

public function __construct(ConstraintViolationList $violations, Constraint $constraint, $message, array $parameters, $root, $propertyPath, $invalidValue, TranslatorInterface $translator, $translationDomain = null)
{
$this->violations = $violations;
Expand Down Expand Up @@ -166,6 +171,16 @@ public function setCode($code)
return $this;
}

/**
* {@inheritdoc}
*/
public function setCause($cause)
{
$this->cause = $cause;

return $this;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -203,7 +218,8 @@ public function addViolation()
$this->invalidValue,
$this->plural,
$this->code,
$this->constraint
$this->constraint,
$this->cause
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ public function setPlural($number);
*/
public function setCode($code);

/**
* Sets the cause of the violation.
*
* @param mixed $cause The cause of the violation
*
* @return ConstraintViolationBuilderInterface This builder
*/
public function setCause($cause);

/**
* Adds the violation to the current execution context.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@

namespace Symfony\Component\Validator\Violation;

use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\ExecutionContextInterface;
use Symfony\Component\Validator\Util\PropertyPath;

/**
* Backwards-compatible implementation of {@link ConstraintViolationBuilderInterface}.
Expand Down Expand Up @@ -149,6 +144,16 @@ public function setCode($code)
return $this;
}

/**
* {@inheritdoc}
*/
public function setCause($cause)
{
// do nothing - we can't save the cause through the old API

return $this;
}

/**
* {@inheritdoc}
*/
Expand Down