如何在symfony2中隐藏表单类中的标签?

时间:2021-04-24 06:45:26

I know that you can split a form out in twig and choose to not render the label for a particular field, but I can't help but think that you must be able to do this from the form class. The 'label' key in the options array lets you change this value to whatever you like, but passing either false or an empty string just returns the field name (see examples below where 'roles' is rendered as the label).

我知道你可以在树枝上拆分一个表格并选择不为特定字段渲染标签,但我不禁想到你必须能够从表格类中做到这一点。 options数组中的'label'键允许您将此值更改为您喜欢的任何值,但传递false或空字符串只会返回字段名称(请参阅下面的示例,其中'roles'呈现为标签)。

$builder
            ->add('roles', 'entity', array(
                'class' => 'Acme\UserBundle\Entity\Role',
                'label' => '' 
            ));

$builder
            ->add('roles', 'entity', array(
                'class' => 'Acme\UserBundle\Entity\Role',
                'label' => false 
            ));

Strangely, passing an empty space (which feels very dirty) seems to render a completely empty label, with no space even when viewing the source. Can anyone shed any light on the best approach, or even why the empty space seems to work?

奇怪的是,传递一个空的空间(感觉非常脏)似乎呈现一个完全空的标签,即使在查看源时也没有空间。任何人都可以对最佳方法有所了解,甚至为什么空白空间似乎有效?

6 个解决方案

#1


85  

Since Symfony 2.2 you can avoid the <label> rendering using the false value for the label attribute:

从Symfony 2.2开始,您可以使用label属性的false值来避免

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('Name', null, array('label' => false))
    ;
}

Source

资源

#2


12  

Keep your 'View' specifications separate from your 'Model'

将“查看”规范与“模型”分开

If you follow the accepted answer which says:

如果你按照接受的答案说:

$builder
        ->add('Name', null, array('label' => false))
    ;

your form is not as re-usable. Especially if your form appears in more than one location (or might in the future).

你的表格不可重复使用。特别是如果您的表单出现在多个位置(或将来可能会出现)。

If you do not want to render the form label it is best to do so in Twig (assuming your using Twig).

如果你不想渲染表单标签,最好在Twig中这样做(假设你使用Twig)。

instead of rendering {{ form_row(form.name) }}, render each element separetly and exclude the form_label

而不是渲染{{form_row(form.name)}},渲染每个元素并排除form_label

ex.

恩。

{{ form_errors(form.name) }}
 {# {{ form_label(form.name) }} <-- just dont include this #} 
{{ form_widget(form.name) }}

If down the road you wanted the label in one instance of the form but the not the other, simply adding {{ form_label(form.name) }} would suffice; Where as changing array('label' => true) would turn the label on everywhere

如果你想要在表格的一个实例中而不是另一个实例中使用标签,只需添加{{form_label(form.name)}}即可;更改数组('label'=> true)会在任何地方打开标签

If you are rendering your form with the one liner {{ form(form) }} then you should have a look at the symfony docs

如果您使用一行{{form(form)}}呈现表单,那么您应该查看symfony文档

#3


3  

To hide my label, I had to render just the widget for the field, and not the label, e.g.

为了隐藏我的标签,我必须只渲染字段的小部件,而不是标签,例如

{{ form_widget(edit_form.event) }}
{{ form_rest(edit_form) }}

The problem with the ' ' label with a space in, is that it still renders the html input which is there and affects the page.

带有空格的''标签的问题在于它仍然呈现那里的html输入并影响页面。

#4


2  

I don't understand very well your question but in form to show the name of label,personnaly I do like that :

我不太了解你的问题,但在形式上显示标签的名称,personnaly我喜欢这样:

  $builder
        ->add('role', 'text')

in my twig :

在我的树枝上:

    <tr>
        <td>{{ form_widget(form.role) }} </td>
        <td>{{ form_label(form.role, "Name of Label") }}</td>
    </tr>
    <tr>
        <td>{{ form_errors(form.role) }}</td>
    </tr>

#5


2  

this should work (although its not a very clean solution)

这应该工作(虽然它不是一个非常干净的解决方案)

$builder
        ->add('roles', 'entity', array(
            'class' => 'Acme\UserBundle\Entity\Role',
            'label' => ' ' 
        ));

(note the space between the ticks)

(注意刻度线之间的空格)

#6


0  

Just add {'label':false} to your form_row()

只需将{'label':false}添加到form_row()

{{ form_row(form.name, {'label':false}) }}

#1


85  

Since Symfony 2.2 you can avoid the <label> rendering using the false value for the label attribute:

从Symfony 2.2开始,您可以使用label属性的false值来避免

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('Name', null, array('label' => false))
    ;
}

Source

资源

#2


12  

Keep your 'View' specifications separate from your 'Model'

将“查看”规范与“模型”分开

If you follow the accepted answer which says:

如果你按照接受的答案说:

$builder
        ->add('Name', null, array('label' => false))
    ;

your form is not as re-usable. Especially if your form appears in more than one location (or might in the future).

你的表格不可重复使用。特别是如果您的表单出现在多个位置(或将来可能会出现)。

If you do not want to render the form label it is best to do so in Twig (assuming your using Twig).

如果你不想渲染表单标签,最好在Twig中这样做(假设你使用Twig)。

instead of rendering {{ form_row(form.name) }}, render each element separetly and exclude the form_label

而不是渲染{{form_row(form.name)}},渲染每个元素并排除form_label

ex.

恩。

{{ form_errors(form.name) }}
 {# {{ form_label(form.name) }} <-- just dont include this #} 
{{ form_widget(form.name) }}

If down the road you wanted the label in one instance of the form but the not the other, simply adding {{ form_label(form.name) }} would suffice; Where as changing array('label' => true) would turn the label on everywhere

如果你想要在表格的一个实例中而不是另一个实例中使用标签,只需添加{{form_label(form.name)}}即可;更改数组('label'=> true)会在任何地方打开标签

If you are rendering your form with the one liner {{ form(form) }} then you should have a look at the symfony docs

如果您使用一行{{form(form)}}呈现表单,那么您应该查看symfony文档

#3


3  

To hide my label, I had to render just the widget for the field, and not the label, e.g.

为了隐藏我的标签,我必须只渲染字段的小部件,而不是标签,例如

{{ form_widget(edit_form.event) }}
{{ form_rest(edit_form) }}

The problem with the ' ' label with a space in, is that it still renders the html input which is there and affects the page.

带有空格的''标签的问题在于它仍然呈现那里的html输入并影响页面。

#4


2  

I don't understand very well your question but in form to show the name of label,personnaly I do like that :

我不太了解你的问题,但在形式上显示标签的名称,personnaly我喜欢这样:

  $builder
        ->add('role', 'text')

in my twig :

在我的树枝上:

    <tr>
        <td>{{ form_widget(form.role) }} </td>
        <td>{{ form_label(form.role, "Name of Label") }}</td>
    </tr>
    <tr>
        <td>{{ form_errors(form.role) }}</td>
    </tr>

#5


2  

this should work (although its not a very clean solution)

这应该工作(虽然它不是一个非常干净的解决方案)

$builder
        ->add('roles', 'entity', array(
            'class' => 'Acme\UserBundle\Entity\Role',
            'label' => ' ' 
        ));

(note the space between the ticks)

(注意刻度线之间的空格)

#6


0  

Just add {'label':false} to your form_row()

只需将{'label':false}添加到form_row()

{{ form_row(form.name, {'label':false}) }}