本文整理汇总了Python中wtforms.validators.StopValidation方法的典型用法代码示例。如果您正苦于以下问题:Python validators.StopValidation方法的具体用法?Python validators.StopValidation怎么用?Python validators.StopValidation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类wtforms.validators
的用法示例。
在下文中一共展示了validators.StopValidation方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __call__
# 需要导入模块: from wtforms import validators [as 别名]
# 或者: from wtforms.validators import StopValidation [as 别名]
def __call__(self, form, field):
if not (isinstance(field.data, FileStorage) and field.data):
return
filename = field.data.filename.lower()
if isinstance(self.upload_set, Iterable):
if any(filename.endswith('.' + x) for x in self.upload_set):
return
raise StopValidation(self.message or field.gettext(
'File does not have an approved extension: {extensions}'
).format(extensions=', '.join(self.upload_set)))
if not self.upload_set.file_allowed(field.data, filename):
raise StopValidation(self.message or field.gettext(
'File does not have an approved extension.'
))
示例2: _run_validation_chain
# 需要导入模块: from wtforms import validators [as 别名]
# 或者: from wtforms.validators import StopValidation [as 别名]
def _run_validation_chain(self, form, validators):
"""
Run a validation chain, stopping if any validator raises StopValidation.
:param form: The Form instance this field beongs to.
:param validators: a sequence or iterable of validator callables.
:return: True if validation was stopped, False otherwise.
"""
for validator in validators:
try:
validator(form, self)
except StopValidation as e:
if e.args and e.args[0]:
self.errors.append(e.args[0])
return True
except ValueError as e:
self.errors.append(e.args[0])
return False
示例3: __call__
# 需要导入模块: from wtforms import validators [as 别名]
# 或者: from wtforms.validators import StopValidation [as 别名]
def __call__(self, form, field):
if not field.has_file():
return
filename = field.data.filename.lower()
if isinstance(self.upload_set, (tuple, list)):
ext = filename.rsplit('.', 1)[-1]
if ext in self.upload_set:
return
message = '{} is not in the allowed extentions: {}'.format(
ext, self.upload_set)
raise StopValidation(self.message or message)
if not self.upload_set.file_allowed(field.data, filename):
raise StopValidation(self.message or
'File does not have an approved extension')
示例4: __call__
# 需要导入模块: from wtforms import validators [as 别名]
# 或者: from wtforms.validators import StopValidation [as 别名]
def __call__(self, form, field):
"""
e.g.::
class SpecialRouteSearchForm(BaseForm):
special_route = StringField('可为空, 空格, 有值时去除空白', validators=[StripString(allow_none=True)])
class SpecialRouteForm(BaseForm):
special_route = StringField('必填, 去除空白赋值', validators=[StripString()])
"""
fdata = field.data
if isinstance(fdata, str):
if self.plain_text:
fdata = get_plain_text(fdata)
else:
fdata = fdata.strip()
if fdata == '' and not self.allow_none:
if self.message is None:
self.message = '{}不能为空{}'.format(field.label.text, '(纯文本)' if self.plain_text else '')
raise StopValidation(self.message)
getattr(form, field.name).data = fdata
示例5: validate
# 需要导入模块: from wtforms import validators [as 别名]
# 或者: from wtforms.validators import StopValidation [as 别名]
def validate(self, form, extra_validators=tuple()):
"""
Validates the field and returns True or False. `self.errors` will
contain any errors raised during validation. This is usually only
called by `Form.validate`.
Subfields shouldn't override this, but rather override either
`pre_validate`, `post_validate` or both, depending on needs.
:param form: The form the field belongs to.
:param extra_validators: A sequence of extra validators to run.
"""
self.errors = list(self.process_errors)
stop_validation = False
# Call pre_validate
try:
self.pre_validate(form)
except StopValidation as e:
if e.args and e.args[0]:
self.errors.append(e.args[0])
stop_validation = True
except ValueError as e:
self.errors.append(e.args[0])
# Run validators
if not stop_validation:
chain = itertools.chain(self.validators, extra_validators)
stop_validation = self._run_validation_chain(form, chain)
# Call post_validate
try:
self.post_validate(form, stop_validation)
except ValueError as e:
self.errors.append(e.args[0])
return len(self.errors) == 0
示例6: post_validate
# 需要导入模块: from wtforms import validators [as 别名]
# 或者: from wtforms.validators import StopValidation [as 别名]
def post_validate(self, form, validation_stopped):
"""
Override if you need to run any field-level validation tasks after
normal validation. This shouldn't be needed in most cases.
:param form: The form the field belongs to.
:param validation_stopped:
`True` if any validator raised StopValidation.
"""
pass
示例7: pre_validate
# 需要导入模块: from wtforms import validators [as 别名]
# 或者: from wtforms.validators import StopValidation [as 别名]
def pre_validate(self, form):
# If any error happen during process, we raise StopValidation here
# to prevent "DataRequired" validator from clearing errors
if self.errors:
raise validators.StopValidation()
super(ModelField, self).pre_validate(form)
示例8: __call__
# 需要导入模块: from wtforms import validators [as 别名]
# 或者: from wtforms.validators import StopValidation [as 别名]
def __call__(self, form: Dict[str, Any], field: Any) -> None:
if not form["start_dttm"].data and not form["end_dttm"].data:
raise StopValidation(_("annotation start time or end time is required."))
if (
form["end_dttm"].data
and form["start_dttm"].data
and form["end_dttm"].data < form["start_dttm"].data
):
raise StopValidation(
_("Annotation end time must be no earlier than start time.")
)