Pregunta

I have read this question

Code in twig is as below

{{ form_widget(form.skills,{ value : "{{allocation.skills.skillsId }}", 'attr': {'class': 'right skillClass', } }) }}

form.skills creates a select box, {{allocation.skills.skillsId }} always returns a valid value which exists in the select box.

But this code is not working while giving direct number constant works very fine.

{{ form_widget(form.skills,{ value : "5", 'attr': {'class': 'right skillClass', } }) }}

Can you say how to solve this problem, thanks in advance.

¿Fue útil?

Solución

your syntax is incorrect, you should use this instead:

{{ form_widget(form.skills,{ value : allocation.skills.skillsId, 'attr': {'class': 'right skillClass', } }) }}

NOTE

value must be a string, so there are two ways you can achive this

  • setting the default value in the form in your controller
  • passing a string to the view
  • "converting" the integer into a string in twig

setting the default value in the form in your controller

->add("skills", "choice", array("label" => "Skills", "choices" => $yourChoices, "attr" => array("class" => "right skillClass"), 'preferred_choices' => array(allocation.skills.skillsId)))

passing a string to the view

return $this->render("Bundle:view.html.twig", array("yourId" => (string)$yourId));

and the use yourId into the twig

{{ form_widget(form.skills,{ value : yourId, 'attr': {'class': 'right skillClass', } }) }}

"converting" the integer into a string in twig (very dirty)

{{ form_widget(form.skills,{ value : "" ~ allocation.skills.skillsId ~ "", 'attr': {'class': 'right skillClass', } }) }}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top