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) %}
.