Hi,
I am using a CascadingSingleSelectField widget in conjuntion with a
SingleSelectField widget as follows:
Widget form:
class CommitmentForm(TableForm):
show_errors = True
resources = DBSession.query(Resource.resourceid,
Resource.name).order_by(Resource.name).all()
resources.insert(0,['',''])
fields = [
TextField('contractref', label_text='Contract Ref :',
validator=NotEmpty(), size=25),
TextField('description', label_text='Description :',
validator=NotEmpty(), size=55),
CascadingSingleSelectField('resource',
label_text='Resource :', validator=Int(not_empty=True),
options=resources, cascadeurl=url('/getSites')),
SingleSelectField('site', label_text='Site :',
validator=Int(not_empty=True)),
HiddenField('projectfundingid', default=0),
HiddenField('commitmentid', default=0),
Spacer()]
addCommitmentForm = CommitmentForm('addCommitmentForm',
submit_text='Save Commitment', action='/commitment/saveAdd')
Function in root.py for cascadeurl:
@expose("json")
def getSites(self, value, *args, **kw):
sites = DBSession.query(Site.siteid,
Site.name).filter(Site.resourceid==value).order_by(Site.name).all()
sites.insert(0,['',''])
return {'site':sites}
And finally, functions in my CommitmentController class:
@expose(template="projects.templates.commitment.addCommitment")
def add(self, projectfundingid=0, *args, **kw):
try:
pf =
DBSession.query(ProjectFunding).filter(ProjectFunding.projectfundingid==int(projectfundingid)).one()
except:
pf =
DBSession.query(ProjectFunding).filter(ProjectFunding.projectfundingid==int(kw['projectfundingid'])).one()
tmpl_context.form = addCommitmentForm
return dict(value={'projectfundingid':pf.projectfundingid})
@validate(form=addCeaCommitmentForm, error_handler=add)
@expose()
def saveAdd(self, **kw):
"""Handle submission from the add commitment form and save the
commitment record."""
try:
projectfunding =
DBSession.query(ProjectFunding).filter(ProjectFunding.projectfundingid==int(kw[projectfundingid'])).one()
site =
DBSession.query(Site).filter(Site.siteid==int(kw['site'])).one()
except:
flash(_('Unable to add retrieve commitment details!'),
'error')
redirect(url('/commitment'))
commitment = Commitment()
commitment.contractref = kw['contractref']
commitment.description = kw['description']
commitment.site = site
commitment.projectfunding = projectfunding
DBSession.add(commitment)
try:
DBSession.flush()
except:
DBSession.rollback()
flash(_('Unable to add commitment'), 'error')
redirect(url('/commitment'))
flash(_('Commitment has been added'), 'ok')
redirect(url('/projectfunding/display/%d' %
(commitment.currency.projectfunding.projectfundingid)))
The idea is that after the form is loaded the user selects a resource
(CascadeSingleSelectField) and the site field (SingleSelectField) is
updated with a subset of data by the cascadeurl function (getSites),
depending on the resource selection.
This works fine, however the problem is when the form fails validation
(say when the description field is left empty, for example), the form
reloads without the SingleSelectField (site) data. It seems that the
form does not keep the previous selection and is empty (as if it was
loaded for the first time). Is this how it is supposed to work, or am
I missing something.
Thanks,
Michael
--
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.