Symfony Form with Form Collection, cannot pass Options array into sub forms -


in entity controller, create , edit use same formtype, has definition field relation collection of entities. issue having cannot find way pass in $options array form builder available sub entity formtype. pass values through constructors of formtypes feels workaround not solution.

my controller example (state additional option wish pass through);

private function createeditform(delivery $entity) {     $form = $this->createform(new deliverytype(), $entity, array(         'state'=>'update', // value wish pass through.         'action' => $this->generateurl('delivery_update', array('id' => $entity->getid())),         'method' => 'put',     ));     $form->add('submit', 'submit', array('label' => 'update'));     return $form; } 

and in form builder class i've included setdefaultoptions()

public function setdefaultoptions(optionsresolverinterface $resolver) {     $resolver->setdefaults(array(         'data_class' => 'acme\demobundle\entity\delivery',         'state' => 'create'      )); } 

but in formtype class cannot find way pass collection of entities without using constructor of collection formtype. main formtype class looks this;

public function buildform(formbuilderinterface $builder, array $options) {     $builder         ->add('date', 'date', array(                 'widget' => 'single_text',                 'datepicker' => true         ))         ->add('ponumber')         ->add('deliveryitems', 'collection', array(             'type'         => new deliveryitemtype($id),             'allow_add'    => true,             'allow_delete' => true,             'prototype'    => true,             'by_reference' => false,         ))     ; } 

and sub entity formtype looks this;

public function buildform(formbuilderinterface $builder, array $options) {      $builder         ->add('stock', 'entity', array(             'class' => 'acme\demobundle\entity\stock',             'attr'   =>  array(                 'class'   => 'chosen'             )         ))         ->add('quantity')     ; } 

the reason trying specify difference between update , create not have duplicate formtype class files single line change each. passing value through constructors work it's not clean or maintainable. possible option doing through twig feel manually outputting form widgets step backwards.

my ideal solution give sub-entity fields custom status (disabled) on edit controller/page relations cannot reset once created cause problems in code.

i've looked form eventlisteners post/pre submit , gives access data, not force output of field disabled on edit page only.

the issue in deliverytype class couldn't work out how pass options sub formtype. information here: http://symfony.com/doc/current/reference/forms/types/collection.html#basic-usage , in irc cleared missing.

my main formtype has line in entity collection definition;

public function buildform(formbuilderinterface $builder, array $options) {     $builder         ->add('date', 'date', array(                 'widget' => 'single_text',                 'datepicker' => true         ))         ->add('ponumber')         ->add('deliveryitems', 'collection', array(             'type'         => new deliveryitemtype($id),             'allow_add'    => true,             'allow_delete' => true,             'prototype'    => true,             'by_reference' => false,             'options' => array('state' => $options['state']), // line!         ))     ; } 

which means can call $options array in sub formtype (ensure both formtypes have setdefaultoptions function name of option)


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -