1

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.

2
  • 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) Commented Oct 5, 2017 at 22:31
  • 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"). Commented Oct 5, 2017 at 22:33

3 Answers 3

0

Try to convert file.id into string:

{{ form_widget(file.save, {'attr':{'data-file-id': file.id.__toString } }) }}
Sign up to request clarification or add additional context in comments.

2 Comments

That gives me this: Neither the property "__toString" nor one of the methods "__toString()", "get__toString()"/"is__toString()"/"has__toString()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView".
Then: {{ form_widget(file.save, {'attr':{'data-file-id': "%d"|format(file.id)} }) }}
0

Just try it:

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

Comments

-2

Try:

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

2 Comments

Then I just get the same error, but on that line
Based on the example documentation: {# render a widget, but add a "foo" class to it #} {{ form_widget(form.name, { 'attr': {'class': 'foo'} }) }}

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.