Table of contents
1.
Introduction
2.
Validators
3.
Date and Time Validators
3.1.
IS_TIME
3.2.
IS_DATE
3.3.
IS_DATETIME
3.4.
IS_DATE_IN_RANGE
3.5.
IS_DATETIME_IN_RANGE
4.
Frequently Asked Questions
4.1.
What is web2py?
4.2.
What are Validators in web2py?
4.3.
How are validators assigned to fields?
4.4.
Name some Validator types in web2py.
4.5.
Why %b and %B symbols are used in date and time validators in web2py?
5.
Conclusion
Last Updated: Mar 27, 2024

Date and Time Validators In Web2py

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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))
You can also try this code with Online Python Compiler
Run Code


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()
You can also try this code with Online Python Compiler
Run Code


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)
You can also try this code with Online Python Compiler
Run Code


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

Date and Time Validators

This validator verifies that a field value has a correct time, date, or a valid datetime in the provided format.

IS_TIME

This validator verifies that a field value has a proper time in the format supplied.

requires = IS_TIME(error_message='must be HH:MM:SS!')
You can also try this code with Online Python Compiler
Run Code

IS_DATE

This validator verifies that a field value has a valid date in the format supplied. To support multiple formats in different locations, it is best practice to indicate the format using the translation operator.

requires = IS_DATE(format=T('%Y-%m-%d'),
                   error_message='must be YYYY-MM-DD!')
You can also try this code with Online Python Compiler
Run Code

IS_DATETIME

This validator ensures that a field value has a valid datetime in the format supplied. To support multiple formats in different locations, it is best practice to indicate the format using the translation operator.

requires = IS_DATETIME(format=T('%Y-%m-%d %H:%M:%S'),
                       error_message='must be YYYY-MM-DD HH:MM:SS!')
You can also try this code with Online Python Compiler
Run Code


For the format string, the following symbols can be used:

Symbol Meaning Example
%Y It shows the year with the century. 2022
%y It shows the year without a century. 22
%d It shows the day of the month. 13
%m It shows the month. 07
%b It shows the abbreviated month name. Jan
%B It shows the full month name. January
%H It shows the hours in 24-hour format. 23
%I It shows the hours in a 12-hour format. 11
%p It shows either AM or PM. PM
%M It shows the minutes. 30
%S It shows the seconds. 45

IS_DATE_IN_RANGE

Similar to the previous(IS_DATE) validator, but allows you to select a range:

requires = IS_DATE_IN_RANGE(format=T('%Y-%m-%d'),
                   minimum=datetime.date(2001, 1, 22),
                   maximum=datetime.date(2022, 6, 30),
                   error_message='must be YYYY-MM-DD!')
You can also try this code with Online Python Compiler
Run Code

IS_DATETIME_IN_RANGE

Similar to the IS_DATETIME validator, but with the ability to provide a range:

requires = IS_DATETIME_IN_RANGE(format=T('%Y-%m-%d %H:%M:%S'),
                       minimum=datetime.datetime(2001, 1, 1, 10, 30),
                       maximum=datetime.datetime(2022, 6, 30, 11, 45),
                       error_message='must be YYYY-MM-DD HH:MM::SS!')
You can also try this code with Online Python Compiler
Run Code

That is all for date and time validators in web2py. We have discussed all the methods provided by web2py for date and time validation. Now let us see some FAQs.

Frequently Asked Questions

What is web2py?

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.

What are Validators in web2py?

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.

How are validators assigned to fields?

Validators are always assigned to fields via the “requires” attribute. For example, requires = IS_TIME(error_message='must be HH:MM:SS!') 

Name some Validator types in web2py.

Text Format Validator, Date and Time Validators, Range, set and equality Validators, Complexity and Security Validators, Special Type Validators, Database Validators, etc.

Why %b and %B symbols are used in date and time validators in web2py?

%b shows abbreviated month name. While %B shows the complete month name. 

Conclusion

In this article, we have discussed web2py and validators in brief. We have also discussed the date and time validators extensively.

After learning, What is the date and time validator in web2pyAre you curious to explore more articles on the web application framework? We have you covered. Check out Django | Learn & Practice from Coding Ninjas StudioBasics of Django Framework - Coding Ninjas Coding Ninjas Studio, and Coding Ninjas | Front-End Web Development with React.

Do upvote our blogs if you find them helpful and engaging!

Happy learning!

Live masterclass