Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8754581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:39:18+00:00 2026-06-13T13:39:18+00:00

I have an entity with both a name (string) and a file (also a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:39:20+00:00Added an answer on June 13, 2026 at 1:39 pm

    Not sure it is the best way but here is how I manage this kind of situation :

    1. create a new formtype that behalf as entityType, IconCheckType for instance:
      (http://symfony.com/doc/master/cookbook/form/create_custom_field_type.html)

      namespace .....\Form\Type;
      
      use Symfony\Component\Form\AbstractType;
      use Symfony\Component\Form\FormBuilder;
      use Symfony\Component\Form\FormView;
      use Symfony\Component\Form\FormInterface;
      
      
      class IconCheckType extends AbstractType
      {
         /**
           * {@inheritdoc}
           */
        public function buildForm(FormBuilder $builder, array $options) {
      
          $builder -> setAttribute('dataType', $options['dataType']);
        }
      
         /**
           * {@inheritdoc}
           */
        public function buildView(FormView $view, FormInterface $form) {
          $view -> set('dataType', $form -> getAttribute('dataType'));
        }
      
         /**
           * {@inheritdoc}
           */
        public function getDefaultOptions(array $options) {
          return array('required' => false,'dataType'=>'entity');
        }
      
      
        /**
           * Returns the allowed option values for each option (if any).
           *
           * @param array $options
           *
           * @return array The allowed option values
           */
          public function getAllowedOptionValues(array $options)
          {
              return array('required' => array(false));
          }
      
         /**
           * {@inheritdoc}
           */
        public function getParent(array $options) {
          return 'entity';
        }
      
         /**
           * {@inheritdoc}
           */
        public function getName() {
          return 'iconcheck';
        }
      
      }
      
    2. in your Form

      ...
      ->add('icon', 'iconcheck', array(
              'class' => 'CroltsMainBundle:Icon',
              'property'=>'formField',
              'multiple'=>false,
              'expanded'=>true
            ))
      ...
      

      Note the property=>'formField', that means that instead of return the __toString as label it will return whatever you want from the function getFormField from your entity class

    3. So, in your entity class:

      class Icon {
      
      ....
          public function getFormField() {  
             return $this;   /* or an array with only the needed attributes */ 
          }
      
      ....
      }
      
    4. then you can render your custom field

      {% block iconcheck_widget %}
         {% for child in form %}
            {% set obj=child.vars.label %}
              <div>
                  <label for="something"><img src="/icons/{{obj.file }}" />{{obj.name}}</label>
                  {{ form_widget(child) }} {# the radio/checkbox #}
                </div>
            {{ form_widget(child) }}#}
          {% endfor %}
      
      
      {% endblock %}
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a Gift entity, this entity has a sender, and a receiver...both from
I have entity e.g. Product which aggregates other entities such as Category . Those
I have an asp.net MVC application. There is an entity called File that it
Say I have an entity as follows: Restaurant Id : int Name : string
I have Entity Framework entities Events which have an EntityCollection of RSVP. I want
I have entity model like this (using EclipseLink and JPA 2.0): @Entity class A
I have an Entity which holds a collection of OtherEntity in a many-to-many style
I have a noticication entity that has OneToMany relationship with its parameters, which is
I have a customer class which has both PhoneNumber and Email properties. Using DataAnnotations
Using EF 4.0. I have two entity models, both named CoreEntities, but in different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.