php - symfony form choice field : set choices option default data from external datasource -
how populate choices option external data array ?
$builder->add('gender', 'choice', array( 'choices' => [array webservice or other class] )); documentation show simple static array http://symfony.com/fr/doc/2.7/reference/forms/types/choice.html#choices
i tried :
controller :
$event = new event(); $form = $this->createform(new eventtype($this->get('api')), $event); form :
class eventtype extends abstracttype { protected $myservice; public function __construct($myservice) { $this->myservice = $myservice; } public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('titre') ->add('id_rubrique', 'choice', array( 'choices' => $this->myservice->getrubriques ; } .... } is right solution ?
define form service:
services: my.form: class: eventtype arguments: - @api then in controller:
$event = new event(); $form = $this->createform('my.form', $event);
Comments
Post a Comment