Validating field with IS_EXPR

34 views
Skip to first unread message

Jorrit

unread,
Jan 18, 2017, 12:13:36 PM1/18/17
to web2py-users
I have this model, where I want to validate that the end_timeslot is not below the start_timeslot:

db.define_table('staffingexception',
                Field('employee_id', 'reference auth_user'),
                Field('from_date', 'date', required=True),
                Field('to_date', 'date', required=True),
                Field('from_location', 'reference location', required=True),
                Field('to_location', 'reference location', required=True),
                Field('start_timeslot', 'text', required=True),
                Field('end_timeslot', 'text', required=True),
                )

db.staffingexception.to_location.requires = IS_IN_DB(db, 'location.id', '%(name)s')
db.staffingexception.end_timeslot.requires = IS_EXPR(lambda value: int(value) > int(request.post_vars.start_timeslot),
                                                     error_message='Eindtijd moet na de starttijd liggen.')

This raises either True of False as an error, instead of passing it when the expression evaluates to True. What am I doing wrong?

Anthony

unread,
Jan 18, 2017, 5:23:20 PM1/18/17
to web2py-users
If you are going to use a callable as the first argument to IS_EXPR, then that callable should either return None (if no error) or an error message (not sure why it was done that way -- and it's not documented).

Anthony

Jorrit

unread,
Jan 19, 2017, 5:00:13 AM1/19/17
to web2py-users
Thanks, good to know...
Reply all
Reply to author
Forward
0 new messages