Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move validation from widgets to the form fields #603

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Commits on Mar 15, 2024

  1. Move validation from widgets to the form fields

    Previously, the widgets handled part of the validation. That behavior
    prevents overriding validation in form fields, as widgets were casting
    the value into a `PhoneNumber` object, validating it in the process.
    
    Following the `MultiValueField` implementation from Django (and
    `MultiWidget`), the widget now handles the presentation logic, but makes
    no attempt at validation.
    
    The new `SplitPhoneNumberField` handles the logic of validating the
    region choice and the number, and the `PhoneNumberPrefixWidget` simply
    dispatches the region and number data to the appropriate widget.
    
    In order to retain backward compatibility, now that the
    `validate_international_phonenumber` actually comes into play, it’s
    error code has been changed to `invalid`, so that the custom error
    message for invalid shows.
    
    Migration guide
    ===============
    
    `validate_international_phonenumber`
    ------------------------------------
    
    Review uses of the `invalid_phone_number` error code. Given that the
    validator usually did not come into play, you shouldn’t find many uses.
    
    `PhoneNumberField` with `RegionalPhoneNumberWidget`
    ---------------------------------------------------
    
    Make sure custom validation occurs in the Django `Form` `clean_FIELD()`
    (or `clean()`), and no changes should be noticeable.
    
    `PhoneNumberField` with `PhoneNumberPrefixWidget`
    -------------------------------------------------
    
    Use the `SplitPhoneNumberField` instead. Error messages will change
    slightly and should be more precise (whether the region is not part of
    the choices, or the number cannot be interpreted in the selected
    region).
    
    For more examples, take a look at
    `tests.test_formfields.SplitPhoneNumberFieldTest`. In particular
    `test_custom_attrs` and `test_custom_choices`, to see how they were
    migrated to the new field.
    francoisfreitag committed Mar 15, 2024
    Configuration menu
    Copy the full SHA
    9d729ea View commit details
    Browse the repository at this point in the history
  2. Rename test_value_from_datadict to test_example_numbers

    Better conveys what it test. Was not changed in previous commit to limit
    help tracking moved tests.
    francoisfreitag committed Mar 15, 2024
    Configuration menu
    Copy the full SHA
    56d52c4 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    237de8a View commit details
    Browse the repository at this point in the history