php - Symfony2: Passing options to buildForm -


so i'm pretty new symfony , i'm trying have controller build different forms based on controller actions. right now, have this

//in controller  public function addlocationentryaction(request $request) {     $entry = new entry();     $form = $this->get('form.factory')->create(new entrytype('addlocation'), $entry);      return $this->render('ootnblogbundle:blog:addentry.html.twig', array(         'form' => $form->createview()     )); }  public function addarticleentryaction(request $request) {     $entry = new entry();     $form = $this->get('form.factory')->create(new entrytype('addarticle'), $entry);      return $this->render('ootnblogbundle:blog:addentry.html.twig', array(         'form' => $form->createview()     )); } 

and

//in entrytype  public function buildform(formbuilderinterface $builder, array $options) {      $builder         ->add('title',      'text')         ->add('continent',  'text', array('required' => false))         ->add('country',    'text', array('required' => false))         ->add('category',   'text', array('required' => false))         ->add('author',     'hidden')         ->add('type',       'hidden')         ->add('text',       'textarea')         ->add('post',       'submit')     ; } 

i'd pass option controller in buildform, can this:

public function buildform(formbuilderinterface $builder, array $options, $option) {     $builder         ->add('title',      'text')     ;      if($option == 'addlocation')     {         $builder             ->add('continent',  'text', array('required' => false))             ->add('country',    'text', array('required' => false))         ;     }     elseif($option == 'addarticle')     {         $builder             ->add('category',   'text', array('required' => false))         ;     }      $builder         ->add('author',     'hidden')         ->add('type',       'hidden')         ->add('text',       'textarea')         ->add('post',       'submit')     ; } 

how do this? i've checked out symfony doc , similar questions on here nothing seems quite fitting case. don't know.

there no need create additional param, use options pass custom data. in following example, made age attribute mandatory, have optional or specify default value. read more optionsresolver here

class exampletype extends abstracttype {     public function buildform(formbuilderinterface $builder, array $options)     {         var_dump($options['age']);     }      public function setdefaultoptions(optionsresolverinterface $resolver)     {         $resolver->setrequired(array(             'age'         ));      } } 

create:

$form = $this->get('form.factory')->create(new exampletype(), $entry, array(     'age' => 13 )); 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -