1
votes

I am trying to set a data- attribute for a save button, but it doesn't like the nested braces. I'm getting an error on the "###THIS LINE" line.

 {% for file in payment.files %}
     <br>
     <li>
         ...
         ... etc...
         ... 
         {{ form_widget(file.save, {'attr':{'data-file-id': {{ file.id}} } }) }} ###THIS LINE

     </li>
{% endfor %}

The error I am getting is: A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value "{".

I guess it doesn't like the nested braces.

Any help is greatly appreciated.

3
Try this: {{ form_widget(file.save, {'attr':{'data-file-id': file.id } }) }} (you are already in a twig statement when you add file.id as parameter) - YaatSuka
Hi YaatSuka, that gives this error: An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class Symfony\Component\Form\FormView could not be converted to string"). - Brent Heigold

3 Answers

0
votes

Try to convert file.id into string:

{{ form_widget(file.save, {'attr':{'data-file-id': file.id.__toString } }) }}
0
votes

Just try it:

{{ form_widget(file.save, {'attr':{'data-file-id': file.id } }) }}
-2
votes

Try:

{{ form_widget(file.save, {'attr': {'data-file-id': file.id|string } }) }}