Thanks, that worked a treat. Add benefit - I can go back to using
WidgetsDeclaration, and I can now also abstract everything in to
forms.py - double win! Cheers.

Code posted below for anyone who wants to see it.

Cheers

-Rob

{{{
# forms.py
from model import *
from turbogears.widgets import *
from turbogears.validators import *

def get_all_projects():
        return [(p.id, p.title) for p in Project.select()]

def get_all_adcodes():
        return [(a.id, a.name) for a in Adcode.select()]

class ProjectsList(WidgetsList):
        projectid = SingleSelectField(label="Project",
                options=get_all_projects, validator=NotEmpty)

class ProjectForm(WidgetsList):
        title = TextField(label="Project title", validator=NotEmpty)
        domain = TextField(label="Domain", validator=NotEmpty)
        adcode = SingleSelectField(label="Advertisements",
                options=get_all_adcodes, validator=Int)
        copy = TextArea(label="Copy", validator=NotEmpty)
        keywords = TextArea(label="Keywords", validator=NotEmpty)
        projectid = HiddenField()

class AdcodesList(WidgetsList):
        adcodeid = SingleSelectField(label="Adcode",
                options=get_all_adcodes, validator=Int)

class AdcodeForm(WidgetsList):
        name = TextField(label="Adcode name", validator=NotEmpty)
        adcode = TextArea(label="Adcode HTML", validator=NotEmpty)
        adcodeid = HiddenField()


allprojects = TableForm(fields=ProjectsList(), submit_text="Edit")
project = TableForm(fields=ProjectForm(), submit_text="Save")
alladcodes = TableForm(fields=AdcodesList(), submit_text="Edit")
adcode = TableForm(fields=AdcodeForm(), submit_text="Save")
}}}


