40

Is it possible to set default value of input field in Twig ?

I am rendering form row with:

{{ form_widget(form.title) }}

I want to be able to set default value like:

{{ form_widget(form.title, {data : 'Default title'}) }}

Is it even possible ?

2 Answers 2

85

Magic keyword for default value is value, not data.

{{ form_widget(form.title, {'value' : 'Default title'}) }}
6
  • thanks for your response :) . Is it possible to put twig variable in value like that : {{ form_widget(form.title, {value : ' {{variable_twig}} '}) }} ?
    – Zagloo
    Nov 24, 2014 at 9:30
  • 1
    @Zagloo Simply try with: {{ form_widget(form.title, {value : variable_twig}) }}
    – hsz
    Nov 24, 2014 at 9:31
  • Nice ! and with text like that : {{ form_widget(form.title, {value : 'blablalbaa variable_twig blablalblaaa'}) }} ?
    – Zagloo
    Nov 24, 2014 at 9:35
  • 1
    @Zagloo 'blablalbaa ' ~ variable_twig ~ ' blablalblaaa'
    – hsz
    Nov 24, 2014 at 9:48
  • 1
    {{ form_widget(form.title, {'value' : variable_twig|number_format }) }} Nov 30, 2015 at 6:37
2

You can do it when creating the Type as well. I think it's "cleaner" than doing it in Twig.

1
  • 2
    You'r right, but I want to sparate backnd from using i18n mssages - I want to do it only in frontend.
    – hsz
    Dec 18, 2012 at 6:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.