php - How to pass option when creating form -


i'm working on form , want pass form array content projects form have select of project option.

i reed alot of answer, can't figure thing out. know i'm suppose like:

$formulaire=$this->createform(new modifiersupprimerprojet(), null, $myarray); 

but, i'm suppose add content of array getdefaultoptions()... do that? other thing, suppose second parameters of createform method?

here post that came closest solve probleme:

accessing variable through $options in buildform()

the best way populate select form element project entities use entity form field. don't have pass options form type class.

$builder     ->add('project', 'entity', [         'label'         => 'form.project',         'class'         => 'appbundle\entity\project',         'property'      => 'name',         'required'      => true,         'empty_value'   => 'form.select_empty_value',         'query_builder' => function($repository) {             return $repository->createquerybuilder('p')->add('orderby', 'p.name asc');         },     ]) ; 

if need populate choice field there best practice. should define form type class service , inject entity manager it. fetch data database , populate choice fields.

services.xml

<service id="app.form.modifier_supprimer_projet" class="adminbundle\form\modifiersupprimerprojet">     <tag name="form.type" alias="app_modifier_supprimer_projet" />     <argument type="service" id="doctrine.orm.entity_manager"/> </service> 

modifiersupprimerprojet.php

<?php  namespace appbundle\form;  use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface; use symfony\component\optionsresolver\optionsresolverinterface; use doctrine\orm\entitymanager;  class modifiersupprimerprojet extends abstracttype {     /**      * @var entitymanager      */     protected $em;      /**      * @param entitymanager $em      */     public function __construct(entitymanager $em)     {         $this->em = $em;     }      /**      * @param formbuilderinterface $builder      * @param array $options      */     public function buildform(formbuilderinterface $builder, array $options)     {         $builder             ->add('project', 'choice', [                 'label'   => 'form.project',                 'choices' => $this->em->getrepository('appbundle:project')->getprojectslist(),             ])         ;     }      /**      * @param optionsresolverinterface $resolver      */     public function setdefaultoptions(optionsresolverinterface $resolver)     {         $resolver->setdefaults([                 'data_class' => 'appbundle\entity\modifiersupprimerprojet',         ]);          }      /**      * @return string      */     public function getname()     {         return 'app_modifier_supprimer_projet';     } } 

controller

$modifiersupprimerprojet = new modifiersupprimerprojet();  $form = $this->createform('app_modifier_supprimer_projet', $modifiersupprimerprojet); 

second param of createform method entity, if form mapped entity.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -