Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9252021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:50:36+00:00 2026-06-18T10:50:36+00:00

I am new to Web2py and am trying to use a custom validator. class

  • 0

I am new to Web2py and am trying to use a custom validator.

class IS_NOT_EMPTY_IF_OTHER(Validator):

    def __init__(self, other,
                 error_message='must be filled because other value '
                               'is present'):
        self.other = other
        self.error_message = error_message

    def __call__(self, value):
        if isinstance(self.other, (list, tuple)):
            others = self.other
        else:
            others = [self.other]

        has_other = False
        for other in others:
            other, empty = is_empty(other)
            if not empty:
                has_other = True
                break
        value, empty = is_empty(value)
        if empty and has_other:
            return (value, T(self.error_message))
        else:
            return (value, None)

I do not understand how to use it on my table:

db.define_table('numbers',

    Field('a', 'integer'),
    Field('b', 'boolean'),
    Field('c', 'integer')

I want to use this in a way that ‘c’ cannot be left black when ‘b’ is ticked.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:50:37+00:00Added an answer on June 18, 2026 at 10:50 am

    save the code on /modules/customvalidators.py

    from gluon.validators import is_empty
    from gluon.validators import Validator
    
    
    class IS_NOT_EMPTY_IF_OTHER(Validator):
    
        def __init__(self, other,
                     error_message='must be filled because other value '
                                   'is present'):
            self.other = other
            self.error_message = error_message
    
        def __call__(self, value):
            if isinstance(self.other, (list, tuple)):
                others = self.other
            else:
                others = [self.other]
    
            has_other = False
            for other in others:
                other, empty = is_empty(other)
                if not empty:
                    has_other = True
                    break
            value, empty = is_empty(value)
            if empty and has_other:
                return (value, T(self.error_message))
            else:
                return (value, None)
    

    then in models/db.py

    from customvalidator import IS_NOT_EMPTY_IF_OTHER
    
    db.define_table("foo",
        Field('a', 'integer'),
        Field('b', 'boolean'),
        Field('c', 'integer')
    )
    
    # apply the validator
    db.foo.c.requires = IS_NOT_EMPTY_IF_OTHER(request.vars.b)
    

    Also, note that it can be done easily without the above validator.
    Forget all the code above and try this simplified way

    Version 2:

    controllers/default.py
    
    def check(form):
        if form.vars.b and not form.vars.c:
            form.errors.c = "If the b is checked, c must be filled"
    
    def action():
        form = SQLFORM(db.foo)
        if form.process(onvalidation=check).accepted:
            response.flash = "success"
        return dict(form=form)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Python and I'm trying to install web2py in a virtualenv. I'm
New to Web2py, so my question might not be too clear. I'm trying to
I am new at web2py and i am struggling with it I am trying
New to Ruby, and I'm trying to figure out what idiom to use to
I started playing around with web2py the other day for a new project. I
While reading the web2py manual, I came across this following: 'Once a new user
new to c#. I'm trying to make a simple system where I can search
New to Node.js and Express, I am trying to understand the two seems overlapping
New programmer here, I am trying to understand and break down this code below
New developer here,Im using the Custom Image Picker by ray wenderlich. But what I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.