Symfony表单生成器-如何将表单标题放入H2选择器中?

时间:2018-10-31 12:24:18

标签: symfony twig formbuilder

我希望我的表单标题显示在h2选择器中。我做了类似的事情,但它给我抛出一个错误“异常:“在渲染模板期间抛出了异常(“注意:未定义的偏移量:-1”)。“”

 // how should I change THIS part? To change only the main form title/label?
 // I made it work somehow but then it changes all labels... Is there some 
 // selector which allows to style MAIN title of the form?
{% block form_label %}
    {% spaceless %}
        <h2>{{ form_label(form) }}</h2>
    {% endspaceless %}
{% endblock form_label %}

我听说我不应该将其放在表单模板中的h2选择器中的项目文件中。它更干净,即使文档没有禁止我被鼓励以另一种方式来做,这就是我想尝试的方式。

{% form_theme form 'Forms/base_form.html.twig' %}
{{ form_start(form) }}

    {{ form_label(form, 'Project title', { 'label_attr': {'class': 'main-form-label'} }) }} 
  // so as I shouldn't put all that line in <h2> can I somehow do it in template between  {% block form_label %} ?

    {{ form_row(form.title, {'label': 'My title'}) }}
    {{ form_row(form.isComplete, {'label': 'Dropdown'}) }}
    {{ form_row(form.comment, {'label': 'Comment'}) }}
    {{ form_row(form.submit, {'label': 'Submit'}) }}


{{ form_end(form) }}

也...有什么区别/我应该使用什么-{% block form_label %}{%- block form_label -%}

我的整个模板:

{% block form_label %}
    {% spaceless %}
        <h2>{{ form_label(form) }}</h2>
    {% endspaceless %}
{% endblock form_label %}

{% block form_row %}
    {% spaceless %}
        {{ form_widget(form) }}
        {{ form_errors(form) }}
    {% endspaceless %}
{% endblock form_row %}

{% block submit_row %}
    {% spaceless %}
        <div class="col-12">
            {{ form_widget(form) }}
        </div>
    {% endspaceless %}
{% endblock submit_row %}

{% block text_widget %}
    {% spaceless %}
        <div class="col-12">
            <div>
                {{ form_label(form) }}
            </div>
            {{ form_widget(form) }}
        </div>
    {% endspaceless %}
{% endblock text_widget %}

{% block choice_widget %}
    {% spaceless %}
        <span>
            {{ form_label(form) }}

            {% if expanded %}
                {{ block('choice_widget_expanded') }}
            {% else %}
                {{ block('choice_widget_collapsed') }}
            {% endif %}
            {{ form_errors(form) }}
        </span>
    {% endspaceless %}
{% endblock choice_widget %}

1 个答案:

答案 0 :(得分:0)

万一您卡在某处;) 我们曾经使用过这种方式(当我们想要自定义渲染时)。

首先,我们扩展FormType:

namespace App\Form\Extension;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

class FormTypeExtension extends AbstractTypeExtension
{
    public function getExtendedType()
    {
        return FormType::class;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefault('main_title', false);
        $resolver->setAllowedTypes('main_title', 'boolean');
    }

    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        $view->vars['main_title'] = $options['main_title'];
    }
}

请考虑在Symfony中注册您的扩展程序!

然后您可以通过以下方式在表单构建器中使用它:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add(
        'myField',
        TextType::class,
        array(
            'main_title' => true
        ));
}

最后,在您的模板中:

{% block form_row -%}
{% spaceless %}

    {% if main_title %}
        <h2>{{ form_label(form) }}</h2>
    {% else %}
        {{ form_label(form) }}
        {{ form_widget(form) }}
    {% endif %}

{% endspaceless %}
{%- endblock form_row %}

可以根据需要随意覆盖该块;-)