How to add placeholder on input in Symfony 4 form?

戏子无情 提交于 2020-01-06 06:24:10

问题


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

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