Personal tools
Home Webmaster Resources Zope/Plone Multiple select boxes in Controller Page Templates

Multiple select boxes in Controller Page Templates

Adding a multiple select box to a controller page template form can be accomplished using a small trick.  The is only useful if you are using one or more validators for your form, and need your form data to persist after validation.  The problem is, if the user has only selected one value, Zope passes a string to your template, but if it selects more than one value, it passes a list.  I'm hoping future versions of Zope take care of this issue and return a list regardless of how many values were chosen.  In the meantime, the count function will work for both lists and strings.  See the example code below:


 <div class="field" tal:attributes="class python:test(error_fieldname, 'field error', 'field')" tal:define="error_fieldname errors/fieldname|nothing;">
    <label i18n:translate="label_fieldname">Fieldname</label><br/>
        <select name="fieldname" tal:define="checkval request/fieldname|string:[ ]" multiple="true">
            <option value="First" tal:condition="python: repval.count('First') > 0" selected>First Value</option>

            <option value="First" tal:condition="python: repval.count('First') == 0">First Value</option>

            <option value="Sec" tal:condition="python: repval.count('Sec') > 0" selected>Sec. Value</option>

            <option value="Sec" tal:condition="python: repval.count('Sec') == 0">Sec. Value</option>
       </select>
</div>

Need assistance with your project? Universal Web Services can help.
Contact us to request a quote.