在Symfony 3中,如何将一个只读的字段显示为标签?[英] With Symfony 3, how to display a read-only field as a label ? (in FOSUBundle's registration form)

本文是小编为大家收集整理的关于在Symfony 3中,如何将一个只读的字段显示为标签?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

使用Symfony 3和覆盖FosuserBundle的注册表格,我有一个可读字段:

->add('name', TextType::class, array('attr' => array('readonly' => true)))

目前,它看起来是文本框,但我希望它以文本/标签的形式出现.我看不到诸如" LabelType"之类的.可以这样做吗?

推荐答案

当它具有readonly属性时,看起来您需要自定义文本框的外观.因此,简单的解决方案可能是单个CSS样式:

// style.css
input[readonly] {
    border: none;
    // more custom styles
}

我的意思是,没有必要在Symfony中做一些特别的事情,并且对于所有输入readonly表单字段可能很有用.

其他推荐答案

" read_only"在Symfony 2.8中被弃用,现在readonly是一个属性.尝试一下:

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'validation_groups' => ['whatever'],
        'attr' => ['readonly' => true],  
    ]);
}

在其他情况下,使用表单字段为attr:

时将其添加到树枝中
   {{ form_widget(form.name, {'attr': {readonly:readonly}) }}

正确的方法是这样做并使用attr属性:

->add('name', TextType::class, array(
'attr' => array(
    'readonly' => true,
),
));

本文地址:https://www.itbaoku.cn/post/2090820.html

问题描述

Using Symfony 3 and overridden FOSUserBundle's registration form, I have a readonly field:

->add('name', TextType::class, array('attr' => array('readonly' => true)))

For now, it appears as a textbox, but I would like it to appear as a text/label instead. I see nothing such as "LabelType". Is it possible to do this?

推荐答案

It look like you need customize the appearance of the textbox when it has the readonly attribute. So the simple solution could be a single CSS style:

// style.css
input[readonly] {
    border: none;
    // more custom styles
}

I mean, there is no need to do something special in Symfony and could be useful for all input readonly form fields.

其他推荐答案

"read_only" was deprecated in Symfony 2.8, now readonly is an attribute. Try this:

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'validation_groups' => ['whatever'],
        'attr' => ['readonly' => true],  
    ]);
}

In other case add this to your twig when using the form field as attr :

   {{ form_widget(form.name, {'attr': {readonly:readonly}) }}

The right way to do this is by doing it like this and making use of the attr property:

->add('name', TextType::class, array(
'attr' => array(
    'readonly' => true,
),
));