Are there TG validator examples?

3 views
Skip to first unread message

Guyon Morée

unread,
Dec 13, 2005, 7:25:57 PM12/13/05
to TurboGears
I couldn't find any.

What I'm looking for is an example that uses validator schema's, but
also shows the roundtrip to the input screen which tells the user whats
wrong.

Does someone have any?

thanx,
Guyon Moree
http://gumuz.looze.net/

Karl Guertin

unread,
Dec 13, 2005, 8:59:45 PM12/13/05
to turbo...@googlegroups.com
On 12/13/05, Guyon Morée <guyon...@gmail.com> wrote:
> What I'm looking for is an example that uses validator schema's, but
> also shows the roundtrip to the input screen which tells the user whats
> wrong.

While it's not a direct example, the validators are actually
FormEncode[1], which is fairly well documented on the website.
htmlfill (same site) is used to complete the trip from the validator
back to the browser.

[1]http://formencode.org/

It's not what you were looking for, but it'll give you something to go
on if nobody has an example.

Karl

Jeremy Jones

unread,
Dec 14, 2005, 11:47:20 AM12/14/05
to turbo...@googlegroups.com
I've been digging for examples of this as well. Now, I'm also looking
for examples of using widgets... If anyone has a couple of really
simple examples of creating a form using formencode and validating
against something (a SQLObject would be nice), I would love to get my
hands on them. I'd love to see the same thing for widgets. If anyone
has examples, I'd be willing to put them into a suitable format for the
wiki/formal documentation.


- jmj

manch...@gmail.com

unread,
Dec 14, 2005, 5:54:04 PM12/14/05
to TurboGears
I recently got my simple widget example working (thanks Kevin!). I'll
share my simple example here and try to explain it.

Start with creating a form. (I did this in a seperate file since I
didn't want to clutter up controllers.py)

myForms.py:
createUserForm = widgets.TableForm(widgets=[
widgets.TextField("userId",
validator=validators.PlainText()),
# widgets.TextField("userId",
validator=validators.PlainText(not_empty=True)),
widgets.TextField("emailAddress",
validator=validators.Email()),
widgets.TextField("emailAddress"),
widgets.TextField("age", default=0,
validator=validators.Int())])

That is your widget table form which you'll use in controllers.py:

@turbogears.expose( html="my.templates.createUser")
def createUser( self):
return dict(message="", form=myForms.createUserForm)

By passing form=myForms.createUserForm you are making form accessible
to the kid template. Later in the kid timplate you can simply call
form.insert(...)

You'll probably want a way to save that data too. (I've made it a
seperate function for simplicity.

Note that I've set inputform=myForms.createUserForm, this tells TG that
I'm using that form to validate.
@turbogears.expose( html="my.templates.createUser",
inputform=myForms.createUserForm)
def createUserSave( self, userId='', emailAddress='', age='',
submit=''):
# Check for validation errors
if cherrypy.request.form_errors:
return dict(message="", form=myForms.createUserForm)

# The user doesn't exist. Create the user
hub.begin()
u=TG_User( userId=userId, emailAddress=emailAddress,
displayName=userId, password="passwd" )
g=TG_Group.byGroupId("admin")
u.addTG_Group(g)
hub.end()
raise cherrypy.HTTPRedirect(turbogears.url("/login"))


So now we just have to use this form in the kid template

createUser.kid
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://purl.org/kid/ns#"
py:extends="'master.kid'">

<head>
<meta content="text/html; charset=UTF-8"
http-equiv="content-type" py:replace="''"/>
<title>Create User</title>
</head>

<body>
<h2>Create Account</h2>
<p>${message}</p>
<div py:replace="form.insert(action='createUserSave')"/>
</body>
</html>

The magic line is <div
py:replace="form.insert(action='createUserSave')"/>.

This will pretty much work how you'd expect. If the age is not a valid
number, it will return you to the input form with the correct error
message. I hope I got this all correct and explained it in a way that
isn't too twisted.

Caveat Emptor:
I have found a bug
widgets.TextField("userId",
validator=validators.PlainText(not_empty=True))
causes an error. I'm not sure why inserting not_empty=True does this,
but the insert code for the widgets doesn't like it. I've also not been
able to get the validdators.Email() to work as a widget validator
either. :(

Anyway, that should do it.

I was thinking that it might be nice to generate a list of useful
widgets that should be created. I was thinking something like
changePasswordForm - old password, new password field 1, new password
checked against field 1
loginForm - username, password
creditCardForm - card number, expiration

Since these are common to many web applications.

A more advanced widget set my be a shopping cart...

Anyway, just a thought.

Lucas.-

Reply all
Reply to author
Forward
0 new messages