Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add placeholder on input in Symfony 4 form?

Tags:

html

symfony4

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 ']])
like image 750
kontenurban Avatar asked Apr 16 '19 12:04

kontenurban


2 Answers

You need to do like this :

->add('firstname', TextType::class, array(
      'label' => 'Vorname ',
      'attr' => array(
          'placeholder' => 'hereYourPlaceHolder'
      )
 ))

As they say in the documentation

like image 56
Nelson Rodrigues Marreiros Avatar answered Sep 28 '22 12:09

Nelson Rodrigues Marreiros


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'}}) }}
like image 41
kontenurban Avatar answered Sep 28 '22 13:09

kontenurban