【问题标题】:Inherit form or add type to each form继承表单或为每个表单添加类型
【发布时间】:2014-03-14 19:32:43
【问题描述】:

我正在寻找一种简单的方法来为每个表单添加一组字段。

我找到了一种方法来扩展 AbstractType 并使用 buildForm 方法添加更多字段。
创建表单时,我给出了我的新类型的名称 (How to Create a Custom Form Field Type)。

在我看来,这是一种简单的方法,但仅限于每个表单一种类型。

有没有更好的方法来实现这样的目标?
我已经阅读了 symfony 的食谱,但我只找到了如何扩展现有表单的内容,而不是如何使用我的字段创建自己的表单“模板”。

【问题讨论】:

    标签: symfony symfony-forms silex symfony-2.4


    【解决方案1】:

    您是否尝试过使用继承?

    这真的很简单,首先你要定义一个表单类型:

    # file: Your\Bundle\Form\BaseType.php
    <?php
    
    namespace Your\Bundle\Form\Type;
    
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
    
    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' => 'Your\Bundle\Entity\YourEntity',
            ));
        }
    
        public function getName()
        {
            return 'base';
        }
    }
    

    那么你可以extend这个表单类型:

    # file: Your\Bundle\Form\ExtendType.php
    <?php
    
    namespace Your\Bundle\Form\Type;
    
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
    
    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' => 'Your\Bundle\Entity\YourEntity',
            ));
        }
    
        public function getName()
        {
            return 'extend';
        }
    }
    

    BaseType 将显示一个 name 字段和一个 add 提交按钮。 ExtendType 将显示一个 name 字段、一个 add 提交按钮和一个 number 字段。

    【讨论】:

    • 如果您像现在一样用ExtendType 扩展BaseType,那么您将用孩子覆盖父buildForm。要正确扩展它,您需要在子 buildForm 的开头包含 parent::buildForm($builder, $options)
    • 你做错了。 Abstract Type 有一个空的 buildForm,所以在 BaseType 中重建是没有意义的。您需要在ExtendType 中使用parent::buildForm(etc),以便它也使用BaseType 中的buildForm。
    • 非常感谢!这是固定的。
    • 当我问起我并不确定表单系统及其行为时。似乎比我想象/理解的要容易。
    • 但似乎无法通过 AbstractTypeExtension 为基本类型添加扩展。
    【解决方案2】:

    您可以使用 getParent() 函数来做到这一点。

    <?php
    
    namespace Your\Bundle\Form;
    
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    
    
    class ChildType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            // $builder->remove('field');
            // $builder->add('field);
        }
    
        public function getParent()
        {
            return ParentType::class;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-20
      • 2012-07-21
      相关资源
      最近更新 更多