pallets-eco / pallets-eco/wtforms

Visibility issues with validating FormFields' fields

Open
#633 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

breaking change
Dominant language
Python
Stars
1.6k
Forks
409
PR merge metrics
No merged PRs in 30d

Description

Quick Description

field.pre_validate() runs for normal fields as well for fields inside a FormField, displaying all errors as expected.
However, field.process_formdata() does not seem to run for fields inside a FormField, or the errors from it are not collected.
There also seems to be some issues with setting data to None outside the FormField.
This leads to inconsistent behaviour - we only see some errors, and some of the problematic data has been set to None.

Setup
>>> from flask import Flask
... from flask_wtf import FlaskForm
... from werkzeug.datastructures import ImmutableMultiDict
... from wtforms import fields
... 
... app = Flask(__name__)
... app.config["WTF_CSRF_ENABLED"] = False
... 
... class FF(FlaskForm):
...     choices_inner = fields.SelectField(label="Test Select", choices=(("1",11), ("2",22)))
...     date_inner = fields.DateField()
... 
... class TestForm(FlaskForm):
...     choices = fields.SelectField(label="Test Select", choices=(("1",11), ("2",22)))
...     date = fields.DateField()
...     form = fields.FormField(FF)
...         
... with app.app_context():
...     test = TestForm(formdata=ImmutableMultiDict({"choices": 3, "date": "20000", "form": {"choices_inner": "3", "date_inner": "20000"}}))
Actual Behavior
>>> test.validate()
False
>>> test.errors
{'choices': ['Not a valid choice'], 'date': ['Not a valid date value'], 'form': {'choices_inner': ['Not a valid choice']}}
>>> test.data
{'choices': '3', 'date': None, 'form': {'choices_inner': None, 'date_inner': None}}
Expected Behavior
>>> test.validate()
False
>>> test.errors
{'choices': ['Not a valid choice'], 'date': ['Not a valid date value'], 'form': {'choices_inner': ['Not a valid choice'], 'date_inner': ['Not a valid date value']}}
>>> test.data
{'choices': None, 'date': None, 'form': {'choices_inner': None, 'date_inner': None}}
Environment
  • Python version: 3.7.3
  • wtforms version: 2.3.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided Flask/WTForms reproducer and trace FormField validation, especially pre_validate() and process_formdata(). Confirm the fix by checking that nested date errors are collected and that invalid field data matches the expected None values in test.errors and test.data.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, python
Domain
backend, testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.