symfony - Symfony2: Set label of checkbox from Class Field -


i have class 3 fields:

$id, $name, $ischecked 

i have formtype , want, label of $id field $name field. possible?

/**  * @param formbuilderinterface $builder  * @param array $options  */ public function buildform(formbuilderinterface $builder, array $options) {     $builder         ->add('id')         ->add('name')         ->add('is_checked', 'checkbox', array(             'required' => false,             'label' => //this should $name field         ))     ; } 

for example have class $id = 1, $name= "car". want this:

/**  * @param formbuilderinterface $builder  * @param array $options  */ public function buildform(formbuilderinterface $builder, array $options) {     $builder         ->add('id')         ->add('name')         ->add('is_checked', 'checkbox', array(             'required' => false,             'label' => 'car',         ))     ; } 

--> "car" word should $name variable of class. or how make whole class checkbox in twig/form? want, can check checkbox , know, ok "ischecked" true , have relation id , name. user needs know, checkbox value, need "name" label

there documentation in symfony on how - want modify form based on underlying data. you'll add form event on pre_set_data, used starting data. buildform() function this:

use symfony\component\form\formevent; use symfony\component\form\formevents;  public function buildform(formbuilderinterface $builder, array $options) {     $builder         ->add('id')         ->add('name')     ;      $builder->addeventlistener(formevents::pre_set_data, function (formevent $event) {         $entity = $event->getdata();         $form = $event->getform();          // set default label if there no data, otherwise use name         $label = (!$entity || null === $entity ->getid())             ? 'default'             : $entity->getname()         ;          $form->add('is_checked', 'checkbox', array(             'required' => false,             'label' => $label,         ));     }); } 

another way pass entity data template , manually set label there, above solution more conventional way.


Comments