StaffCoder
Django
DjangoDjango 5.2Python 3.13
18 modules · 72 challengesSign up
Module 10 · Forms

Custom Field Rules

easyDjango FormsInput Validation100%2 of 2 solved

Problem statement

Bookings are accepted with usernames that contain spaces and with event dates that have already passed, because the only validation running is the one each field class does for itself: CharField checks that text arrived, DateField checks that it parses as a date, and neither has an opinion about what the booking actually means. Two rules are missing from app/forms.py, and they belong in two different places - one as a reusable validator, because a second form needs exactly the same rule, and one attached to the form, because it has to compare the value against today.

Requirements

Implement:

  • validate_no_spaces(value) in app/forms.py
  • the two rules on BookingForm in app/forms.py
  • the shared rule on ProfileForm in app/forms.py

The following files are already implemented and provided: app/models.py, app/views.py, app/urls.py and the template. Booking holds username (CharField(max_length=200)) and event_date (DateField). BookingForm already declares username (forms.CharField) and event_date (forms.DateField) with no rules attached, and ProfileForm already declares handle (forms.CharField), which follows exactly the rule username follows. POST /book/ stores a Booking through BookingForm, redirecting to the route named book on success and re-rendering with status 400 on failure; ProfileForm is not routed anywhere and is validated directly.

1. validate_no_spaces - the reusable username rule

  • It keeps its name and takes the cleaned value as its one argument, so it can be imported by that name and called directly.
  • A value containing a space raises forms.ValidationError with exactly this message:
Username must not contain spaces.
  • A value with no space in it raises nothing. A validator says nothing when the value is fine; its return value is never read.

2. The rule attached in both places

  • BookingForm.username and ProfileForm.handle both refuse a value containing a space, both with that same message, and both report it under the field's own name - username on one form, handle on the other.
  • The check itself exists in one place only. That message string appears exactly once in app/forms.py, so a second copy of the rule for the second form fails this requirement however it is wired up.

3. The future-date rule on event_date

  • An event_date that is not later than the current local date is invalid, with exactly this message:
The event date must be in the future.
  • Today itself is not in the future and is refused; tomorrow passes. The comparison is against the current local date, so the rule is still right tomorrow rather than against a date written into the file.
  • The error lands under the event_date key of form.errors.

4. Errors stay on their fields

  • A booking that breaks both rules at once reports both, each under its own field name, and form.non_field_errors() stays empty.
  • A valid booking exposes cleaned_data["username"] as the submitted text and cleaned_data["event_date"] as a date.

Notes

  • Only app/forms.py changes.
  • A validator is a plain callable attached through a field's validators list, and is the right home for a rule two forms share. A clean_<field> method lives on one form, reads self.cleaned_data, and must return the value it wants kept - drop the return and the value is lost.
  • Both kinds of rule attach their error to the field, so it appears under the field's name in form.errors and never in form.non_field_errors().
  • Do not add length limits, character rules or any validation beyond these two rules.

More in Forms

See the module
Contact Form BasicsDeclare a Django Form with typed required fields so the contact view stores validated data only.100%2 of 2 solvedeasyCross-Field Password CheckCompare two password fields in the form's clean() method and render field and non-field errors in the template.100%2 of 2 solvedeasyEvent Registration FormBuild an event registration form end to end: typed required fields, a reusable validator, per-field and cross-field rules, error rendering and the view flow.100%2 of 2 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.