Michele Cella wrote:
> Hi Robin,
> 
> Unfortunately your code is unreadable here on the g-groups UI, anyway
> you don't need this Forms class, what you need is just passing a
> callable as options to the SingleSelectField, for example:
> 
> def get_options():
>      return [(p.id, p.title) for p in Project.select()]
> 
> and then:
> 
> select = widgets.SingleSelectField(name="projectid", label="Project",
> options=get_options, validator=validators.NotEmpty)
> 
> use this inside a simple TableForm, for example:
> 
> class MyFields(widgets.WidgetsList):
>     name = TextField()
>     projectid = select
> 
> robin_form = TableForm(fields=MyFields())
> 
> and then:
> 
> @validate(form=robin_form)
> 
> Hope this helps, and sorry for the lack of good doc. :-(
> 
> Ciao
> Michele
> 
> PS
> If you want to send me the code I can be more precise probably (when I
> get home). ;-)
> 
> Robin Haswell wrote:
> 
>>Hey there
>>
>>I have a small problem with using widgets for my form. I think the
>>problem lies in the fact that the form contains a select field which is
>>pulled from the database, and therefore needs to be "refreshed" every
>>time that data changes - this breaks links with
>>@validate(form=forms.myform), and from then on every time I submit an
>>invalid form I don't get messages and my data isn't populated.
>>
>>Below is the code for my app. You'll notice a call to forms.refresh
>>after I save an adcode. It's saving an adcode which breaks everything.
>>
>>The only solution I can think of is reloading the entire controller, but
>>it doesn't sound optimal to me. Could someone suggest another approach?
>>
>>Thanks
>>
>>-Rob
>>
>># controllers.py
>>
>>import cherrypy
>>import turbogears
>>from turbogears.toolbox.catwalk import CatWalk
>>from turbogears import controllers
>>from turbogears import identity
>>from turbogears import widgets
>>from turbogears import validators
>>from turbogears.fastdata import *
>>
>>import model
>>from model import *
>>
>>class Forms:
>>
>>        def __init__(self):
>>                self.refresh()
>>
>>        def refresh(self):
>>                self.allprojects = widgets.TableForm(fields=[
>>                        widgets.SingleSelectField(name="projectid",
>>label="Project",
>>                                options=[(p.id, p.title) for p in
>>Project.select()], validator=validators.NotEmpty)
>>                ], submit_text="Edit")
>>                self.project = widgets.TableForm(fields=[
>>                        widgets.TextField(name="title", label="Project
>>title", validator=validators.NotEmpty),
>>                        widgets.TextField(name="domain", label="Domain",
>>validator=validators.NotEmpty),
>>                        widgets.SingleSelectField(name="adcode",
>>label="Advertisements",
>>                                options=[(a.id, a.name) for a in
>>Adcode.select()], validator=validators.NotEmpty),
>>                        widgets.TextArea(name="copy", label="Copy",
>>validator=validators.NotEmpty),
>>                        widgets.TextArea(name="keywords",
>>label="Keywords", validator=validators.NotEmpty),
>>                        widgets.HiddenField(name="projectid")
>>                ], submit_text="Save")
>>                self.alladcodes = widgets.TableForm(fields=[
>>                        widgets.SingleSelectField(name="adcodeid",
>>label="Adcode", validator=validators.NotEmpty,
>>                                options=[(a.id, a.name) for a in
>>Adcode.select()])
>>                ], submit_text="Edit")
>>                self.adcodes = widgets.TableForm(fields=[
>>                        widgets.TextField(name="name",
>>validator=validators.NotEmpty),
>>                        widgets.TextArea(name="adcode",
>>validator=validators.NotEmpty),
>>                        widgets.HiddenField(name="adcodeid")
>>                ], submit_text="Save")
>>
>>forms = Forms()
>>
>>class Pyrgen(controllers.RootController):
>>
>>        catwalk = CatWalk(model, allowedHosts=["192.168.0.0/24"])
>>
>>        @turbogears.expose(template=".templates.index")
>>        def index(self, tg_errors=None):
>>                return dict(
>>                        title = "Manage Projects",
>>                        editprojectform = forms.allprojects,
>>                        projectform = forms.project
>>                )
>>        @turbogears.expose(template=".templates.adcodes")
>>        def adcodes(self):
>>                return dict(
>>                        title="Manage Adcodes",
>>                        all=forms.alladcodes,
>>                        form=forms.adcodes,
>>                        value={},
>>                )
>>
>>        @turbogears.expose(template=".templates.index")
>>        @turbogears.validate(form=forms.project)
>>        @turbogears.error_handler(index)
>>        def SaveProject(self, projectid, title, domain, adcode, copy,
>>keywords):
>>                try:
>>                        p = Project.get(projectid)
>>                        p.title = title
>>                        p.domain = domain
>>                        p.adcode = adcode
>>                        p.copy = copy
>>                except ValueError:
>>                        p = Project(
>>                                title=title,
>>                                domain=domain,
>>                                adcode=adcode,
>>                                copy=copy)
>>                keywords = keywords.split("\n")
>>                keywords = [ keyword.strip() for keyword in keywords if
>>keyword.strip() ]
>>                for keyword in p.keywords:
>>                        p.removeKeyword(keyword)
>>                for keyword in keywords:
>>                        try:
>>                                kw = Keyword.byKeyword(keyword)
>>                        except SQLObjectNotFound:
>>                                kw = Keyword(keyword=keyword)
>>                        p.addKeyword(kw)
>>                turbogears.flash("Project saved")
>>                return dict(
>>                        title="Project Saved",
>>                        editprojectform = forms.allprojects,
>>                        projectform = forms.project
>>                        )
>>        @turbogears.expose(template=".templates.editproject")
>>        def EditProject(self, projectid):
>>                p = Project.get(projectid)
>>                values = dict(
>>                        projectid=p.id,
>>                        title=p.title,
>>                        domain=p.domain,
>>                        adcode=p.adcode,
>>                        copy=p.copy,
>>                        keywords="\n".join([ kw.keyword for kw in
>>p.keywords ])
>>                )
>>                return dict(
>>                        title="Edit Project: "+p.title,
>>                        form=forms.project,
>>                        values=values
>>                )
>>
>>        @turbogears.expose(template=".templates.adcodes")
>>        @turbogears.validate(form=forms.adcodes)
>>        def SaveAdcode(self, adcodeid, name, adcode):
>>                try:
>>                        a = Adcode.get(adcodeid)
>>                        a.name = name
>>                        a.adcode = adcode
>>                except:
>>                        a = Adcode(
>>                                name=name,
>>                                adcode=adcode,
>>                        )
>>                turbogears.flash("Adcode Saved")
>>                forms.refresh()
>>                return dict(
>>                        title="Adcode Saved",
>>                        all=forms.alladcodes,
>>                        form=forms.adcodes,
>>                        value={}
>>                )
> 
> (Editing disabled while spellchecking)
> Stop spell checking
> 
> 
> > 

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

Reply via email to