Question

I am using template toolkit, with perl Dancer. I have a form, and this form has a select. I am trying after the form is submitted and the page returns to the same form, maintain the previous item selected in select. The Ldap_List contains 10 items.

This is the select code:

<form class="well form-inline" method="post" action="[% request.uri_base %]/foo/ldap">
<fieldset>  
<legend>Select the LDAP:</legend>
<select class="selectpicker" name="ldap_selected">
[% FOREACH ldap IN Ldap_List %]
   <option value=[% ldap %]>[% ldap %]</option>
[% END %]
</select>
<button type="submit" class="btn btn-primary">Go!</button>
</fieldset>
</form>

The variable with the last ldap selected is ldap_selected, and its defined and have the correct value, im checked it with:

[% IF ldap_selected %]
   [% ldap_selected %]
[% END %]

I tried this, but doesn't works:

<select class="selectpicker" name="ldap_selected">   
   [% FOREACH ldap IN Ldap_List %]
       [% IF ldap == ldap_selected %]
           <option value=[% ldap %] selected>[% ldap %]</option>
       [% ELSE %]
           <option value=[% ldap %]>[% ldap %]</option>
       [% END %]
   [% END %]
</select>

Because with that, in the select appears the ldap_selected, but its the only one option in the list.

How can i achieve this?

Was it helpful?

Solution

You just need a simple HTML Form Tag around the Select Tag and then read the Form Parameters inside your Perlscript with something like params->{'ldap_selected'}. I can suggest to read the Dancer Documentation.

For this case you can start to take a look here


EDIT:

After your edit, it seems to be a Template Toolkit Problem. You said, that you have the Value of ldap_selected and it seems to be the value you need. [% IF ldap == ldap_selected %] is exactly the thing you need to do. Your Syntax is correct.

There are 2 Possible Problems/Solutions i can see there:

  1. ldap_selected contains a newline or its not set.
  2. ldap contains a newline.

Check with ctrl+u inside your browser the output of each variable. Maybe you can see a newline. Try the IF ... Part with an hardcoded ldap_selected ([% IF ldap_selected == 'foo' %]) and with

[% FOREACH ldap IN [ 'foo', 'bar', 'baz' ] -%]

The ldap Part.

When nothing works, check if you maybe changed the Configuration of TT in your MyWeb-App/config.yml

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top