-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[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(); | ||
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. to improve readability I would make this line an |
||
|
||
if (null !== $constraint->dataPath && '' !== $constraint->dataPath) { | ||
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.
|
||
$variables['this'] = $this->getPropertyAccessor()->getValue($this->context, $constraint->dataPath); | ||
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. if 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. In the other hand, if Symfony deciders decides to make it configurable I think we could start the path from the $this->getPropertyAccessor()->getValue($this->context->getRoot(), $constraint->dataPath); else, I suggest pass the if (null === $variables['this'] = $this->context->getObject()) {
$variables['this'] = $this->context->getRoot();
} thus, you don't have to worry about the "dataPath", you know the data linked to the root, therefore you just have to access them: new Expression(array('expression' => 'value <= this["data"]["dateEnd"]')); wdyt? < 8000 details-menu class="dropdown-menu dropdown-menu-sw show-more-popover color-fg-default" style="width:185px" src="" preload >
Contributor
Author
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. I like the idea, to pass the |
||
} | ||
|
||
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() | ||
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. IMO we don't need this method anymore and can inline the |
||
{ | ||
6D40 | if (!class_exists('Symfony\Component\PropertyAccess\PropertyAccess')) { | |
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. we should use the |
||
throw new RuntimeException('Unable to use expressions with data path as the Symfony PropertyAccess component 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. I wonder if we should not throw this exception in the constraint to allow to detect this mistake earlier. 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. Of course we could throw it there. But than we need to check there if 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. I think that would be fine. |
||
} | ||
|
||
return PropertyAccess::createPropertyAccessor(); | ||
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. the exception in src/Symfony/Component/Validator/Constraints/Expression.php should be moved here IMHO: 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. I would rather make the property 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. I made the property |
||
} | ||
} |
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