-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Unique entity custom message #16345
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
Unique entity custom message #16345
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,11 +12,11 @@ | |
namespace Symfony\Bridge\Doctrine\Validator\Constraints; | ||
|
||
use Doctrine\Common\Persistence\ManagerRegistry; | ||
use Symfony\Component\Validator\Context\ExecutionContextInterface; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\PropertyAccess\PropertyAccess; | ||
|
||
/** | ||
* Unique Entity Validator checks if one or a set of fields contain unique values. | ||
|
@@ -128,16 +128,24 @@ public function validate($entity, Constraint $constraint) | |
$errorPath = null !== $constraint->errorPath ? $constraint->errorPath : $fields[0]; | ||
$invalidValue = isset($criteria[$errorPath]) ? $criteria[$errorPath] : $criteria[$fields[0]]; | ||
|
||
if ($this->context instanceof ExecutionContextInterface) { | ||
$this->context->buildViolation($constraint->message) | ||
->atPath($errorPath) | ||
->setInvalidValue($invalidValue) | ||
->addViolation(); | ||
} else { | ||
$this->buildViolation($constraint->message) | ||
->atPath($errorPath) | ||
->setInvalidValue($invalidValue) | ||
->addViolation(); | ||
$parameters = array(); | ||
|
||
if (preg_match_all('/{{ ([a-zA-Z0-9_]+) }}/', $constraint->message, $vars) > 0) { | ||
|
||
if (!class_exists('Symfony\\Component\\PropertyAccess\\PropertyAccess')) { | ||
throw new \RuntimeException('Unable to access entity property as the Symfony PropertyAccess is not installed.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should probably explain why it is necessary There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dupuchba You can use the Doctrine bridge without using the full framework. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, I am not sure if it's widespread but fair enough ! Thx @xabbuh |
||
} | ||
|
||
$accessor = PropertyAccess::createPropertyAccessor(); | ||
|
||
foreach ($vars[1] as $var) { | ||
$parameters[sprintf('{{ %s }}', $var)] = $accessor->getValue($entity, $var); | ||
} | ||
} | ||
|
||
$this->context->buildViolation($constraint->message, $parameters) | ||
->atPath($errorPath) | ||
->setInvalidValue($invalidValue) | ||
->addViolation(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this check is a bad idea: when using translation keys, the params are needed, but they are not part of the key
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stof I don't understand ! Do you have an example please ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ping @stof