Just to comment, get back to Mengu's original post:

I believe the problem Mengu is seeking to solve:   To have field-
specific feedback per specific validator failure.

On Mar 15, 4:52 am, Mengu <[email protected]> wrote:
> 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"

The current way to do this:

db.users.name.requires = IS_NOT_EMPTY( error_message="Please enter
your name")


Without a table (as per Carlo's later request):

form=FORM('Your name:',
             INPUT(_name='name',
requires=IS_NOT_EMPTY(error_message="Please enter your name"),
             INPUT(_type='submit'))

> 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"]

in either way you are doing this (db field type, or FORM-INPUT), the
way is similar:

....requires=[IS_EMAIL(error_message="Given e-mail is not a valid
one."), IS_NOT_IN_DB(db, 'users.email', error_message="Sorry, this e-
mail already exists in our database")]

Hope this helps clarify.

Regards,
- Yarko
>
> 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...
>
> 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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en.

Reply via email to