如何在symfony2的表单类中隐藏标签? [英] How do you hide labels in a form class in symfony2?

查看:50
本文介绍了如何在symfony2的表单类中隐藏标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您可以在树枝中拆分表单,并选择不为特定字段呈现标签,但是我不禁认为您必须能够在表单类中执行此操作。通过options数组中的'label'键,您可以将此值更改为所需的值,但是传递false或空字符串只会返回字段名称(请参见下面的示例,其中将'roles'呈现为标签)。

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).

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

推荐答案

自Symfony 2.2起,您可以为标签 false 值避免使用< label> 呈现$ c>属性:

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

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

来源

这篇关于如何在symfony2的表单类中隐藏标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