Linking errors to the right field in WTForms FieldLists

I am using WTForm to validate a form that I submit directly from a javascript model (filled through knockout). In my form I have a list of bank accounts which can be added/removed dynamically. On the python side I have something like this :

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

class InformationForm(Form):
    account_list = FieldList(FormField(Account))

The json data that is received for validation is the following :

'account_list': [{'acc_name': 'aaaaa'}, {'acc_name': 'b'}]}

The problem is then when I validate I receive something like this where there is no way to know which account in the list is the source of the error:

'account_list': [{'acc_name': [u'Field must be between 2 and 35 characters long.']}

How can I do to link the error to the right account ?

EDIT : the way I did it in the end is that I added a getErrors method to my InformationForm class, which builds a dictionnary indexed by a unique Id for each Account where the values are each Account error. I then return that as json to my application. I keep the question open in case there is a "natural" solution...


Each individual element in a FieldList is a field in and of itself. If the FieldList contains a TextField, then each entry is a TextField. If it contains a FormField then it's a FormField (which then contains a form with its own fields) The FieldList entries can be accessed either by iterating a FieldList or by accessing the .entries attribute of the FieldList.

So instead of looking at form.account_list.errors look at the enclosed field's errors.

So for your use, something like this:

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

You probably want to abstract this out to a macro instead of doing this for every specialty field setup, if this is something you need to do consistently and often.

Need Your Help

How to call write() function of ConnectedThread class of android bluetooth from main activity

java android android-bluetooth

i just want to know how to call the write function of the ConnectedThread class from main activity java file

Ansible Handler notify vs register

ansible ansible-playbook

So after reading Ansible docs, I found out that Handlers are only fired when tasks report changes, so for example: