Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Form] Attatching an AbstractComparisonValidator to FormType contains the Form object and throws exception #28987

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
gmponos opened this issue Oct 26, 2018 · 6 comments

Comments

@gmponos
Copy link
Contributor

gmponos commented Oct 26, 2018

Symfony version(s) affected: 4.1.x

Description
When I attach to an object of mine through Yaml or annotation one of the EqualsTo/NotEqualsTo/IdenticalTo/NotIdenticalTo constraints then this line returns the object that the constraint is attached to.

App\Form\Transfer:
    properties:
        from:
            - NotIdenticalTo:
                propertyPath: 'to'

So the line above will return the object App\Form\Transfer

If I add the constraint on a form type like this:

        $builder
            ->add('from', ChoiceType::class, [
                'constraints' => [
                    new NotIdenticalTo(['propertyPath' => 'to']),
                ],
            ])

The line above will bring back a form object.

This causes the constraint to throw an exception because it reaches on this line and in the Form object the path of the property it does not exist.

How to reproduce

Build a form in the way described above and submit it.

@linaori
Copy link
Contributor

linaori commented Oct 26, 2018

Setting a constraint via the form builder is not the same as setting a constraint on the data object. Not sure if this is a bug, but their behavior is different.

@HeahDude
Copy link
Contributor

Thanks for reporting this @gmponos.

I will investigate on this, but to quickly fix your problem the workaround is to use the property path data.to when adding the constraint in the type.

@gmponos
Copy link
Contributor Author

gmponos commented Oct 26, 2018

@HeahDude thanks in advance. If this should behave differently then I will submit a PR to docs suggesting a notice about this and the work around you mention. For me I believe that constraints should behave the same regardless if they are defined through yaml/annotation/php/form.

@xabbuh
Copy link
Member

xabbuh commented Oct 27, 2018

This looks expected to me. When using the constraints option of the form type, the constraint will be associated with the form and thus property paths must be given according to the structure of the form as @HeahDude said. In your first example, you are attaching the constraint to your model.

@xabbuh
Copy link
Member

xabbuh commented Oct 31, 2018

I am closing as explained

@xabbuh xabbuh closed this as completed Oct 31, 2018
@gmponos
Copy link
Contributor Author

gmponos commented Nov 8, 2018

Hello,

My explanation was wrong

The line above will bring back a form object.

Basically if I attach a constraint to the field I have only the field visible so if I do something like this:

        $builder
            ->add('from', ChoiceType::class, [
                'constraints' => [
                    new NotIdenticalTo(['propertyPath' => 'data.to']),
                ],
            ])

I get the following error:

PropertyAccessor requires a graph of objects or arrays to operate on, but it found type "string" while trying to traverse path "data.to" at property "to".

If I add the constraint to the form I do not have access to both parts of the comparison

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver
            ->setDefault('constraints', function (Options $options) {
                return [
                    new NotIdenticalTo([
                        'value' => '', // what?
                        'propertyPath' => 'data.to'
                    ]),
            })
    }

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

6 participants