(Solution) Problem with rendering a checked CheckBoxField using redender_from_request()
Sebastian Ware <[email protected]> Wed, 25 Oct 2006 17:18:36 +0200
| Newsgroups | gmane.comp.web.zope.formulator.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi!
Thanks to Alexis Roda for helping me out on this matter.
The problem was that I was using Formulator render_from_request() to
render a form with a couple of CheckBoxFields and some other fields.
The form was used to update propeties in a PropertyFolder
(Abracadbraobjects). So I would set the form parameters in the
request in order for the form to render the current values. It worked
well for TextAresField, StringField, ListField. But it failed to
render the CheckBoxField checked. I tried setting the key/value pair
to basically everything I could think of
(True/"on"/"checked"/"Checked"/1/"pretty please"...). Three hours of
research, trial and error...
As it turns out you should allways add the key
"formulator_submission" to the request when rendering a Formulator
form with data other than the default values set in the ZMI-interface.
# Set formulator submission
request.form.update({'formulator_submission':True})
This does the trick and suddenly the CheckBoxFields are rendered as
checked when they should, and unchecked when they shouldn't.
Python script (sending "property" of the type PropertyFolder as
single parameter). the name of the Formulator form is "form":
# Import a standard function, and get the HTML request and response
objects.
from Products.PythonScripts.standard import html_quote
request = container.REQUEST
RESPONSE = request.RESPONSE
# Set the mapping information using the dictionary in the script
mapping()
mapping = context.mapping()
# Set formulator submission key
request.form.update({'formulator_submission':True})
# Load the form field with data from PropertyFolder.
for field in container.form.get_fields():
prop = property.getProperty( mapping.get(field.getId()) )
if field.meta_type == "CheckBoxField":
if prop:
request.form.update({'field_' + field.getId():True})
else:
request.form.update({'field_' + field.getId():False})
else:
request.form.update({'field_' + field.getId():str(prop)})
Regards
Sebastian