gpt4 book ai didi

python - 为我的 SelectField 启用验证

转载 作者:行者123 更新时间:2023-12-01 06:00:57 29 4
gpt4 key购买 nike

我有一个 SelectField,我想使用 WTForms 添加验证。这些字段从动态下拉列表中获取其值,因为它是一对区域/城市选择的城市字段,用户首先选择区域,然后城市选项切换以显示所选区域的城市:

enter image description here

如果我将其设置为与表单类中相同的名称,那么我仍然可以对输入执行验证:

<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 %}

我想要在不验证的字段周围有一个红框,这适用于 SelectField 之外的其他字段(参见附图)。我想知道如何为我的 SelectField 启用验证?它适用于所有其他领域。为了启用字段周围的红框,我尝试对 Select 类进行子类化并将其添加为小部件,因为这适用于其他字段,但不适用于 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'
))

你能告诉我我做错了什么以及如何启用我的 SelectField 进行验证吗?

谢谢

最佳答案

您尝试过验证器 AnyOf 了吗?请参阅http://wtforms.simplecodes.com/docs/0.6/validators.html

class wtforms.validators.AnyOf(values, message=u'无效值,必须是以下之一:%(values)s',values_formatter=None)将传入数据与一系列有效输入进行比较。

关于python - 为我的 SelectField 启用验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10501114/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com