Django form validation requires external resource
I have a Django form where the user enters a password. I can't validate the password locally; I call an external API to set the password, and if there's a problem it returns an error message (i.e. password too short, contains a dictionary word, etc.)
The API also needs the user's login name, which is not an element on that form, but is known in the view.
So where is the best place to do the form validation? In the form's clean() method, or in the view code?
And if I use the clean() method, how do I pass in the user's login name?
This validation is related to the form, and i think you should put this validation to the form. You can add username to form data and pass it to the form. For example,if username will be wrong, you can raise ValidationError. And others messages password short and etc. seems to be validation errors, so should be in the form.
For example you pass request.POST to the form. You can do something like this:
data = request.POST.dict() data['username'] = 'myusername' form = Form(data)
And what is the problem to add username to the form as field, if you always need it?