Have you tried using inheritance?
This is really simple, first you have to define a form type:
# file: YourBundleFormBaseType.php
<?php
namespace YourBundleFormType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolverInterface;
class BaseType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', 'text');
$builder->add('add', 'submit');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'YourBundleEntityYourEntity',
));
}
public function getName()
{
return 'base';
}
}
Then you can extend
this form type:
# file: YourBundleFormExtendType.php
<?php
namespace YourBundleFormType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolverInterface;
class ExtendType extends BaseType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
# you can also remove an element from the parent form type
# $builder->remove('some_field');
$builder->add('number', 'integer');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'YourBundleEntityYourEntity',
));
}
public function getName()
{
return 'extend';
}
}
The BaseType
will display a name field and an add submit button. The ExtendType
will display a name field, an add submit button and a number field.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…