Rewrote test_forms_fields_set.py to work on both Pydantic versions:
- Use PYDANTIC_V2 flag from fastapi._compat
- Use model_fields_set (v2) or __fields_set__ (v1) appropriately
- Removed field_validator which is v2-only
- All tests now run on both versions for full coverage
When using Form() with Pydantic models, FastAPI was preloading default
values and passing them to Pydantic, causing all fields to appear in
model_fields_set even when not provided. This also caused validation
to be enforced on unprovided defaults.
Changes:
- Modified _get_multidict_value() to check if values is FormData
- For FormData, return None for unprovided fields instead of defaults
- This lets Pydantic handle defaults properly and preserve fields_set
- Updated test expectation in test_forms_single_model.py
- Added comprehensive test suite in test_forms_fields_set.py
The fix ensures Form models behave consistently with JSON body models
regarding field tracking and validation.
Closes#13399