2013-03-18 10 views
17

Ich habe SuperType Formular für die Entität Super.Wie übergebe ich Optionen an CustomType im Sammlungsfeld Symfony 2.1?

In dieser Form habe ich ein collection Feld ChildType Formulartypen für Entity Child

class SuperType:

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder->add('childrens', 'collection', array(
      'type' => new ChildType(null, array('my_custom_option' => true)), 
} 

class ChildType:

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    if ($options['my_custom_option']) { 
     $builder->add('my_custom_field', 'textarea')); 
    } 
} 

public function setDefaultOptions(OptionsResolverInterface $resolver) 
{ 
    $resolver->setDefaults(array(
     ... 
     'my_custom_option' => false 
)); 
} 

Wie kann ich die 012 ändernWert nur für diese SuperType Form?

Natürlich, was ich versucht habe, diese Option über Konstruktor übergeben, funktioniert nicht.

Antwort

28

Sie können eine array of options auf Ihre childType passieren, wie folgt:

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder->add('childrens', 'collection', array(
      'entry_type' => new ChildType(), 
      'entry_options' => array(
       'my_custom_option' => true, 
      ), 
    // ... 

} 
+2

Erwägen Sie, Ihre Antwort zu aktualisieren, da dies jetzt veraltet ist – Trix

12

In Symfony 3 ist diese entry_options genannt wird.

$builder->add('childrens', CollectionType::class, array(
    'entry_type' => ChildType::class, 
    'entry_options' => array(
     'my_custom_option' => true 
    ), 
)); 
Verwandte Themen