【问题标题】:auto div and field in Collection Form Symfony 2收集表单 Symfony 2 中的自动 div 和字段
【发布时间】:2013-02-03 20:31:52
【问题描述】:

我有一个表格集合。当我渲染我的结果时,我有每个表单的 div 和标签(当我使用原型添加表单时,这些也被添加) 第一种形式是

namespace Webwall\CamBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class SocialType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder

                ->add('account')

        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'Webwall\CamBundle\Entity\Social',
        ));
    }

    public function getName() {
        return 'social';
    }

}

“主窗体”是

namespace Webwall\CamBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class MediaType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {

        $builder                
                ->add('socials', 'collection', array(
                    'type' => new SocialType(),
                    'allow_add' => true, 
                ))

        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'Webwall\CamBundle\Entity\Media'
        ));
    }

    public function getName() {
        return 'foto';
    }

}

在我的树枝上

<div id="form_foto">
{{ form_errors(form) }}
{{ form_rest(form) }}

在我的 dom 我有这个

<div><label class="required">Socials</label><div id="foto_socials" data-prototype=""><div><label class="required">0</label>

为什么我有这个默认的 div? 谢谢!

【问题讨论】:

  • 你能把你完整的&lt;div id="form_foto"&gt; 贴在你的树枝上并渲染出来吗?

标签: symfony twig formbuilder


【解决方案1】:

编辑树枝文件

                        <ul class="unstyled child" data-prototype="{{ form_widget(form.socials.vars.prototype)|e }}">
                            {% for row in form.socials %}
                                <li style="margin-bottom: 5px;"> {{ form_widget(row.account) }}</li>
                            {% endfor %}
                        </ul>

【讨论】:

  • 当我为新字段使用 Prototype 时,我拥有所有形式的结构,如标签等...我可以个性化输出吗?
【解决方案2】:

我遇到了同样的问题,您可以修改 twig(按照 Kaan 的建议)或者您可以修改您的类型表单,如下所示,只需 添加选项行

    $builder                
            ->add('socials', 'collection', array(
                'type' => new SocialType(),
                'options' => array('label' => false), // ADD THIS LINE IN YOUR CODE
                'allow_add' => true, 
            ))

【讨论】:

    猜你喜欢
    • 2013-11-14
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多