On Mon, Nov 24, 2008 at 8:38 PM, Gerry <[EMAIL PROTECTED]> wrote:
>
> Hello,
>
> I have a simple form:
>
> class ReportForm(TableForm):
> class fields(WidgetsList):
> passed = CheckBox(label_text='Passed Year')
> printRep = CheckBox(label_text='Print Report Card')
> id = HiddenField()
>
> report_form = ReportForm("report_form", action='reportsave',
> submit_text="Update")
>
> which is displaying OK on my template with:
> ${tmpl_context.form(report)}
> where 'report' has attributes id, passed and printRep and the
> CheckBoxes are getting set correctly depending on the data.
>
> my 'reportsave' controller looks like this:
> @expose()
> def reportsave(self, **kw):
> reportData = DBSession.query(Report).filter(Report.id==kw
> ['id']).one()
> reportData.passed = kw['passed']
> reportData.printRep = kw['printRep']
>
> If both the checkboxes are set initially, I can press the Submit
> button and everything works fine.
> kw is a dict with keys corresponding to the CheckBoxes and 'True' for
> their values.
>
> If however, one of the CheckBoxes is unset, I get a KeyError on that
> CheckBox in the reportsave method after submitting.
> The kw dict does not contain either the id of the CheckBox (e.g.
> 'passed') or its value (False?).
> So, kw only contains the CheckBox identifier if the CheckBox is True/
> Set.
>
> Is the KeyError the intended way to know that the CheckBox is
> unchecked?
> Is this expected behaviour?
>
This is basic html behavior, you don't get a key if no values are
selected. Therefore I assume TW is honoring this. you could either
a- pass in attrs to the function with sane defaults for example
@expose()
def reportsave(self, passed=False,printRep=False):
reportData = DBSession.query(Report).filter(Report.id==kw
['id']).one()
reportData.passed = passed
reportData.printRep = printRep
or
b- you could use dict.get
@expose()
def reportsave(self, **kw):
reportData = DBSession.query(Report).filter(Report.id==kw
['id']).one()
reportData.passed = kw.get('passed',False)
reportData.printRep = kw.get('printRep',True)
it really depends on your style.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---