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

  • Home
  • SEARCH
  • 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 7025299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:57:22+00:00 2026-05-27T23:57:22+00:00

I have an entity ,there is a array type attribute:productKey.I try to add a

  • 0

I have an entity ,there is a array type attribute:productKey.I try to add a choice typy to the form to show the productKeys,I wrote codes:

1.my formType:

public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
                ->add('heading','text',array('label'=>'title'))
                ->add('productKey','choice',array(
                    'required'=>TRUE,
                    'label'=>'choose your product',
                ));
    }

2.In my Product entity the productKey is defined:

 /**
         * @var array $productKey
         *
         * @ORM\Column(name="productKey", type="array",nullable=true)
         */
        private $productKey;

3.In my controller:

$entity = new Product();
$productKey = array("1"=>"one","2"=>"two","3"=>"three");
$entity ->setProductKey($productKey);
$formType = new TicketType($productKey);
$form = $this->createForm($formType,$entity);
return array(
     'form'=>$form->createView(),
     'entity'=>$entity
 );

when I run my project ,the value of productKey can not be listed ! it just appear an empty select chice input.
How can i solve it?

  • 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-05-27T23:57:22+00:00Added an answer on May 27, 2026 at 11:57 pm

    You need to specify the choices using the choices option in the form type:

    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('productKey', 'choice', array(
                'choices' => array('1' => 'one', '2' => 'two', '3' => 'three'),
            ))
        ;
    }
    

    If your choices come from a service, you can create a custom product_key form type and configure it in the service container.

    services:
        product_key_form_type:
            class: ProductKeyFormType
            arguments:
                - @product_key_choices_provider
            tags:
                - { name: form.type, alias: product_key }
    

    This form type would look something like:

    class ProductKeyFormType extends AbstractType
    {
        private $provider;
    
        public function __construct($provider)
        {
            $this->provider = $provider;
        }
    
        public function getDefaultOptions(array $options)
        {
            return array(
                'choices' => $this->provider->getProductKeyChoices(),
            );
        }
    
        public function getParent(array $options)
        {
            return 'choice';
        }
    
        public function getName()
        {
            return 'product_key';
        }
    }
    

    And would be used in your current form type like so:

    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('productKey', 'product_key')
        ;
    }
    

    If all of that is too much, you can also add a product_key_choices option to your form (by adding 'product_key_choices' => array() to getDefaultOptions) and just pass the choices in from your controller. This is easier to get up and running, but less portable since you would need to pass this option in every time you use the form.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my form I have field of type entity . How to disable validation
I have an entity type form field in my Symfony2 project. $builder = $this->createFormBuilder();
I'm having a serialized array of some type. Is there a way to append
I have a product entity which has several classes (each product type has different
I have a form that has an array of dynamically created labels of varying
Is there a way to add a custom EAV attribute that automatically gets set
I have an entity User and an entity Address. There is a relation One-to-Many
I have a Core Data model entity NoteObject that has a transformable type arrayOfTags
Hi every one I have these classe @Entity @Table(name = login, uniqueConstraints={@UniqueConstraint(columnNames={username_fk})}) public class
I have entity Recipes and it has a HasMany collection comments. In a MVC

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.