WTForms用于动态选项?

时间:2015-05-13 05:14:09

标签: javascript google-app-engine html-select wtforms

我在引擎中使用带有Jinja2的WTForms,并且我有一个动态选项字段,其中城市的可用选项取决于区域的选择。现在我想为区域和城市启用验证(主要是如果用户没有选择区域/城市),但我不知道如何做。你能告诉我如何修改我的表单类以启用动态选项的验证吗?

我的表单类是

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>
       ...

1 个答案:

答案 0 :(得分:1)

我做了类似的事情,我对每种可能性都有不同的WTForm - 在你的情况下,我想那将是每个地区。

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

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

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

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

在客户端,我会使用jQuery / Javascript来更新用户选择区域后呈现给用户的城市表单字段。