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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10602923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:35:05  来源:igfitidea点击:

How do you hide labels in a form class in symfony2?

phpformsclasssymfony

提问by Mark

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?

奇怪的是,通过一个空的空间(感觉很脏)似乎渲染了一个完全空的标签,即使在查看源时也没有空间。任何人都可以阐明最佳方法,甚至为什么空白空间似乎有效?

回答by cheesemacfly

Since Symfony 2.2 you can avoid the <label>rendering using the falsevalue for the labelattribute:

从 Symfony 2.2 开始,您可以<label>使用属性false值来避免渲染label

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

Source

来源

回答by Chris

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) }}就足够了;在哪里改变array('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 文档

回答by PrestonDocks

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

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

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

回答by jmoz

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输入并影响页面。

回答by Nll

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

我不太明白你的问题,但以显示标签名称的形式,我确实喜欢这样:

  $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>

回答by philipphoffmann

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)

(注意勾号之间的空格)