[FE-discuss] Multiple HTML fields to one Python value, …

Top Page
Author: Mike Orr
Date:  
To: formencode-discuss
Subject: [FE-discuss] Multiple HTML fields to one Python value, and vice-versa
I have three composite fields to port to FormEncode & Pylons, and I'm
not sure how to go about it. The correspondence of Python values to
HTML values is not 1:1. In Quixote I used a composite widget
                 (multiple HTML controls), with a tuple for the Python
value. But I can't figure out what to do in FormEncode. I can't
figure out how to split one Python value to multiple HTML values or
vice-versa, or how to use my normal Python values as the "Python" side
of a validator rather than converting it to a different "validator
Python" value. Here's what I'm trying to do:

* * * *

1) Latitude/longitude widget. The Python value is a floating point,
possibly None. The HTML side is a set of text fields, radio buttons,
and pulldowns:

    * Input style: degrees
        ____ °  North/South
    * Input style: degrees/minutes
        ____ °____ '    North/South
    * Input style: degrees/minutes/seconds
        ____ ° ____ '  ____ "  North/South

When the user chooses an input style, Javascript shows the
corresponding input row and hides the other two. Non-Javascript users
see all input rows. The initial value is calculated in all three
styles to prepopulate the controls. (45.2083° = 45° 12.50' = 45°
12' 30.0"). There's a single error message for the entire group of
controls.

Is it possible to have a .to_python that combines these all to a
single float, and a .from_python that splits them out to a sub-dict?
How would I do it? And can I trust webecode's dotted notation for
this?

As for prepopulating the fields, I'm doing this all myself due to
FormEncode's limitations with boolean values. But I have to split out
the values somehow, and it would be logical to call the validator to
do it and then place the values in the form. So I'd like the
validator to be round-trip safe.

* * * *

2) Quantity range widget. The HTML has two text fields and a pulldown
for the unit:

      min _____   max _____   pounds
                                        kilograms
                                        gallons
                                        barrels
                                        ...

These are stored as three string literals, to reconstruct the original
input. Additionally, two floats and a boolean are calculated and
stored: the values themselves in gallons or pounds, and a switch
telling whether the values are mass (pounds) or volume (gallons). The
"max" field may be blank, indicating a single value rather than a
range. Or both numbers may be blank, meaning a null answer.

This is the opposite situation as (1): more Python values than HTML
values. The calculated fields are only one way (.to_python), and I
could do them outside the validator, except I have to check whether
the input is valid anyway and it makes sense to do all the
calculations in one place. So how would I express this in FormEncode?

* * * * *

3) Attachment manager. The database stores the filename (string), and
the file is placed in the filesystem. If there's no existing file,
the HTML shows a normal file upload control. If there is an existing
file, the HTML shows some info and radio buttons:

         [thumbnail of existing attachment if image or PDF]
         [type and size of existing attachment]
         * Keep existing attachment
         * Replace attachment with [file upload control]
         * Delete attachment

Again a single Python value is split to multiple HTML controls. And
the number of HTML values changes depending on whether there's an
existing attachment. In my Quixote form the Python is a tuple:
(action, upload), where action is "keep", "replace", or "delete". An
additional action "add" is used as a dummy value if there's no
existing attachment; with a hidden HTML field. The download must be
non-null for "replace", may be non-null for "add", and must be null
for "keep" or "delete", otherwise a user error. How would I make a
validator for this?

-- 
Mike Orr <sluggoster@???>

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
FormEncode-discuss mailing list
FormEncode-discuss@???
https://lists.sourceforge.net/lists/listinfo/formencode-discuss