Reputation: 26644
I use WTForms with Jinja2 in appengine and I have a dynamic option field where the available choices for a city depends on the choice of a region. Now I want to enable validation also for regions and cities (mainly if a user didn't select a region/city) but I don't know how to do it. Can you tell me how I should modify my form class to enable validation for dynamic options?
My form class is
class InsertForm(Form):
categories = [
('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')),
('1000', _('Sports & Outdoors')),
('1010', _('Hobby & Collectables')),
('1020', _('Hobby & Collectables')),
('1030', _('Music/Movies/Books')),
('1050', _('Pets')),
('1080', _('BUSINESS TO BUSINESS')),
('1100', _('Hobby & Collectables')),
('1090', _('Professional/Office equipment')),
('2010', _('Business for sale')),
('2030', _('Sports & Outdoors')),
('2040', _('Hobby & Collectables')),
('2080', _('Music/Movies/Books')),
('2070', _('Pets')),
('3000', _('BUSINESS TO BUSINESS')),
('3040', _('Hobby & Collectables')),
('3050', _('Professional/Office equipment')),
('3060', _('Business for sale')),
('4000', _('Sports & Outdoors')),
('4010', _('Hobby & Collectables')),
('4020', _('Music/Movies/Books')),
('4040', _('Pets')),
('4030', _('BUSINESS TO BUSINESS')),
('4090', _('Hobby & Collectables')),
('4060', _('Professional/Office equipment')),
('4070', _('Business for sale')),
('5030', _('Music/Movies/Books')),
('5020', _('Pets')),
('5010', _('BUSINESS TO BUSINESS')),
('5040', _('Hobby & Collectables')),
('6010', _('Professional/Office equipment')),
('6020', _('Business for sale')),
('6030', _('Music/Movies/Books')),
('6040', _('Pets')),
('7010', _('BUSINESS TO BUSINESS')),
('Other', _('Hobby & Collectables')),
]
regions = [('', _('Choose')), ('3', _('Delhi')), ('4', _('Maharasta'
)), ('7', _('Gujarat'))]
cities = [('', _('«Choose city»')), ('3', _('Mumbai')), ('4',
_('Delhi'))]
nouser = HiddenField(_('No user')) # dummy variable to know whether user is logged in
name = StringField(_('Name'),
[validators.Required(message=_('Name is required'
))], widget=MontaoTextInput())
title = StringField(_('Subject'),
[validators.Required(message=_('Subject is required'
))], widget=MontaoTextInput())
text = TextAreaField(_('Ad text'),
[validators.Required(message=_('Text is required'
))], widget=MontaoTextArea())
phonenumber = TextField(_('Phone'), [validators.Optional()])
type = StringField(_('Type'),
[validators.Required(message=_('Type is required'
))])
phoneview = BooleanField(_('Display phone number on site'))
price = StringField(_('Price'), [validators.Regexp('^[0-9]+$',
message=_(
'This is not an integer number, please see the example and try again'
)), validators.Optional()], widget=MontaoTextInput())
email = StringField(_('Email'),
[validators.Required(message=_('Email is required'
)),
validators.Email(message=_('Your email is invalid'
))], widget=MontaoTextInput())
#area = SelectField(_('City'), choices=cities,
# validators=[validators.Optional()])
category_group = SelectField(_('Category'), choices=categories,
validators=[validators.Required(message=_('Category is required'
))])
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'
))
def validate_area(form, field):
if len(field.data) > 888:
raise ValidationError(_('Dummy validator'
))
In my HTML I have a script that enables the cities for a region.
<select onchange="cities(this);document.getElementById('area').display='';" name="region" id="region">
<option value="">«{% trans %}Choose region{% endtrans %}»</option>
...
Upvotes: 1
Views: 190
Reputation: 16563
I do something similar and I have a different WTForm for each possibility -- in your case, I guess that would be for each region.
On the server, when you receive the form data, you first use the region in the form data to select the form that you need to process all of the form data.
In your case, you could have a base form that has all info except for cities. This form would never be used, but you could then subclass that form for each region where you specify the appropriate cities for the region. Something like this:
class DelhiInsertForm(InsertForm):
cities = SelectField(...)
class MaharastaInsertForm(InsertForm):
cities = SelectField(...)
On the client side, I would use jQuery/Javascript to update the cities form field that is presented to the user after the user selects a region.
Upvotes: 1