Javascript WTForms用于动态选项?

Javascript WTForms用于动态选项?,javascript,google-app-engine,html-select,wtforms,Javascript,Google App Engine,Html Select,Wtforms,我在appengine中将WTForms与Jinja2一起使用,我有一个动态选项字段,其中城市的可用选项取决于区域的选择。现在我还想为地区和城市启用验证(主要是在用户没有选择地区/城市的情况下),但我不知道如何进行验证。您能告诉我应该如何修改表单类以启用动态选项的验证吗 我的班次是 class InsertForm(Form): categories = [ ('1', _('All categories')), ('disabled', _('VEHIC

我在appengine中将WTForms与Jinja2一起使用,我有一个动态选项字段,其中城市的可用选项取决于区域的选择。现在我还想为地区和城市启用验证(主要是在用户没有选择地区/城市的情况下),但我不知道如何进行验证。您能告诉我应该如何修改表单类以启用动态选项的验证吗

我的班次是

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'
            ))
在我的HTML中,我有一个脚本,可以为一个地区启用城市

 <select onchange="cities(this);document.getElementById('area').display='';" name="region" id="region">
            <option value="">«{% trans %}Choose region{% endtrans %}»</option>
       ...

«{%trans%}选择区域{%endtrans%}»
...

我做了一些类似的事情,我对每种可能性都有不同的WTForm——在您的情况下,我想这将适用于每个地区

在服务器上,当您收到表单数据时,首先使用表单数据中的区域来选择需要处理所有表单数据的表单

在您的情况下,您可以有一个基本表单,其中包含除城市以外的所有信息。此表单永远不会被使用,但是您可以为每个区域指定相应的城市,然后将该表单子类化。大概是这样的:

class DelhiInsertForm(InsertForm):
    cities = SelectField(...)

class MaharastaInsertForm(InsertForm):
    cities = SelectField(...)
在客户端,我将使用jQuery/Javascript更新用户选择区域后显示给用户的cities表单字段