With Symfony 3, how to display a read-only field as a label ? (in FOSUBundle's registration form)

房东的猫 提交于 2019-12-11 01:27:26

问题


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?


回答1:


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.




回答2:


"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,
),
));


来源:https://stackoverflow.com/questions/45757448/with-symfony-3-how-to-display-a-read-only-field-as-a-label-in-fosubundles-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!