简体   繁体   English

如何解决Symfony2中的这些表单主题问题?

[英]How to fix these form theming problems in Symfony2?

I have multiple questions about forms 我对表格有多个疑问

First question, how to wrap an input with a div (with classes) in the form builder? 第一个问题,如何在表单构建器中用div(带有类)包装输入?

So I want to wrap this submit input : 所以我想包装此提交输入:

->add('Submit', 'submit', array(
    'attr' =>  array(
        'class' => 'btn btn-success'
    ),
))

To get this result : 要获得此结果:

<div class="col-sm-offset-2 col-sm-10">
    <input type="submit" class="btn btn-success"/>
</div>

Second question, how to set a template to a form via the form builder? 第二个问题,如何通过表单构建器将模板设置为表单?

Is there a method for this like in my wrong example? 有没有像我错误的例子那样的方法? Or not? 或不?

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->setTemplate('::vertical-form.html.twig')
        ->add(...)
}

Last question, how to add a class to the form element in a custom form template? 最后一个问题,如何在自定义表单模板的表单元素中添加类?

I grab this piece of code from form_div_layout.html.twig and I add the class (9th line) to the form element. 我从form_div_layout.html.twig抓取这段代码,并将类(第9行)添加到form元素。 But it doesn't work, I can't see the element in my view! 但这是行不通的,我看不到视图中的元素!

{% block form_start %}
    {% spaceless %}
        {% set method = method|upper %}
        {% if method in ["GET", "POST"] %}
            {% set form_method = method %}
        {% else %}
            {% set form_method = "POST" %}
        {% endif %}
        <form class="form-horizontal" role="form" name="{{ form.vars.name }}" method="{{ form_method|lower }}" action="{{ action }}"{% for attrname, attrvalue in attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}{% if multipart %} enctype="multipart/form-data"{% endif %}>
        {% if form_method != method %}
            <input type="hidden" name="_method" value="{{ method }}" />
        {% endif %}
    {% endspaceless %}
{% endblock form_start %}

In my views, I've only that piece of code to render the form: 在我看来,我只有那段代码来呈现表单:

{% form_theme form '::custom-form.html.twig' %}
{{ form(form_name) }}

Maybe should I copy all form_div_layout.html.twig file to another template and tweak what I need? 也许我应该将所有form_div_layout.html.twig文件复制到另一个模板并进行调整?

Thanks in advance for your help! 在此先感谢您的帮助!

  1. In my opinion in this case You should create submit button manually in a template (not by form class) 我认为在这种情况下,您应该在模板中手动创建“提交”按钮(而不是通过表单类)

  2. I don't know solution which looks like this. 我不知道这样的解决方案。

  3. You should copy all file content. 您应该复制所有文件内容。 Read this: http://symfony.com/doc/current/cookbook/form/form_customization.html 阅读以下内容: http : //symfony.com/doc/current/cookbook/form/form_customization.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM