I have SuperType
Form for Entity Super
.
In this form I have a collection
field of ChildType
Form types for 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
));
}
How can I change the my_custom_option
value only for this SuperType
form?
Of course, what I've tried passing this option via constructor doesn't work.
You can pass an array of options to your childType as follows:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('childrens', 'collection', array(
'entry_type' => new ChildType(),
'entry_options' => array(
'my_custom_option' => true,
),
// ...
}
In Symfony 3, this is called entry_options.
$builder->add('childrens', CollectionType::class, array(
'entry_type' => ChildType::class,
'entry_options' => array(
'my_custom_option' => true
),
));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With