8000 [Validator] Access container array in Expression/Context by Engerim · Pull Request #23134 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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

Closed
wants to merge 11 commits into from
Closed
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 @@ -11,6 +11,7 @@ CHANGELOG
the `Url::CHECK_DNS_TYPE_*` constants values and will throw an exception in Symfony 4.0
* added min/max amount of pixels check to `Image` constraint via `minPixels` and `maxPixels`
* added a new "propertyPath" option to comparison constraints in order to get the value to compare from an array or object
* added a `dataPath` option to the `Expression` constraint to allow an other way to get `this` from the context

3.3.0
-----
Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\RuntimeException;

/**
* @Annotation
Expand All @@ -30,6 +32,28 @@ class Expression extends Constraint

public $message = 'This value is not valid.';
public $expression;
protected $dataPath;

/**
* {@inheritdoc}
*/
public function __construct($options = null)
{
parent::__construct($options);

if ($this->dataPath && !class_exists(PropertyAccess::class)) {
throw new RuntimeException('Unable to use expressions with data path as the Symfony PropertyAccess component is not installed.');
}
}

public function __get($option)
{
if ('dataPath' === $option) {
return $this->dataPath;
}

return parent::__get($option);
}

/**
* {@inheritdoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,7 +45,12 @@ public function validate($value, Constraint $constraint)

$variables = array();
$variables['value'] = $value;
$variables['this'] = $this->context->getObject();

if ($constraint->dataPath) {
$variables['this'] = PropertyAccess::createPropertyAccessor()->getValue($this->context, $constraint->dataPath);
Copy link
Member
@yceruto yceruto Oct 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still I think we can get rid of dataPath altogether and set the root context directly for this case. Even, if the root is a form instance then we could set the form data.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you do that if the data is not available at the time you configure the constraint (which by the way is almost always the case when not using PHP to set up your constraints)?

8000

Copy link
Member
@yceruto yceruto Oct 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure to understand your question :( but in short this is what I meant:

if (null === $variables['this'] = $this->context->getObject()) {
    if (($root = $this->context->getRoot()) instanceof FormInterface) {
        $variables['this'] = $root->getData();
    } else {
        $variables['this'] = $root;
    }
}

This will work with the example of the PR description and even for cases where there is data_class and the expression is setting over form fields.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this would force the Validator component to know something about the Form component. I don't think that's a good idea. There may also be other similar issues (not related to Symfony) which we couldn't fix here.

Copy link
Member
@yceruto yceruto Oct 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, then just:

$variables['this'] = $this->context->getObject() ?: $this->context->getRoot();

?

Copy link
Member
@yceruto yceruto Oct 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO there is no need to configure a dataPath if we have already a data root that we know, why not start from it directly?

} else {
$variables['this'] = $this->context->getObject();
}

if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
$this->context->buildViolation($constraint->message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,17 @@ public function testExpressionLanguageUsage()

$this->assertTrue($used, 'Failed asserting that custom ExpressionLanguage instance is used.');
}

public function testExpressionWithCustomDataPath()
{
$constraint = new Expression(array('expression' => 'value <= this["dateEnd"]', 'dataPath' => 'root[data]'));

$this->setRoot(array('data' => array('dateEnd' => '2011-06-07', 'dateStart' => '2011-06-05')));
$this->setPropertyPath('');
$this->setProperty(null, 'property');

$this->validator->validate('2011-06-05', $constraint);

$this->assertNoViolation();
}
}
0