[ZPT] select, option, selected in ZPT?

Harry Wilkinson harryw@nipltd.com
Wed, 19 Jun 2002 15:33:19 +0100


On Wednesday 19 June 2002 2:55 pm, Jim Kutter wrote:
> Hi Folks
>
> I'm trying to figure out how to retain form data after a post (in the event
> of an error of some sort) so I fill in the values of the form with what was
> passed. For example, a registration process that must do a DB query and
> return the same form if the username is already taken so the user can pick
> another without filling out the whole thing again.
>
> How would I add the "selected" attribute to an <option> html tag in zpt?
> Does the tal:attribute method work? If so, wouldn't I need to tell it what
> attribute to assign a value to?
>
> Basically I want to do this in pseudocode
> while (dropdown)
> if dropdown[n] == options.dropdown_from_post:
>     <option "selected">Blah</option>
> else:
>     <option>Blah not selected</option>
>
> Thanks
>
> -jim


I think you want something like this:

<select name="blah">
  <tal:loop repeat="opt opts"
    <option tal:attributes="selected python:options['blah'] == opt; value opt"
                              tal:content="opt" />
</tal:loop>
</select>

You can give the 'selected' attribute a true or false value and ZPT will sort 
it out.


Harry