[SOLVED] Wrong country "selected" if FormItCountryOptions snippet is used twice in same form

Im using the FormItCountryOptions snippet from FormIt package (latest version) in some registration and update profile pages.

...
<select class="form-control" name="country" id="country">
    <option value="" [[!+upd.country:is=``:then=`selected="selected"`]]>Land wählen</option>
    [[!FormItCountryOptions?
        &selected=`[[!+upd.country]]`
        &prioritized=`[[++country_priorisation]]`
        &limited=`[[++country_limitation]]`
        &country=`de`
   ]]
</select>
.....
...
..
<select class="form-control" name="country2" id="country2">
    <option value="" [[!+upd.country2:is=``:then=`selected="selected"`]]>Land wählen</option>
    [[!FormItCountryOptions?
        &selected=`[[!+upd.country2]]`
        &prioritized=`[[++country_priorisation]]`
        &limited=`[[++country_limitation]]`
        &country=`de`
   ]]
</select>
...

The problem: If the FormItCountryOptions snippet is used twice within the same page/form (e.g. payment address + delivery address), the second snippet call selects the wrong country when the option tags are listed. It always selects the country from the first snippet call.

I tested the correct placeholder content with a hidden field. The placeholder data is correct - only the FormItCountryOptions snippet selects the wrong country.

Any hints on how to prevent/fix this behavior?

BTW, I already filed a bug report on GitHub: Wrong country "selected" if FormItCountryOptions snippet is used twice in same form · Issue #255 · Sterc/FormIt · GitHub

As a temporary fix you can add the line

$co->setOption('selected',$modx->getOption('selected',$scriptProperties,''));

before this line in the snippet “FormItCountryOptions”.

This should fix this issue.


The problem is that for the second call to $fi->loadModule(...) the code determines that the class is already loaded and all the new settings are never used.

1 Like

You are great! Works like a charm!
May I post your solution to my FormIt GitHub issue?

Greetings,
Martin

Sure, but it’s just a partial fix.

If you had different values for other properties (like &prioritized or &limited etc.) these would be ignored too. So someone with more knowledge of the code has to come up with a real solution.

1 Like

I’ll post a hint to this forum thread!
Thanks!