8000 [Form] Access underlying data of choices · Issue #4171 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Access underlying data of choices #4171

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
webmozart opened this issue May 1, 2012 · 2 comments
Closed

[Form] Access underlying data of choices #4171

webmozart opened this issue May 1, 2012 · 2 comments

Comments

@webmozart
Copy link
Contributor

One can't curently access the underlying data of choices. This should be possible though to make further calculations in the templates possible.

Assume that the choices are a set of objects with a getId() and a getName() method. Then the choice values can be configured to point to $object->getId() and the choice labels can be configured to point to $object->getName(). In the template, these values can be retrieved in the following way:

{# expanded=true #}
{% for child in form %}
    Value (ID): {{ child.vars.value }}
    Label (Name): {{ child.vars.label }}
{% endfor %}

{# expanded=false #}
{% for choice in choices %}
    Value (ID): {{ choice.value }}
    Label (Name): {{ choice.label }}
{% endfor %}

It is however not possible to access the $object instance itself. It would be desirable to make this possible, which can be done using the following approach:

  • add a "data" field to ChoiceView that contains the original choice, i.e. $object in our case
  • add a "form" field to ChoiceView that contains the form representing the choice (only relevant if expanded=true)

The template can then be handled in a more uniform manner (and more independent of expanded/collapsed). Furthermore, $object can be accessed.

{# expanded=true #}
{% for choice in choices %}
    Value (ID): {{ choice.value }}
    Label (Name): {{ choice.label }}
    Full Object: {{ choice.data }}
    Checkbox/Radio: {{ form_widget(choice.form) }}
{% endfor %}

{# expanded=false #}
{% for choice in choices %}
    Value (ID): {{ choice.value }}
    Label (Name): {{ choice.label }}
    Full Object: {{ choice.data }}
{% endfor %}
@webmozart
Copy link
Contributor Author

Depends on #4067

@webmozart
Copy link
Contributor Author

The "form" property cannot be added to ChoiceView, otherwise caching of the ChoiceView objects (as is currently the case) is impossible.

fabpot added a commit that referenced this issue Jul 25, 2012
Commits
-------

686bf6b [Form] Made original data of a form and choices accessible in templates

Discussion
----------

[Form] Made original data of a form and choices accessible in templates

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: #4171
Todo: -

Now you can access the normalized data of a form in the template:
```
form.vars.data
```
You can also access the original data of a choice, for example the entities in an entity type:
```
choice.data
```
@fabpot fabpot closed this as completed Jul 25, 2012
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

2 participants
0