Thanks to everyone who has explained! I'm very grateful. Unfortunately
it takes time for me to digest it all, so I only start with the
validation stuff.
On 9/6/06, Alberto Valverde <[EMAIL PROTECTED]> wrote:
> > The other reason is that I don't want to use TurboGears widgets, and
> > @validate and @error_handler seem to be tightly coupled to the widget
> > system.
>
> Wrong again. "validate" is tightly coupled with FormEncode *only*.
> You can skip widgets altogether and use plain FE validators with it:
> @validate(validator={'param1':Int, 'param2', WhatEver})
> or
> @validate(validator=Schema(....))
Ok, understood. But, IMHO, the parameter name "form" in the validate()
decorator gives the impression that there is a relationship to some
kind of "form"... I assume you meant to name the keyword parameter
"validators" instead of "validate"?
> If you look at validate's source, you can even notice that form
> doesn't have to be a widget at all. You can pass any object that has
> a "validate" method and raises FE's Invalids on wrong input.
I've tried just that. Here is controllers.py from
http://trac.turbogears.org/turbogears/wiki/SimpleWidgetForm rewritten
to work without widgets:
######################################################################
import time
from cherrypy import request
from turbogears.controllers import (RootController, expose, error_handler,
flash, redirect, validate)
import formencode
from formencode import validators
class Comments(list):
def add(self, name, email, text):
self.append((name, email, text, time.ctime()))
comments = Comments()
class CommentSchema(formencode.Schema):
name = validators.String(not_empty = True)
email = validators.Email(not_empty = True)
comment = validators.String(not_empty = True)
notify = validators.Bool()
class Root(RootController):
@expose(template=".templates.index")
def index(self):
return dict(comments=comments)
@expose(template=".templates.add")
def add(self, tg_errors=None):
if tg_errors:
# If there are errors, then the request has the attributes
# 'input_values' and 'validation_errors'
print request.input_values
print request.validation_errors
flash("There was a problem with the form!")
print tg_errors
return dict(name = "",
email = "",
comment = "",
notify = False)
@expose()
@validate(validators = CommentSchema())
@error_handler(add)
def save(self, name, email, comment, notify=False):
comments.add(name, email, comment)
if notify:
flash("Comment added! You will be notified.")
else:
flash("Comment added!")
raise redirect("index")
######################################################################
I also changed the add.kid template to match Root's add() method. It
renders the four parameters manually instead of using the widgets
display() method. When I run the code and submit the form, I always
get the error "The input field 'self' was not expected"
Also, the attributes input_values and validation_errors aren't
assigned to the request object. Shouldn't they be?
Are these problems bugs or have I fundamentally misunderstood
something?
--
mvh Björn
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---