Skip to content

ConstraintViolationBuilder and ExecutionContext - unable to set propertyPath #11072

Closed
@denisvmedia

Description

@denisvmedia

I've upgraded from Symfony 2.3 to Symfony 2.5 and encountered a problem.

Before I had the following code (entity with: Assert\Callback(methods={"isEmailCollectionValid", "isCompanySelected"}))

    // this worked fine in Symfony 2.3 but stopped working properly in 2.5
    public function isEmailCollectionValid(ExecutionContextInterface $context)
    {
        foreach ($this->getEmails() as $id => $email) {
            $context->validateValue($email->getValue(), array(
                new Assert\Email(array(
                    'message' => "'{{ value }}' is not a valid email.",
                ))
            ), 'emails['.$id.'].value');
        }
    }

Now I have the following code:

    // this doesn't work properly (at least as I expect) in 2.5
    public function isEmailCollectionValid(ExecutionContextInterface $context)
    {
        $validator = $context->getValidator();
        $contextValidator = $validator->inContext($context);

        foreach ($this->getEmails() as $id => $email) {
            $contextValidator->atPath('emails['.$id.'].value')->validate($email->getValue(), array(
                new Assert\Email(array(
                    'message' => "'{{ value }}' is not a valid email.",
                ))
            ));
        }
    }

I'm unable to add all the violations to the correspondent fields. Instead the path is being appended (i.e.: 1. "data.emails[0].value", 2. "data.emails[0].emails[1].value", and so on). And it seems (according to the sources), no way exists to overcome it. Older code works the same as the newer one in 2.5.

Moreover, if I have more validators in the same entity (in this entity I have two of them), then the first error path is appended to the second one, which also produces wrong error path.

Is it a bug or how can I fix it? Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions