-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[Validator] Access container array in Expression/Context #23134
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
Changes from 7 commits
2aa3e7c
c6d83d5
9e0882b
c964fcb
53016a5
1907d8b
209c386
afd2406
26f4505
bd3abc9
5c849fe
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,6 +12,7 @@ | |
| namespace Symfony\Component\Validator\Constraints; | ||
|
|
||
| use Symfony\Component\ExpressionLanguage\ExpressionLanguage; | ||
| use Symfony\Component\PropertyAccess\PropertyAccess; | ||
| use Symfony\Component\Validator\Constraint; | ||
| use Symfony\Component\Validator\ConstraintValidator; | ||
| use Symfony\Component\Validator\Exception\RuntimeException; | ||
|
|
@@ -46,6 +47,10 @@ public function validate($value, Constraint $constraint) | |
| $variables['value'] = $value; | ||
| $variables['this'] = $this->context->getObject(); | ||
|
||
|
|
||
| if (null !== $constraint->dataPath && '' !== $constraint->dataPath) { | ||
|
||
| $variables['this'] = $this->getPropertyAccessor()->getValue($this->context, $constraint->dataPath); | ||
|
||
| } | ||
|
|
||
| if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) { | ||
| $this->context->buildViolation($constraint->message) | ||
| ->setParameter('{{ value }}', $this->formatValue($value)) | ||
|
|
@@ -65,4 +70,13 @@ private function getExpressionLanguage() | |
|
|
||
| return $this->expressionLanguage; | ||
| } | ||
|
|
||
| private function getPropertyAccessor() | ||
|
||
| { | ||
| if (!class_exists('Symfony\Component\PropertyAccess\PropertyAccess')) { | ||
|
||
| throw new RuntimeException('Unable to use expressions with data path as the Symfony PropertyAccess component is not installed.'); | ||
|
||
| } | ||
|
|
||
| return PropertyAccess::createPropertyAccessor(); | ||
|
||
| } | ||
| } | ||
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.
another
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" should be enclosed with backticks