Skip to content

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

Closed
@bertrandseurot

Description

@bertrandseurot

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions