Pass custom value to each input in Symfony form (type is choice)

bassplayer7

I'm trying to figure out the best approach to this problem. I am querying a database to get a list of values which I am using to create a form field. In reality, though, it will be creating about 12 <input type="checkmark" /> tags. I need to provide an image URL, which is also from the database, to each checkmark (really the label associated with it).

This seems like it would be a piece of cake if I was rendering each <input /> individually by simply creating a Form Extension. I'm not building them individually, and it doesn't seem right to loop through an array (which Propel would provide) to build as many checkboxes as needed even if I was.

The Propel model form field type is easy to use, but will pass the image column data right into the label which is entirely unpractical. It makes the label into a string that's something like (exploding the string also doesn't seem a good option):

Label => "
    ID: 2
    Title: Label of Checkmark
    Parent ID: null
    Image: ek9dkB.jpeg" 

So what would the best way to pass a parameter to each child so I can access it in my fields.html.twig?

Relevant Form Builder (PictureCheckbox is my custom field type so it will render a custom Twig form template):

$builder->add('skills', new PictureCheckbox(), array(
      'choices'     => $choices,
      'required'    => true,
      'label'       => 'What are your skills?',
      'multiple'    => true,
      'expanded'    => true,
    ));

I've used the model type as well above, which is very similar in style, but the idea is the same.

Here's the field.html.twig where I've created a new form type. {% block picture_checkbox_widget %}

    {% if expanded %}
        {% for child in form.children %}
            {% spaceless %}
            <label for="{{ id }}">
                {# Most of this would change, but is here for example: #}
                {{ form_label(child.label) }}
                    {% set file = path.from.child %}
                    <img src="{{ asset('uploads' ~ file) }}" alt="Test Image" />
            </label>
            {% endspaceless %}
        {% endfor %}
            <input type="checkbox" id="{{ id }}"{{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />
    {% endif %}
{% endblock %}

I am using Symfony 2(.6) with Propel as the ORM. I'd be happy to add info, if I missed something relevant.

Perhaps the choice_list is my answer? But it seems there would be a way to add a variable to the list I see when I dump the child: {% dump(child) %}.

bassplayer7

I believe I have found the answer. I built a ChoiceList and added the image and title to that. I'm sure there is a better way, but it seems to work well, and seems logical from the standpoint that the image is in the <label /> anyway so delivering it as part of the label is natural.

I'll try to update with specifics when I have more time.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Symfony set value checked on a form type choice

From Dev

Symfony 2.3 pass custom data to entity form, using choice or other type

From Dev

Symfony Form EntityType, add custom data to choice

From Dev

Symfony creating choice from entity in form type

From Dev

Allow all value on choice field type in Symfony2 form builder

From Dev

symfony custom form type with assets

From Dev

field array type in entity for form choice type field symfony

From Dev

Manipulate the list of countries provided by Symfony Intl and the form choice type

From Dev

Manipulate the list of countries provided by Symfony Intl and the form choice type

From Dev

How to pass custom options to a Symfony form

From Dev

Symfony. How do I pass parameters to Custom Form Field Type?

From Dev

Use a custom constraint/validator in symfony form type

From Dev

Symfony2 Custom Form Type or Extension

From Dev

Use a custom constraint/validator in symfony form type

From Dev

Symfony 2.8 form entity type custom property

From Dev

Custom select in a form type in Symfony3

From Dev

Symfony - pass custom data from Form Builder to form theme

From Dev

Symfony form choice disable translator

From Dev

Add a percentage (%) symbol to input value, but not pass with form

From Dev

Pass input value whcih is outside of the form on submit

From Dev

Boolean values and choice symfony type

From Dev

Symfony Choice type with disabled options

From Dev

Symfony Choice type with disabled options

From Dev

Symfony/FOS - pass the user id variable to a form Type

From Dev

Map some input value to a custom type

From Dev

Symfony2 : set default value from database in radio buttons choice form?

From Dev

Symfony2 - Expanded choice (form) does not set default value(s)

From Dev

How to override Symfony form MoneyType input as type="number"?

From Dev

How to add a button input type in a form with Symfony2.1

Related Related

  1. 1

    Symfony set value checked on a form type choice

  2. 2

    Symfony 2.3 pass custom data to entity form, using choice or other type

  3. 3

    Symfony Form EntityType, add custom data to choice

  4. 4

    Symfony creating choice from entity in form type

  5. 5

    Allow all value on choice field type in Symfony2 form builder

  6. 6

    symfony custom form type with assets

  7. 7

    field array type in entity for form choice type field symfony

  8. 8

    Manipulate the list of countries provided by Symfony Intl and the form choice type

  9. 9

    Manipulate the list of countries provided by Symfony Intl and the form choice type

  10. 10

    How to pass custom options to a Symfony form

  11. 11

    Symfony. How do I pass parameters to Custom Form Field Type?

  12. 12

    Use a custom constraint/validator in symfony form type

  13. 13

    Symfony2 Custom Form Type or Extension

  14. 14

    Use a custom constraint/validator in symfony form type

  15. 15

    Symfony 2.8 form entity type custom property

  16. 16

    Custom select in a form type in Symfony3

  17. 17

    Symfony - pass custom data from Form Builder to form theme

  18. 18

    Symfony form choice disable translator

  19. 19

    Add a percentage (%) symbol to input value, but not pass with form

  20. 20

    Pass input value whcih is outside of the form on submit

  21. 21

    Boolean values and choice symfony type

  22. 22

    Symfony Choice type with disabled options

  23. 23

    Symfony Choice type with disabled options

  24. 24

    Symfony/FOS - pass the user id variable to a form Type

  25. 25

    Map some input value to a custom type

  26. 26

    Symfony2 : set default value from database in radio buttons choice form?

  27. 27

    Symfony2 - Expanded choice (form) does not set default value(s)

  28. 28

    How to override Symfony form MoneyType input as type="number"?

  29. 29

    How to add a button input type in a form with Symfony2.1

HotTag

Archive