Reputation: 382
When using an email field, attempting to submit a form with an invalid email address raises and error like this:
What I like about this:
doesn't reload the page, rather doesn't submit at all and instead displays an error
the look
Here is what I am able to accomplish currently with Name and Password (CharFields)
The "A name is needed!" and "This field is required" messages only display after the form is submitted, which is not as sleek as displaying without the form even needing to be submitted.
Upvotes: 0
Views: 241
Reputation: 382
kmmbvnr directed me to what I was looking for. To add the type of validation I was looking for, add code similar to that below:
class UserForm(forms.ModelForm):
...
username = forms.CharField(
widget=forms.TextInput(attrs={'required': 'true'}),#HERE
)
password = forms.CharField(
widget=forms.PasswordInput(attrs={'required': 'true'}),#OR HERE
)
...
Upvotes: 0
Reputation: 6139
The popup that you see for the email it is not django validation. It is the build in in-browser html5 email field checking (and some older browsers will not do it for you).
And you can't get any kind of validation in html5, but for checking field presents you can add required
attribute to the rendered input field.
There are several ways to add attibutes to widgets in django. take a look to the How do I get Django forms to show the html required attribute? and select appropriate for you.
Upvotes: 1