vekah
vekah

Reputation: 990

symfony2 form twig input value

I am doing a form with symfony2 and twig, form who get infos from BDD. I want to customize render in function of some informations.

If my data chanson is empty, I want to show input to set it. If my data is not empty I want to show a paragraphe who shows data, and a link for modify the value and show the input.

I try something like that :

{% if form_widget(session.chanson).attrvalue!='' %}
 <p>{{form_widget(session.chanson).attrvalue}} <a>modify</a></p>
{% else %}
 <p>{{ form_label(session.chanson,"Chanson : ") }}
 {{ form_errors(session.chanson) }}
 {{ form_widget(session.chanson) }}</p>
{% endif %}

It's not working. I try with value instead of attrvalue, it's not working neither. Here is what symfony say : Item "attrvalue" for "<input type="text" id="form_chanson" name="form[chanson]" required="required" value="La Rage" />" does not exist in CDUserBundle:Prof:edit_session.html.twig at line 19

Did someone know the issue ? Thank you,

Upvotes: 1

Views: 953

Answers (1)

cheesemacfly
cheesemacfly

Reputation: 11762

You could check if the app.session.chanson variable is empty instead using:

{% if app.session.chanson %}
   <p>{{ app.session.chanson }} <a href="#">modify</a></p>
{% else %}
   <p>{{ form_label(app.session.chanson,"Chanson : ") }}
   {{ form_errors(app.session.chanson) }}
   {{ form_widget(app.session.chanson) }}</p>
{% endif %}

You then need to plug the action you want on the modify link.

Also note that if your object chanson is stored in a session, the right way to access it in your twig template is by using the app.session object.

Upvotes: 1

Related Questions