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

  • SEARCH
  • Home
  • 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 8248215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:07:06+00:00 2026-06-07T23:07:06+00:00

I have a SelectField that I want to add validation to with WTForms. The

  • 0

I have a SelectField that I want to add validation to with WTForms. The fields gets its values from a dynamic dropdown since it is the city field of a pair of region/city choice where the user first select region and then the city options switches to display the cities of the selected region:

enter image description here

If I set it to the same name as in the form class then I still can perform validation on the input:

<div id="cities">
                {{form.area}}
</div>{% if form.area.errors %} <div class="maintext">
        <ul class="errors">{% for error in form.area.errors %}<li>{{ error }}</li>{% endfor %}</ul></div>
    {% endif %}

I want a red frame around the fields which do not validate and this works for other fields than the SelectField (see attached image).
I wonder how to enable validation for my SelectField? It works for all other fields. To enable the red frame around the field I tried to subclass the Select class and add this as a widget since this works for other fields but not for the SelectField:

from wtforms.widgets import Select
class SelectWithRedFrame(Select):

    def __init__(self, error_class=u'has_errors'):
        super(SelectWithRedFrame, self).__init__()
        self.error_class = error_class

    def __call__(self, field, **kwargs):
        if field.errors:
            c = kwargs.pop('class', '') or kwargs.pop('class_', '')
            kwargs['class'] = u'%s %s' % (self.error_class, c)
        return super(SelectWithRedFrame, self).__call__(field, **kwargs)

class AdForm(Form):

    my_choices = [
        ('1', _('All categories')),
        ('disabled', _('VEHICLES')),
        ('2010', _('Cars')),
        ('3', _('Motorcycles')),
        ('4', _('Accessories & Parts')),
        ('disabled', _('PROPERTIES')),
        ('7', _('Apartments')),
        ('8', _('Houses')),
        ('9', _('Commercial properties')),
        ('10', _('Land')),
        ('disabled', _('ELECTRONICS')),
        ('12', _('Mobile phones & Gadgets')),
        ('13', _('TV/Audio/Video/Cameras')),
        ('14', _('Computers')),
        ('disabled', _('HOME & PERSONAL ITEMS')),
        ('16', _('Home & Garden')),
        ('17', _('Clothes/Watches/Accessories')),
        ('18', _('For Children')),
        ('disabled', _('LEISURE/SPORTS/HOBBIES')),
        ('20', _('Sports & Outdoors')),
        ('21', _('Hobby & Collectables')),
        ('22', _('Music/Movies/Books')),
        ('23', _('Pets')),
        ('20', _('BUSINESS TO BUSINESS')),
        ('24', _('Hobby & Collectables')),
        ('25', _('Professional/Office equipment')),
        ('26', _('Business for sale')),
        ('disabled', _('JOBS & SERVICES')),
        ('28', _('Jobs')),
        ('29', _('Services')),
        ('30', _('Events & Catering')),
        ('31', _('Others')),
        ]
    regions = [
        ('', _('Choose')),
        ('3', _('Delhi')),
        ('4', _('Maharasta')),
        ('7', _('Gujarat')),      
        ]
    cities = [
        ('', _('Choose')),
         ('3', _('Mumbai')),
        ('4', _('Delhi')),     
        ]
    nouser = HiddenField(_('No user'))  # dummy variable to know whether user is logged in
    name = TextField(_('Name'),
                     [validators.Required(message=_('Name is required'
                     ))], widget=MyTextInput())
    title = TextField(_('Subject'),
                      [validators.Required(message=_('Subject is required'
                      ))], widget=MyTextInput())
    text = TextAreaField(_('Ad text'),
                         [validators.Required(message=_('Text is required'
                         ))], widget=MyTextArea())
    phonenumber = TextField(_('Phone'), [validators.Optional()])
    phoneview = BooleanField(_('Display phone number on site'))
    price = TextField(_('Price'), [validators.Regexp('^[0-9]+$',
                      message=_('This is not an integer number, please see the example and try again'
                      )), validators.Optional()], widget=MyTextInput())
    email = TextField(_('Email'),
                      [validators.Required(message=_('Email is required'
                      )),
                      validators.Email(message=_('Your email is invalid'
                      ))], widget=MyTextInput())

    region = SelectField(_('Region'),choices=regions,validators=[validators.Required(message=_('Region is required'))],option_widget=SelectWithRedFrame())
    area = SelectField(_('City'),choices=cities,validators=[validators.Required(message=_('City is required'
                      ))],option_widget=SelectWithRedFrame())

    def validate_name(form, field):
        if len(field.data) > 50:
            raise ValidationError(_('Name must be less than 50 characters'
                                  ))

    def validate_email(form, field):
        if len(field.data) > 60:
            raise ValidationError(_('Email must be less than 60 characters'
                                  ))

    def validate_price(form, field):
        if len(field.data) > 8:
            raise ValidationError(_('Price must be less than 9 integers'
                                  ))

Can you tell me what I’m doing wrong and how I can enable my SelectField for validation?

Thanks

  • 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-07T23:07:07+00:00Added an answer on June 7, 2026 at 11:07 pm

    Have you tried the validator AnyOf yet?
    See http://wtforms.simplecodes.com/docs/0.6/validators.html

    class wtforms.validators.AnyOf(values, message=u’Invalid value, must be one of: %(values)s’, values_formatter=None)
    Compares the incoming data to a sequence of valid inputs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three models that I want to interact with each other. Kase, Person
i have a script in jquery (that grabs a value from a select field
I have a bit of data that I want to use to build a
I want to re-use a template I have with my WTForms form: <th>${form.name.label}</th> <td>${form.name()}</td>
I have a form with a single select_field that gets submitted via jquery whenever
I have a field that contains strings such as 'Blah-OVER', 'Blah-OveR', etc. and want
I have a form with two selectfields. I would like that when something is
I have a AS400 datasource. I want to union a select field. This union
Having problems with IE8... I have a button that onclick fires the showImageBrowser() function.
I have a table that contains multiple rows. Each row contains 5 columns (or

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.