Hi all,

Currently web2py validation works on CRUD and SQLFORM. But some of us
don't use those and build their forms by hand. But when this is the
issue we have to validate every input ourselves in the controller
which is not nice so I have came up with an idea. I can set an
error_message per field object as an addition to requires attribute.
Let's say this is our table:

db.define_table(
    'users',
    Field('name'),
    Field('email')
    )

And this is how we can define our requires attribute:
db.users.name.requires = IS_NOT_EMPTY()
db.users.email.requires = [IS_EMAIL(), IS_NOT_IN_DB(db,
'users.email')]

Now, we can add our error messages like this:
db.users.name.requires = IS_NOT_EMPTY()
db.users.name.error_message = "Please enter your name"
db.users.email.requires = [IS_EMAIL(), IS_NOT_IN_DB(db,
'users.email')]
db.users.email.error_message = ["Given e-mail is not a valid one.",
"Sorry, this e-mail already exists in our database"]

My patch for object validation is working when the insert method is
called. So the following example will return a list of errors:

print db.users.insert(name="", email="b...@email")

and the return is: ['Please enter your name', 'Given e-mail is not a
valid one.']

so we can iterate over the errors and show them.

you can see my addition here: 
http://github.com/mengu/web2py-patches/blob/master/gluon/sql.py#L1888-1904

let me know what you think or if i should follow any other way for
this.

-- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To post to this group, send email to web...@googlegroups.com.
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en.

Reply via email to