I need help with this since I just can’t figure it out by myself even after reading and examining all resources I found on the internet.
I have an Image entity. It has 3 mapped properties.
- id
- location
- thumb_location
And I have custom ImageSelectType form type which extends AbstractType:
buildForm function looks like this:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$em = $options['em'];
$qb = $em->createQueryBuilder();
$result = $qb->select('i')
->from('BloggerBlogBundle:Image', 'i')
->leftJoin('i.articles', 'a')
->where('a is NULL')
;
$builder
->add('images', 'entity', array(
'class' => 'BloggerBlogBundle:Image',
'query_builder' => $result,
'required' => true,
'multiple' => false,
'expanded' => true,
)
)
->setAttribute('widget', 'imageSelect')
;
}
So it takes all images that aren’t used by any article and populates form with them. With these options, I get radiobuttons, like this:
<div id="image_images">
<input type="radio" id="image_images_67" name="image[images]" required="required" value="67"><label for="image_images_67" class="required">c5252b4ffc9c50540218e25be1353b33aaa4ee05.png</label>
<input type="radio" id="image_images_68" name="image[images]" required="required" value="68"><label for="image_images_68" class="required">fcfc7d7d05d63b1f55dff8cbff0bedeb3c917dfc.jpeg</label>
</div>
What I want now is all radiobuttons to have custom html5 data attribute data-thumb="thumbnail/location.png" which would be the thumb_location property value of image object represented by that radiobutton.
I hope I was clear enough. If any more info is needed I will provide it.
I’ve read so much about this but I think I’m imagining things more complex then they actually are. At one point I just wanted to say ‘Oh, forget it, I’ll just render this manually’ and use:
{% for choice in form.vars.choices %}
<input type="radio" data-thumb="{{choice.thumb_location}}" />
{% endfor %}
But I really want to use good practices that this amazing framework provides….Sometimes just don’t seem as obvious to me as they should.
Ah, I thought this was relating to this object as the FormType is. So in this case I see the possibility to set a variable withe the buildView method.
You can set this option whether in the ->add(x, x, array(‘thumb_location’ => ‘location’))
(or in the setDefaultOptions method).
But now I am not sure to be honest. Because you would need to set in the view like this:
And this means you make
<input data...by hand again.Not sure if this is possible in your case but I think it would be nice to define a second FormType for the radio input field: