required=True

48 views
Skip to first unread message

Andrea Fae'

unread,
Dec 28, 2016, 7:24:57 AM12/28/16
to web2py-users
Why if I use this option in Field table database but when I insert and I leave not typed this field the system accepted it?

Leonel Câmara

unread,
Dec 28, 2016, 9:55:50 AM12/28/16
to web2py-users
Probably you are sending it an empty string so there is a value there.

Anthony

unread,
Dec 28, 2016, 11:14:01 AM12/28/16
to web2py-users
On Wednesday, December 28, 2016 at 7:24:57 AM UTC-5, Andrea Fae' wrote:
Why if I use this option in Field table database but when I insert and I leave not typed this field the system accepted it?

If you are making insertions via a form, you are better off using a validator:

Field('myfield', requires=IS_NOT_EMPTY())

By default, the above will disallow empty strings (which is what you get if you simply leave the form field blank). Validators are the proper approach for validating form input because they do not result in exceptions but instead display user-friendly error messages on the page. If you set required=True and fail to submit a value, you will get an exception and an error ticket, with no feedback to the user.

Anthony
 

Paolo Caruccio

unread,
Dec 28, 2016, 5:24:33 PM12/28/16
to web2py-users
In addition to the Anthony's answer, if you want also experiment simple native client side validation for web form, you could customize the field widget

Field('myfield', 'string', requires=IS_NOT_EMPTY(), widget=lambda f,v:SQLFORM.widgets.string.widget(f,v,_required=True))

Reply all
Reply to author
Forward
0 new messages