1

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,

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok thank you it's working nice. My object isn't stored in a session, session is the name of the form I create and render in the view. Just a bad name.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.