I have an entity with both a name (string) and a file (also a string representing the filename). This is the “Icon” entity.
I have another entity called “Category” which has a name (string) and a relation to an Icon (OneToMany). I want the form to allow a user to select an Icon for a Category.
So I could display it in the form as:
$builder->add('icon', 'entity', array(
'class' => 'CroltsMainBundle:Icon',
'expanded' => true,
'multiple' => false
));
But what I really want is to display something like this in twig for each radio button:
<div>
<label for="something"><img src="/icons/{{icon.file }}" />{{icon.name}}</label>
<input type="radio" name="something" value="{{ icon.id }}" />
</div>
Is there a good way to make that type of radio form with Symfony forms? Like would a custom Type be what I want? I really haven’t done too much with custom types to know how much of this is possible.
Not sure it is the best way but here is how I manage this kind of situation :
create a new formtype that behalf as
entityType,IconCheckTypefor instance:(http://symfony.com/doc/master/cookbook/form/create_custom_field_type.html)
in your Form
Note the
property=>'formField', that means that instead of return the__toStringas label it will return whatever you want from the function getFormField from your entity classSo, in your entity class:
then you can render your custom field