I'm trying to submit multi-checkbox (Formulator widget) values to a database using ZSQL. My database schema defines the fields as boolean; Formulator converts the values to a list of checked items, e.g. ['checked_item_one', 'checked_item_two']. Only checked items are added to the list. How can I map the formulator output to proper SQL? (a list of booleans) I have a couple of ideas, but I'm new to Zope and not sure what would be best, or even possible. Thanks Norman Lorrain
Norman Lorrain wrote at 2003-5-25 23:18 -0600:
I'm trying to submit multi-checkbox (Formulator widget) values to a database using ZSQL.
My database schema defines the fields as boolean; Formulator converts the values to a list of checked items, e.g. ['checked_item_one', 'checked_item_two']. Only checked items are added to the list.
How can I map the formulator output to proper SQL? (a list of booleans) I have a couple of ideas, but I'm new to Zope and not sure what would be best, or even possible.
You are not very specific about how your SQL should look like, e.g. whether it should be a "select" or an "insert/update" sql command. For a "select", the "multiple" attribute of "dtml-sqltest" may be helpful. For an "update" (and similarly an "insert"), you would want something like ... set ... item_x1 = true, item_x2 = true, ... item_y1 = false, ... item_y2 = false, ... You can do this through iteration. Assume "checkedList" is the list of checked items, you can derive the list of unchecked items ("uncheckedList") by means of "reorder" (documented in your embedded online help --> DTML Reference --> Functions). It looks something like: <dtml-let uncheckedList="_.reorder(allItems,without=checkedItems)"> ... <dtml-in checkedList>&dtml-sequence-item; = true,</dtml-in> <dtml-in uncheckedList>&dtml-sequence-item; = false,</dtml-in> ... </dtml-let> The biggest problem is to get the SQL syntax right, i.e. have a "," at precisely the correct places. Sometimes, it is easy to ensure that there is an additional "COL = VAL" after the "dtml-in". If not, you must add tedious logic to suppress the tailing ",". It may help in this case to build the whole sequence of "COL = VAL" in Python, where logic is much easier. Dieter
At 23:18 2003-05-25 -0600, Norman Lorrain wrote:
I'm trying to submit multi-checkbox (Formulator widget) values to a database using ZSQL.
My database schema defines the fields as boolean; Formulator converts the values to a list of checked items, e.g. ['checked_item_one', 'checked_item_two']. Only checked items are added to the list.
How can I map the formulator output to proper SQL? (a list of booleans) I have a couple of ideas, but I'm new to Zope and not sure what would be best, or even possible.
Maybe you've found a solution to this already. Since with checkboxes, non-existances == false. This is not always good so you'll have to provide some more details. I usually do something like this: <input type="checkbox" name="ids:list" value="abc123"> <input type="hidden" name="askedfor_ids:list" value="abc123"> Then in a Python Script I get a list of possible answers and a list of answers. By comparing them, I can tall which ones were set to false (i.e. unticked) Don't go directly from DTML to a SQL Method. Bad practise. Get the habit of always controlling the flow with Python Scripts. Peter
Thanks
Norman Lorrain
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
participants (3)
-
Dieter Maurer -
Norman Lorrain -
Peter Bengtsson