Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将错误链接到WTForms字段列表中的右侧字段_Python_Ajax_Wtforms_Fieldlist - Fatal编程技术网

Python 将错误链接到WTForms字段列表中的右侧字段

Python 将错误链接到WTForms字段列表中的右侧字段,python,ajax,wtforms,fieldlist,Python,Ajax,Wtforms,Fieldlist,我使用WTForm来验证直接从javascript模型提交的表单(通过敲除填充)。在我的表格中,我有一个可以动态添加/删除的银行账户列表。在python方面,我有如下内容: class Account(Form): acc_name = TextField('Account', [validators.Length(min=2, max=35)]) class InformationForm(Form): account_list = FieldList(

我使用WTForm来验证直接从javascript模型提交的表单(通过敲除填充)。在我的表格中,我有一个可以动态添加/删除的银行账户列表。在python方面,我有如下内容:

class Account(Form):
    acc_name        = TextField('Account', [validators.Length(min=2, max=35)])    

class InformationForm(Form):
    account_list = FieldList(FormField(Account))
接收用于验证的json数据如下所示:

'account_list': [{'acc_name': 'aaaaa'}, {'acc_name': 'b'}]}
问题是,当我验证时,我收到这样的消息,无法知道列表中的哪个帐户是错误源:

'account_list': [{'acc_name': [u'Field must be between 2 and 35 characters long.']}
如何将错误链接到正确的帐户


编辑:我最后的做法是在InformationForm类中添加了一个getErrors方法,该方法为每个帐户构建一个由唯一Id索引的词汇表,其中值是每个帐户错误。然后,我将其作为json返回到我的应用程序。如果有一个“自然”的解决方案,我会保留这个问题。

字段列表中的每个元素本身就是一个字段。如果FieldList包含一个TextField,则每个条目都是一个TextField。如果它包含一个FormField,那么它就是一个FormField(然后它包含一个具有自己字段的表单)。可以通过迭代FieldList或访问FieldList的
.entries
属性来访问FieldList条目

因此,不要查看
form.account\u list.errors
而是查看所附字段的错误

因此,对于您的使用,如下所示:

{% for subfield in form.account_list %}
    <!-- subfield in this case is an instance of FormField -->
    {{ subfield.form.acc_name() %}
    {% if subfield.errors %}
        {% for error in subfield.form.acc_name.errors %}
            <p class="error">{{ error }}</p>
        {% endfor %}
    {% endif %}
{% endfor %}
{%用于form.account_list%}
{{subfield.form.acc_name()%}
{%if subfield.errors%}
{%表示子字段中的错误。form.acc_name.errors%}

{{error}

{%endfor%} {%endif%} {%endfor%}

如果您需要经常一致地执行此操作,您可能不想对每个专业字段设置都执行此操作。

能否提供一个指向
getErrors
实现的链接?我认为在WTForms本身中还没有这样做的方法。我希望它能返回类似
[None]这样的错误列表[“第二个元素出现第一个错误”,“第二个元素出现第二个错误”],无]
。我为此打开了一个。