Skip to content

[Form] property path for violations is incorrect #3995

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
Burgov opened this issue Apr 19, 2012 · 8 comments
Closed

[Form] property path for violations is incorrect #3995

Burgov opened this issue Apr 19, 2012 · 8 comments
Labels
Form Good first issue Ideal for your first contribution! (some Symfony experience may be required)

Comments

@Burgov
Copy link
Contributor

Burgov commented Apr 19, 2012

I have the following situation:

There is a form which shows a list of entities of some kind, kind of similar to a collection form. However, the entities need to be shown in some kind of hierarchy, so even though the app Data is a plain array, the clientData and form it self are built in the following way:

form:
  Mon:
    0: entity
    1: entity
  Tue:
    0: entity

etc...

The transformation between the two is done using a DataTransformer on the topmost form: https://gist.github.com/2419554

The form works fine except for validation, for some reason all errors bubble up all the way to the top form, even if they should display on the bottom forms (form.Mon.0, form.Mon.1, etc). While trying to find out what was wrong, I came to the conclusion that the property path of the violations was incorrect, or at least not conform to the way the form was build up:

data[0].field
data[0].field2
data[1].field

rather than

data[Mon][0].field
data[Mon][0].field2
data[Mon][1].field

This causes them to bubble all the way up, as they cannot be mapped to the form.

@webmozart
Copy link
Contributor

When the DelegatingValidationListener is refactored (see #3903), this can be implemented. What you want is basically a custom mapping function that maps the property paths

data[0]
data[1]

to the forms

children[Mon].children[0]
children[Mon].children[1]

A second solution for your problem is to make the forms Mon, Tue etc. virtual. Virtual forms are used for presentation only and are not mapped directly to the domain model, resulting in the mapping:

data[0] <-> children[Mon].children[0]
data[1] <-> children[Mon].children[1]
data[2] <-> children[Tue].children[2]

To do this, you would have to move your value transformer logic to a form listener that rebuilds the form every time your data is set.

@Burgov
Copy link
Contributor Author

Burgov commented Apr 20, 2012

Thanks for your explanation. For now I'll keep it as-is and wait for #3903 to be resolved. The form between the base and the entity forms also contain quite some logic which would probably result in a lot of refactoring and possibly unclean code. I'll resort to the virtual solution if I'll find myself running out of time before the issue is resolved

@webmozart
Copy link
Contributor

Can you please verify if this is fixed by #4341?

@Burgov
Copy link
Contributor Author

Burgov commented May 20, 2012

@bschussek it appears the bug still exists. The error is still mapped to "data[2].break" rather than "data[Wed][0].break" (or something similar)

Anything I can do to help you debug?

@webmozart
Copy link
Contributor

I think that the mapping function I described above is not really feasible. I rather think now that your use case is exactly the use case of virtual forms.

Could you test whether virtual forms fix your problem?

@Burgov
Copy link
Contributor Author

Burgov commented May 21, 2012

@bschussek When the deadline approached a while ago, I implemented the work-a-round you provided around a month ago, and this indeed worked fine.

Does this mean that the way the form was before, is actually not intended use?

@webmozart
Copy link
Contributor

Yes, forms are not intended to be used like that. A form that does not map to a property of the underlying data should either be unmapped ("property_path" => false, or "mapped" => false as of #4341) or virtual ("virtual" => true).

@webmozart
Copy link
Contributor

See also symfony/symfony-docs#1387

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Form Good first issue Ideal for your first contribution! (some Symfony experience may be required)
Projects
None yet
Development

No branches or pull requests

2 participants