As I know, I can use choice type in form fields of config.yml as below
- { property: 'sendstyle', label: 'product.sendStyle', type: 'choice', type_options: {choices: {'text1': 1, 'text2': 2, 'text3': 3}} }
But it's not work in the list, how can I do it, or how to transform the value to specific string in the list as my need ?
I use this in a backend, but I put the choice values in the opposite order. MAybe that's the issue here?
- { property: 'sendstyle', ..., type_options: { choices: { 1: 'text1', 2: 'text2', 3: 'text3' }} }
I'm sorry but I didn't read this issue description right. You want to display a form widget (ChoiceType) in a list view? If that's your need, I'm afraid you can't do that natively. You can do it with a custom template fragment (as explained here: https://symfony.com/doc/current/bundles/EasyAdminBundle/book/list-search-show-configuration.html#customizing-the-template-used-to-render-each-property-type) but I don't think it makes sense to display a form widget (something to modify information) in the lsit view (which only display information and doesn't change it).
I'm closing this as "fixed", but if you disagree, please reopen. Thanks!
I don't think that's what @TonyGao meant. I'm running into the same problem:
I want to use the human readable version of the property in the list, by using:
- { property: 'sendstyle', label: 'product.sendStyle', type: 'choice', type_options: {choices: {'text1': 1, 'text2': 2, 'text3': 3}} }
If I use the above, the list either shows an empty field or nothing at all, not "text1" for example.
If I just use the following:
- { property: 'sendstyle', label: 'product.sendStyle'}
I just get "1" in my list, I want it to say "text1"
Same here, I just get database value instead of label, anyone found a solution?
Same for me... Still no solution yet?
I need this functionality too.
You can create a template for this:
{% set hasValue = false %}
{% spaceless %}
{% if value is iterable %}
{% for val in value %}
{% for key,choice in field_options.type_options.choices %}
{% if choice == val %}
{% if not hasValue %}
<ul>
{% set hasValue = true %}
{% endif %}
<li>{{ key }}</li>
{% endif %}
{% endfor %}
{% endfor %}
{% if hasValue %}
</ul>
{% endif %}
{% else %}
{% for key,choice in field_options.type_options.choices %}
{% if choice == value %}
{% set hasValue = true %}
{{ key }}
{% endif %}
{% endfor %}
{% endif %}
{% if not hasValue %}
{{ include(entity_config.templates.label_null) }}
{% endif %}
{% endspaceless %}
Most helpful comment
Same here, I just get database value instead of label, anyone found a solution?