Introduction
In this blog, we will discuss Date and Time Validators in web2py. Before discussing the date and time validators, let's look briefly at what web2py is and what are validators.
Web2py is a free and open-source web application framework written in Python. Web2py allows web developers to use Python to create dynamic web content. Web2py is intended to help decrease time-consuming web development activities such as making web forms from scratch.
Web2py was created as a training tool emphasizing usability and deployment. As a result, it lacks project-level configuration files. The Ruby on Rails and Django frameworks inspired the creation of web2py. Web2py, like these frameworks, emphasizes rapid development, prefers convention over configuration, and adheres to the model-view-controller (MVC) architectural paradigm.
Now that we have seen what web2py is let's look at what are validators in web2py.
Validators
They are classes used to check input fields (including forms generated from database tables). Validators use SQLFORM advanced forms to build widgets such as drop-down menus and lookups from other tables.
Here's an example of a validator being used with a FORM field:
INPUT(_name='abc', requires=IS_INT_IN_RANGE(0, 10))
Validators are always assigned to fields via the "requires" attribute. A field may have a single or several validators. Here's an example of how to make a table field require a validator:
db.define_table('person', Field('name'))
db.person.name.requires = IS_NOT_EMPTY()
Normally, validators are invoked by the function that takes and processes a FORM or another HTML helper object that contains a form. They are called in the order that they are listed. Validators can also be called specifically for a field:
db.person.name.validate(value)
which produces a tuple (value, error), with the error set to None if the value is valid.
Some types of Validators in web2py are:
- Text Format Validator
- Date and Time Validators
- Range, set and equality Validators
- Complexity and Security Validators
- Special Type Validators
- Database Validators