This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

tobias7884
tobias7884

Hi there,

I am using form_select in twig template:

{{form_select('contacts[][type]', {'0':'Please Select','1':'Contract Negotiation','2':'Orders','3':'Finance','4':'Marketing','5':'Tech (API only)'}, data.type, {'class':'form_input form-control rounded-0'})}}

But when I try to use a twig variable containing the dropdown options I get an error.

This is my Twig-Variable which is getting the options from database:
{% set contactTypes = __SELF__.contactTypes %}

The ouput of the variable is exactly the same as in the example above:
{'0':'Please Select','1':'Contract Negotiation','2':'Orders','3':'Finance','4':'Marketing','5':'Tech (API only)'}

But when I try to use these variable in the form_select-function I get an error: {{form_select('contacts[][type]', contactTypes, data.type, {'class':'form_input form-control rounded-0'})}}

An exception has been thrown during the rendering of a template (Invalid argument supplied for foreach()).

I found no solution to get It working but I think this must be possible. Any idea how to get it working ?

mjauvin
mjauvin

Try manually setting the options in a variable in twig to see if you get the same error. Also try using __SELF__.contactTypes in a twig for loop to see what happens.

tobias7884
tobias7884

Hello Marc,
thank you for the reply.
Unfortunately this produces the same error:

{% set cTypes = "'0':'Please Select','1':'Contract Negotiation','2':'Orders','3':'Finance','4':'Marketing','5':'Tech (API only)'" %}

{{form_select('contacts[][type]', cTypes, data.type, {'class':'form_input form-control rounded-0'})}}

mjauvin
mjauvin

Well, twig is expecting an array, not a string... see below:

{% set options = { "0": "Please Select", "1": "Contract Negotiation", "2": "Orders", "3": "Finance", "4": "Marketing", "5": "Tech (API only)" } %}

1-4 of 4

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.