- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
当我将 wtforms.IntegerField
与 required
验证器一起使用时,我只会收到消息“此字段是必需的”。
另一方面,似乎只能要求 IntegerField
而我不应该使用要求的验证器。
这里的逻辑是什么?这个字段只是添加了一个 is integer 验证器,它应该仍然可以用作必填字段(但必须是 int)或不是必填字段,这没有意义吗?
只是想在这里发现一些模式。
__version__ = '1.0.5'
__version__ = '0.9.3'
class OrderForm(Form):
order_number = wtforms.IntegerField('Order Number', validators=[validators.Required()])
def render_response(self):
ctx = {}
order_form = OrderForm()
ctx['order_form'] = order_form
if order_form.validate_on_submit(): pass
# stuff.
return render_template('support/warranty_form.html', **ctx)
{% macro render_field(field) %}
<div class="form-field-wrapper">
<div class="form-field">
{{ field(**kwargs) }}
</div>
{{ render_field_errors(field) }}
</div>
{% endmacro %}
{{ render_field(order_form.order_number, placeholder="ORDER NUMBER") }}
如果我删除了必需的验证器,我的表单仍然需要该值。
我刚刚在文本字段上使用了自定义验证器。
最佳答案
wtforms Issue #14你可能会感兴趣。用户希望尝试创建一个“NullableIntegerField”。一个想法是使用 optional validator .您可以在下面的脚本中看到它是如何工作的:
from werkzeug import MultiDict
from wtforms import Form, IntegerField, validators
class MyDefaultForm(Form):
my_field = IntegerField('My Field')
class MyRequiredForm(Form):
my_field = IntegerField('My Field', [validators.required()])
class MyOptionalForm(Form):
my_field = IntegerField('My Field', [validators.optional()])
input_data = MultiDict({
'my_field' : ''
})
default_form = MyDefaultForm(input_data)
required_form = MyRequiredForm(input_data)
optional_form = MyOptionalForm(input_data)
print 'Default Form Validation: {0}'.format(default_form.validate())
print 'Default Form Errors: {0}'.format(default_form.errors)
print 'Default Data: {0}'.format(default_form.data)
print
print 'Required Form Validation: {0}'.format(required_form.validate())
print 'Required Form Errors: {0}'.format(required_form.errors)
print 'Required Data: {0}'.format(required_form.data)
print
print 'Optional Form Validation: {0}'.format(optional_form.validate())
print 'Optional Form Errors: {0}'.format(optional_form.errors)
print 'Optional Data: {0}'.format(optional_form.data)
print
结果:
Default Form Validation: False
Default Form Errors: {'my_field': [u'Not a valid integer value']}
Default Data: {'my_field': None}
Required Form Validation: False
Required Form Errors: {'my_field': [u'This field is required.']}
Required Data: {'my_field': None}
Optional Form Validation: True
Optional Form Errors: {}
Optional Data: {'my_field': None}
我创建了三个带有整数字段的表单。一种没有验证器,一种具有必需的验证器,一种具有可选的验证器。您可能希望使用可选的验证器以使结果为 None。
关于python - IntegerField 和必需的验证器不直观?总是需要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21451058/
内部 TransactionScope 将哪个事务作为环境事务? using ( var t1 = new TransactionScope( TransactionScopeOption.Requi
我需要匹配以下模式:N.N.N 324324.234324.234324 匹配,以及 1.1.1 I have the following pattern: (\d*\.\d*\.\d*) 问题是,它
这个问题在这里已经有了答案: HTML5 required attribute not working (2 个答案) 关闭上个月。 我最近为我创建了一个网页来学习更多关于 HTML 的知识,我发现
我在 NgForm 中有以下字段: 0'" /> Dependency is required 问题是这个字段总是需要的,不管 [required]相关条件depSelected>0 . d
我有一个模型类,其中包含几个必填字段: public class UserMetadata { [Required(ErrorMessage = "Please enter a name.")]
我有一张预订表格,需要验证。某些字段的启用/禁用取决于之前选择的选项。我的问题是,我无法提交禁用字段的表单,因为它等待来自空字段的“有效”输入数据。 是否有一种方法可以仅在启用字段集时启用/禁用这些字
如何为textarea设置两个属性(默认文本,点击后隐藏,必填)? Default text HTML“认为”默认文本是我的输入。我该如何修复它?谢谢大家。 最佳答案 如果您不介意兼容性,简单
我是编码新手,所以这可能是一个非常简单的问题。 编译时出现错误: GradeAnalyzer.java:49: error: method getAverage in class GradeAnaly
我有一个带有 2 个输入字段的 from:手机和电话。我希望至少需要其中一个字段,以便可以提交表单。要么您输入手机号码,不再需要电话输入,要么反之亦然。 我发现 Jquery 验证器有条件语句,
看看下面的代码,我正在使用 required(System.ComponentModel.DataAnnotations) 命名空间,但它一直向我显示红色波浪线,并显示无法找到“type or nam
所以我有一个 knockout 原型(prototype),您可以在其中动态添加输入,然后设置每个设置。将其视为表单生成器就是这样。然而,我注意到禁用和必需的效果不太好。它将值设置为禁用或必需,但是当
我正在使用 Entity Framework 的表拆分功能来拆分我的实体数据模型,如下所示: +--------+ +--------------+ | News | | NewsI
我有 3 个关于 TransactionScopeOption 的问题。Required 把我逼疯了,我无法在网上找到他们的答案。 A. 我很难思考什么时候我必须在现实中编写这段代码?为什么我不应该将
public static int biggestArrayGap(int []a, int n) { int biggestGap = Math.abs(a[1]-a[0]); for (i
我真的发现 django 表单集令人困惑。 我尤其对以下我不太了解的概念有疑问: The formset is smart enough to ignore extra forms that were
在我的 Angular v6 应用程序中,我尝试显示一个下拉列表,并根据 bool 值将其设置为必需,该 bool 值设置在复选框的值上。以下是我的模板中的代码片段(includeModelVersi
我正在使用jquery validation插入。当我使用 required( dependency-expression ) 时,我注意到 required( dependency-expressi
代码: ArrayList marks = new ArrayList(); String output = "Class average:" + calculateAverage() + "\
static void sort (Textbook [ ] info, int numEntries) { long tempIsbn = 0; String tempTitle =
在我的 Angular v6 应用程序中,我尝试显示一个下拉列表,并根据 bool 值将其设置为必需,该 bool 值设置在复选框的值上。以下是我的模板中的代码片段(includeModelVersi
我是一名优秀的程序员,十分优秀!