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
Use Django's "normal" validation, not hardcoded in __init__() #597
Comments
Hi, thanks for reporting the issue. There’s indeed an unexpected check for A first step seems is to remove it, which should be fine, as the However, handling short codes phone numbers is a bit trickier: the wrapper of I believe the suggestion is correct, that check should be removed. But we’ll need to adapt the model fields and Edit: the |
@nerdoc, please feel free to try out the PR, see if it works for your use case? |
I'm afraid I don't find time ATM - if it works for you, just merge it, I will test it in a few weeks when it is easier, and reopen if still a problem, ok? |
I would rather wait for input before merging (and releasing). I’ll wait another month or two, unless I get feedback otherwise. |
So, I tested the new behaviour a bit. The field now seems to validate the phone number correctly: it is done in the I don't know exactly how to solve this. So there are few options.
But at least, the behaviour techically is correct now, AFAICT. So my title "allow cleaning of field before validation" is incorrect, as Django runs validation before cleaning. Which is a bit of nonsense IMHO. But that's not your problem. So I think you can close the issue, unless you have a better idea. |
The only thing I can think of is: Be less strict. If you have a username check, the validator just checks if the username generally is correct. and in my form , I can add a I maybe would suggest to follow this road in phonnumbers too. What you really want, is:
So, I would
The |
Thanks for the feedback! First thing, now that I know more about your use case, have you seen the I believe the default of validating international phone number makes sense, developers usually store phone numbers in order to be able to reach people, so they’ll want valid phone numbers, and people may very well come from abroad so developers shouldn’t really limit to national phone numbers. One thing you can easily do for your app is to change the default validator (by subclassing the field class and setting
One difference is that phone numbers have known rules they follow, they aren’t free-form values.
I disagree.
The form is usually the best place to be strict, since developers can surface issues to users. A value that passes form validation is expected to pass model validation and save to the database without issues. |
That's what I mean. When I need people to fill in numbers in various formats, what I need is a default format that is interpreted. So whatever the user fills in, if it's international, it's ok, of it's not international, let it be of that region.
Sure, but there are two steps. One is the form, here should be the validator "can be a phone number". Noone should be allowed to enter "lkjdsjfh" as Phone number, not even "89273465f3", nor "++4366412345678" THEN, dev interaction should be possible, like cleaning. You could provide a default clean method helper, like using the default regional format as matrix to interpret the filled in number. THIRD the model validator is called anyway if it is a ModelForm. And THERE, it should be checked (as a model validator) that ALL phone numbers are SAVED in international format. It also should be an error when a developer saves a number as non-international, if the field should be international.
No, I didn't mean you should override And there's my only problem: If not using a ModelForm,
Think about an input field that is validate using AJAX at each keyup. In good UIs, you can enter a phone number and don't get at each keypress the red flag that it is wrong. It should be tolerant until it is clearly wrong (you enter a "F" or "ß"), but as long as the number could be correct (if entered further this way) there shouldn't be an error as response. So IMHO Form validators should be "more generic" letting clean methods fix common used errors (add international notation, remove whitespace, etc), and Model validators should be very strict. |
This is a very cool project - but I need to "validate" my prhone numbers and clean them before the PhoneNumerField gets hand on it. It may have to do something with #441 and could be solved together with that issue too IMHO.
Use case: I have an input form that asks for a phone number, and it is likely one of two countries (DE,AT). People tend to fill in , numbers in a local format, and I want to call
clean_phone_number()
before the PhoneNumberField calls it's validators, and try to convert it into an international format.ATM this is not possible, as the validation is done more or less hard coded. I can't override the form's
clean_phone_number()
orclean()
methods, as PhoneNumberField does it's own validation stuff already before, and throws an error before my cleaning even starts.Could you rearrange the code so that PhoneNumberField uses the default Django validation methods in the right order?
See https://docs.djangoproject.com/en/5.0/ref/forms/validation/
The validation should not be done in the
__init__()
method, but in the field'svalidate()
. This breaks the possibility of "normal" Django usage of this else very cool module.I think this would close #441 too as we then could use custom validators.
The text was updated successfully, but these errors were encountered: