This the pattern I am using:
class Customers:
#you need to have an _instance_ of widgets.TableForm within your
class!
email_form = widgets.TableForm(fields
=[widgets.HiddenField(name="mailID"),
widgets.HiddenField(name="customerID"),
widgets.TextField(validator=validators.Email(not_empty=True),
label
= "Email", name="address"),
widgets.TextField(validator=validators.NotEmpty(), label =
"Contacto",
name="contact"),
widgets.CheckBox(label="Mandar Pruebas", name="draftshere")])
...
@turbogears.expose(template="jobtickets.templates.edit_customer_email")
def editform_email(self, mailID):
try:
mailID = int(mailID)
mail = model.CustomerEmail.get(mailID)
data = dict(mailID=mail.id, customerID=mail.customer,
address=mail.address, contact=mail.contact,
draftshere=mail.draftshere)
except (ValueError, model.SQLObjectNotFound):
raise cherrypy.NotFound
return dict(data=data, form=self.email_form)
@turbogears.expose()
@turbogears.validate(email_form)
@turbogears.error_handler(editform_email)
def edit_email(self, mailID, customerID, address, contact,
draftshere=False):
try:
mailID = int(mailID)
mail = model.CustomerEmail.get(mailID)
mail.address = address
mail.contact = contact
mail.draftshere = draftshere
except (ValueError, model.SQLObjectNotFound):
raise cherrypy.NotFound
raise redirect(turbogears.url("/customers"))
...
class Root(controllers.RootController):
...
customers = Customers()
...
this is your template:
<p py:content="form(value=data, action=tg.url('/customers/
edit_email'),
submit_text='Guardar')">Email Form</p>
On 13 jun, 11:07, Tjaart <[EMAIL PROTECTED]> wrote:
> Just one typo in the code... the @validator should read
> @validator(form=form) NOT @validator(form=modelling_form)
>
> If possible could you also paste the form code with the template code?
>
> Thanks!
>
> On 13 Jun, 10:42, [EMAIL PROTECTED] wrote:
>
> > It took me a short while as well to figure out how it works. The
> > problem is that you need an _instance_ of the form in your class that
> > you can pass to to the @validate decorator, a class def as shown in
> > some of the tutorials is not enough. This also means that you cannot
> > set values in the form within the script, but there is a very easy and
> > handy way to do that in the template. I'll post a code snippet later
> > today.
>
> > On 13 jun, 08:43, "Tjaart de Beer" <[EMAIL PROTECTED]> wrote:
>
> > > Hi
>
> > > I have a problem with form validation not working. Here is the setup.
>
> > > I have generic methods in controllers.py called view and model_view.
> > > The appropriate method (either method X which displays the form, or
> > > method Y which handles the form input) located in a sub_controllers.py
> > > is then decided on by dispatch.
>
> > > Here is my code for the form:
>
> > > class ModellingFields(widgets.WidgetsList):
> > > template = widgets.TextField(validator = validators.NotEmpty(),
> > > label = "Template")
> > > target = widgets.TextField(validator = validators.NotEmpty(),
> > > label = "Target")
> > > program = widgets.TextField(validator = validators.NotEmpty(),
> > > label = "program:")
>
> > > form = widgets.TableForm(fields = ModellingFields(),submit_text =
> > > "Submit")
>
> > > Method X which displays the form:
>
> > > @generics.view.when("viewtype.startswith('modelling')")
> > > def X(sid,viewtype,tg_errors=None):
>
> > > if tg_errors:
> > > print tg_errors, "********************************************"
> > > submit_action = "model_view?viewtype=modelling_result"
> > > return
> > > dict(tg_template=".templates.modelling",sid=sid,form=modelling_form,
> > > action=submit_action,tg_errors=tg_errors)
>
> > > Method Y which should handle the validation and if correct proceed:
>
> > > @validate(form=modelling_form)
> > > @error_handler(X)
> > > @generics.model_view.when("viewtype == 'modelling_result'")
> > > def Y(viewtype, target, template, program,**kwargs):
> > > result = modelling.modelling(target, template, program)
> > > return dict(tg_template=".templates.modelling_results", result =
> > > result)
>
> > > Code for the generic function which dispatches it:
>
> > > @expose()
> > > def view(self, sid="",viewtype=""):
> > > try:
> > > return generics.view(sid,viewtype)
> > > except dispatch.interfaces.NoApplicableMethods:
> > > return dict(tg_template=".templates.missing_record",sid=sid)
>
> > > @expose()
> > > def model_view(self, viewtype, target, program, template):
> > > try:
> > > return generics.model_view(viewtype, target, template,
> > > program)
> > > except dispatch.interfaces.NoApplicableMethods:
> > > return dict(tg_template=".templates.missing_record",sid="")
>
> > > I followed the example in the Turbogears book as well as numerous
> > > tutorials on the web but I simply do not get any validation. I just
> > > want the same form to be redisplayed if one of the fields are empty
> > > and a warning next to the form.
>
> > > Is the problem because I use dispatch?
>
> > > Any help would be appreciated. Thanks!
> > > --
> > > Tjaart de Beer
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---