Skip to content

[Validator] cannot use "this" in an Expression constraint when embeded in AtLeastOneOf #39145

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
bertrandseurot opened this issue Nov 23, 2020 · 3 comments

Comments

@bertrandseurot
Copy link

Symfony version(s) affected: 5.1

Description

An exception 'Unable to call method of a non-object.' is thrown by Symfony\Component\ExpressionLanguage\Node\GetAttrNode when using this keyword in an Expression constraint when embeded in AtLeastOneOf

How to reproduce

When validating an entity with this kind of constraint:

    /**
     * @Assert\AtLeastOneOf({
     *    @Assert\Expression("this.getCategory() in ['php', 'symfony']"),
     *    @Assert\IdenticalTo("Baz")
     * })
     */
    private $foo;

Since the AtLeastOneOfValidator validates the sub-constraint without context, $variables['this'] = $this->context->getObject(); in Symfony\Component\Validator\Constraints\ExpressionValidator results in a null value

@xabbuh
Copy link
Member

xabbuh commented Jan 4, 2021

Status: Reviewed

@xabbuh
Copy link
Member

xabbuh commented Jan 19, 2021

see #39872

nicolas-grekas added a commit that referenced this issue Jan 26, 2021
… constraints (xabbuh)

This PR was merged into the 5.1 branch.

Discussion
----------

[Validator] propagate the object being validated to nested constraints

| Q             | A
| ------------- | ---
| Branch?       | 5.1
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #39145
| License       | MIT
| Doc PR        |

Commits
-------

8c1bac9 propagate the object being validated to sub-constraints
@bertrandseurot
Copy link
Author

Thanks @xabbuh !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants