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?
You need to specify the choices using the
choicesoption in the form type:If your choices come from a service, you can create a custom
product_keyform type and configure it in the service container.This form type would look something like:
And would be used in your current form type like so:
If all of that is too much, you can also add a
product_key_choicesoption to your form (by adding'product_key_choices' => array()togetDefaultOptions) 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.