Problem statement
Signup asks for the password twice and then accepts whatever arrives, because each field only ever sees its own value: password1 has no idea password2 exists. A rule about two fields has nowhere to live among the fields, so it lives on the form. The page has the same gap - app/templates/app/register.html renders the inputs but shows none of the errors, so a rejected signup looks identical to a fresh one.
Requirements
Implement:
- the cross-field rule on RegistrationForm in app/forms.py
- the error areas in app/templates/app/register.html
The following files are already implemented and provided: app/models.py, app/views.py and app/urls.py. RegistrationForm already declares username (forms.CharField), password1 and password2 (both forms.CharField with a PasswordInput widget), and keeps those three names. POST /register/ is wired: a valid form stores a Registration and redirects to the route named register, and an invalid one re-renders the same bound form with status 400, so the template is handed a form that already knows what went wrong. The inputs are already in the template.
1. The pair of passwords
- A submission whose two passwords differ is invalid, with exactly this message:
The two passwords do not match.
- That error lands among the form's own errors, reachable through form.non_field_errors(), and is attached to neither password1 nor password2. Neither password is wrong by itself; the rule is about the two of them together.
- A matching pair is valid, and cleaned_data["username"] is the submitted username.
2. Only compare what arrived
- When either password is missing, the required-field error is the whole story: that field carries its own error and no mismatch is reported. Both passwords have to be present before the comparison means anything.
3. Field errors survive
- A submission can be wrong in several ways at once. An empty username and a mismatched pair report both: the username error under username, the mismatch on the form.
4. The page shows both kinds of error
- Four elements, each found by its id:
| Element id | Shows |
|---|---|
| form-errors | every form-wide error, from form.non_field_errors |
| error-username | username's own errors |
| error-password1 | password1's own errors |
| error-password2 | password2's own errors |
- All four are present on every render, including the unsubmitted GET /register/ page, where each one holds no text at all.
- After an invalid POST, #form-errors carries the mismatch message and each error-<field> carries that field's own error text.
Notes
- Only app/forms.py and app/templates/app/register.html change.
- The form's own clean() runs once, after every field has cleaned itself, and is the only place that sees all the values together; a clean_<field> method cannot see its neighbours. Call super().clean() first and return the cleaned data.
- Read a value out of the cleaned data with .get() rather than [...]: a field that failed its own validation is absent.
- Raising inside clean() attaches the message to the form. self.add_error(None, message) is the same thing written differently, while self.add_error("password2", message) would pin it to that field instead.
- Each element is found by its id, and only its text counts, with runs of whitespace collapsed to one space. Extra attributes and classes, wrapper elements and tags inside an element are all fine.
- Do not add password strength, length or character rules; the only new rule is that the two match.
- Leave the inputs that are already in the template alone; add the error areas around them.
