durusmail: quixote-users: Help setting RadiobuttonsWidget options
Help setting RadiobuttonsWidget options
2004-12-27
2004-12-27
Help setting RadiobuttonsWidget options
Jason Sibre
2004-12-27
Quoting mso@oz.net:
....
> Which makes me wonder why is the third element ('key') turning into a
> value, what the first element is for, and how this relates to redisplaying
> the form if there's a user error?  Currently I'm setting the value
> explicitly
>     value="and"
> which is fine for a blank form but not right for a redisplayed form.
> Should I redisplay it as
>     value = request.form['relation']
> or should I be doing something with this 'object' element?

The reason for the odd behavior is that the value (of "(value, description,
key)" doesn't have to be a string (or htmltext).   It could be dict, an object,
etc.  These more complicated data types can't (easily) be coerced into something
the browser can treat as the value of an option, so, Quixote does a little
shell
game.  You give it the value (as whatever datatype you'd like), provide a
description, and it'll give you your value back when the user selects it (even
if it doesn't fit the HTML-is-text paradigm).  As you've discovered, it
accomplishes this by using the description as the value (as far as the HTML is
concerned).  The 'key' is there in case for some reason your descriptions
aren't unique. The Qx developers figured that most of the time the descriptions
would be unique, so they just use them in the absence of explicit 'key' values.
(I believe Form will throw an exception if your descriptions aren't unique, and
you don't provide unique keys.)

Given this code:
    AND_OR = [
        ("and", "Match all words between fields (AND)"),
        ("or", "Match any words between fields (OR)") ]
    form.add_radiobuttons('relation', title='Relation', options=AND_OR,
        delim=htmltext("
\n"), value="and") "and" will start off selected, but you'll see: ''' ''' ... in the browser (or something to that effect). To this I say "don't worry about it." Or, just realize that the value in the add_radiobuttons() call does not directly turn into the value in the options tag - the description does, or the key, if provided. When you check the value in the form using Qx (i.e., server side), you'll get 'and' or 'or', not 'Match all...' or 'Match any...', because that's what you provided in the options list for add_radiobuttons(). You will see this same behavior for select lists, as you've no doubt already deduced. Hope that helps clear things up, Jason
reply