Table of contents
1.
Introduction
2.
Range, set and equality validators in web2py
2.1.
IS_EQUAL_TO
2.2.
IS_NOT_EMPTY
2.3.
IS_NULL_OR
2.4.
IS_EMPTY_OR
2.5.
IS_EXPR
2.6.
IS_DECIMAL_IN_RANGE
2.7.
IS_FLOAT_IN_RANGE
2.8.
IS_INT_IN_RANGE
2.9.
IS_IN_SET
3.
Frequently Asked Questions
3.1.
Is Web2py an MVC framework?
3.2.
Can web2py be used with Python 3?
3.3.
Is Web2py an open source?
3.4.
What is Web2py’s default port?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Range, Set and Equality Validators in web2py

Author Urwashi Priya
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Web2py is a full-stack web framework based on Python. It is an easy-to-learn, fast, secure and reliable framework. Now, in each framework, we have some validators which check the correctness of input fields that is taken by forms. Validators are mainly used to validate the field created by forms so that it becomes easy for users to keep a check on the application. We can add a single validator or multiple validators, depending on our needs. This always comes with the “require” attribute in web2py.

require attribute must be present.

In this blog, we will learn about range, set and equality validators.

Range, set and equality validators in web2py

Let’s start discussing variants of range, set and equality validators one by one.

start image

IS_EQUAL_TO

This validator is used to check whether the field input value equals the given desired value.

requires = IS_EQUAL_TO(request.vars.password, error_message='wrong password! Check again')
You can also try this code with Online Python Compiler
Run Code
checks for equality

IS_NOT_EMPTY

This validator is used to check that none of the fields remains empty.

requires = IS_NOT_EMPTY(error_message='Must have some value!')
You can also try this code with Online Python Compiler
Run Code

IS_NULL_OR

This validator is used to allow an input field to have NULL as a value given. Say, for example, middle name.

IS_EMPTY_OR

This validator is used to allow an input field to remain empty. Say, for example, society name in address or date.

requires = IS_EMPTY_OR(IS_DATE())
You can also try this code with Online Python Compiler
Run Code

IS_EXPR

This validator is used to check for the correctness of a given condition.

requires = IS_EXPR(lambda v: T('not a prime number') if int(v) a prime number else None)
You can also try this code with Online Python Compiler
Run Code

IS_DECIMAL_IN_RANGE

This validator is used to convert the inputted value to a decimal of a given range. If it can’t be converted, then it throws an error.

INPUT(_type='text', _name='name', requires=IS_DECIMAL_IN_RANGE(0, 100))
You can also try this code with Online Python Compiler
Run Code

IS_FLOAT_IN_RANGE

This validator is used to check for the floating point number in a given range. Minimum and maximum values of the range are decided by the user.

IS_INT_IN_RANGE

This validator is used to check for the integer value in a given range. Minimum ana maximum values of the range are decided by the user.

requires = IS_INT_IN_RANGE(0, 100, error_message='not in a desired range!')
You can also try this code with Online Python Compiler
Run Code

IS_IN_SET

This validator is used to create a drop-down menu.

requires = IS_IN_SET(['pizza', 'burger', 'pasta'], zero=T('choose which one you would like to have’),error_message='choose only one')
You can also try this code with Online Python Compiler
Run Code
drop down menu

Frequently Asked Questions

Is Web2py an MVC framework?

Web2py is a stable python framework following well known MVC framework. Web applications built on this framework are very secure.

Can web2py be used with Python 3?

Web2py can easily function on Python 2.7 and Python 3, along with CPython (the C implementation) and PyPy (Python written in Python).

Is Web2py an open source?

Yes, Web2py is an open source, allowing developers to do dynamic programming. This framework is easier to learn, more compact, and does not have any complex configuration files.

What is Web2py’s default port?

Port 8000 is the default port of Web2py.

Conclusion

In this article, we have clearly discussed about range, set and equality validators in web2py and how their syntax.  Along with a clear understanding of these validators, we also covered some frequently asked questions. 

After reading about Range, set and equality validators in web2py, are you not feeling excited to read/explore more articles on other topics? Don't worry; Coding Ninjas has you covered. See Coding Ninjas StudioDBMS guided pathoperating system to learn.

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

Happy Learning!

Thank you

Live masterclass