[Zope] Using ZPT with options/radio/checkboxes

Ron Bickers rbickers-expires-1060839764.872734 at logicetc.com
Thu Aug 7 02:42:12 EDT 2003


> -----Original Message-----
> From: zope-bounces at zope.org [mailto:zope-bounces at zope.org]On Behalf Of
> Chris Bruce

> Is there an easy way to use TAL/TALES  to set the checked values
> of radios and checkboxes and the selected attribute of options?

> <select name="question_type" id="question_type">
>   <option value="Text" selected="selected">Text</option>
>   <option value="TextArea">TextArea</option>
>   ...
> </select>
>
> If the question_type was in the REQUEST object, how would I
> evaluate which one was selected?

<select name="question_type" id="question_type">
  <option value="Text" tal:attributes="selected
python:path('request/question_type | nothing') == 'Text'">Text</option>
  ...
</select>

If you have the option values in a list, you can "repeat" through them,
filling in the blanks.  Otherwise, you'll need to put the tal:attributes in
each option tag.  The "| nothing" is to fail gracefully if question_type
happens to not be in REQUEST.

> Same goes for radio buttons:
>   <input type="radio" name="required" value="1" /> Yes
>   <input name="required" type="radio" value="0" checked="checked" /> No
>
> and the 'required' was in the REQUEST object.

<input type="radio" name="required" value="1" tal:attributes="checked
python:path('request/required | nothing') == '1'" /> Yes
<input type="radio" name="required" value="0" tal:attributes="checked
python:path('request/required | nothing') == '0'"/> No

This will work for checkboxes too.  The "| nothing" is especially important
for checkboxes, since a checkbox variable is only set if it's checked.  If
it's unchecked, it's left out of REQUEST altogether.

Hope this helps.
_______________________

Ron Bickers
Logic Etc, Inc.




More information about the Zope mailing list