我很想为某些领域写一个包。 现在我得到了一个我可以延伸的课程。 班上有一些领域,例如标题,名称等。
现在我想写一个formType,可以用另一种formtype扩展。
喜欢
class newsgroupType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('unusername','text',array(
'required' => true))
->add('unactive','checkbox',array(
'required' => false));
...
我想要扩展的FormType是
class mainType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('unurl','text',array(
'required' => false,
'disabled' => true))
->add('untitle','text',array(
'required' => false))
...
有没有办法扩展mainType以将其中的所有字段都放入我的newsgroupType?
非常感谢=)
答案 0 :(得分:4)
There is a cookbook article on the Symfony website that gives and example of how to extract common fields into a separate form definition which can then be included into other form definitions.
Create a separate Form
class that includes the common fields using the builder. You can then configure that form using the inherit_data
option:
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'inherit_data' => true
));
}
The other forms can then add that form in the builder:
public function buildForm(FormBuilderInterface $builder, array $options)
{
// ...
$builder->add('foo', new CommonType(), array(
'data_class' => 'AppBundle\Entity\Foo'
));
}
答案 1 :(得分:1)
I think you should think it differently.
You can use nested form types to do what you want.
For example :
class newsgroupType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('main', new MainType())
->add('unusername','text',array(
'required' => true))
->add('unactive','checkbox',array(
'required' => false));
...
You should also namle your classes the right way, with CamelCase (in my example, MainType instead of mainType)