Reputation: 6238
How can I raise an exception when a user hits the "Submit" button on a registration form, without having filled in any of the fields on a form?
In the event that this would happen, I only want to display that single error ("Please fill out the form before submitting."), without displaying the field errors that would typically be displayed if a user forgets to fill in a required field.
In other words, if a user hits the submit button without filling in a single field, I want it to display one error only: "Please fill out the field before submitting."
I figure there might be two ways to achieve this:
Generate the error somewhere in the clean methods of the form; and have an if statement in the template that disregards all other errors if this error is produced.
Generate the error in the actual template, as soon as the user hits the submit button (I'm not sure if this is even possible).
The problem with the first option is that I would need to use an if statement, like the one below. But I don't know how to single out that particular error.
{% if form.special_error %}
...display the special error
...disregard any other field or non_field errors
{% endif %}
I only know how to use form.errors
, form.non_field_errors
, and form.<specific_field>
in if statements. None of these would specifically indicate that this special error occurred.
There has to be an easy way to do this!!!
Upvotes: 2
Views: 669
Reputation: 7238
If that's the case you could just check on the server side if the form has changed instead of validating it. By doing this:
def submit_form(request):
if request.method == 'POST':
form = Form(request.POST)
if not form.has_changed():
#Generate Error
Upvotes: 3