问题
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