问题
In my Symfony 4 form, I am try to get a placeholder for my input. I tried the below but I get an error that it is not allowed. Any ideas how else I can achieve this?
->add('firstname', TextType::class, ['label' => 'Vorname ', 'placeholder' => 'Your name',])
As suggested in the documentation I have also tried the below, here I am not getting an error, just nothing is rendering.
->add('firstname', TextType::class, ['label' => 'Vorname '], ['attr' => ['placeholder' => 'Vorname ']])
回答1:
You need to do like this :
->add('firstname', TextType::class',array(
'label' => 'Vorname ',
'attr' => array(
'placeholder' => 'hereYourPlaceHolder'
)
))
As they say in the documentation
回答2:
The 'placeholder' option don't exists in TextType. If you wan to fill the field. You have to fill the entity you pass to build the form.
$entity = new MyEntity();
$entity->setFirstName('Your name');
$form = $this->createForm(MyEntityType::class, $entity);
回答3:
This also works, by rendering the items individually and adding an attribute in the twig!
{{ form_label(form.firstname) }}
{{ form_widget(form.firstname, {'attr': {'placeholder': 'FIRSTNAME'}}) }}
来源:https://stackoverflow.com/questions/55707663/how-to-add-placeholder-on-input-in-symfony-4-form