Thanks Diez, if I try the first option, how can I access the site
field from the controller? Would this be via child_args in the
template? Not sure what you mean by tmpl_context-variable.

Rgds,
Michael

On Apr 18, 7:41 am, "Diez B. Roggisch" <[email protected]> wrote:
> Am 17.04.2011 um 15:56 schrieb mpearce:
>
>
>
>
>
> > 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:I
> >            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.
>
> I have newer worked with this, but I wonder where the site field  
> should get the data from on the re-load. Because it normally gets it  
> from a change in the resource-field, but there is no change - it's  
> just reloaded.
>
> I can see two fixes for this: either setting the options of the site-
> field in the controller for the currently selected resource. You could  
> do that with a callable that returns the values based on a  
> tmpl_context-variable which can be set - or not.
>
> The other option would be to force the CSSF to re-load the data, maybe  
> by a piece of jquery that emits an artificial change-event on load of  
> the form.
>
> Diez- Hide quoted text -
>
> - Show quoted text -

-- 
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.

Reply via email to