Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions docs/adr/0008-pat-validation-own-and-group-errors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 0008 pat-validation: Separate own errors and group errors

Status: Accepted (2026-11-29)

## Context

pat-validation treated all inputs with the same name as one group which shared
a single validation state:

- `set_error()` set the custom validity on all inputs with the same name.
- `check_input()` started by clearing the custom validity of all of them.
- `remove_error()` cleared the state and removed the error messages of all of
them.

This was introduced for radio and checkbox groups and for `min-values` and
`max-values`, where the error belongs to the group.

But each input only runs its own checks. Checking one input of a group cleared
the errors which another input of the group had set, and did not set them
again. Forms which use the same name for inputs which are not a group broke.
For example, a date input and a time input named `start`, which the server
reads as a `[date, time]` list: a `not-after` error on the date input was
cleared when the time input was checked, and an invalid form could be
submitted. Zope `:list` and `:records` forms have the same problem.

Deciding which inputs form a group, e.g. by input type or by the presence of
`min-values`/`max-values`, does not solve this. As soon as a group mixes
checks of single inputs with checks of the group, the errors of the single
inputs are lost again.

## Decision

pat-validation distinguishes two kinds of errors:

- **Own errors** belong to a single input: all native errors (except the
`required` error of a radio group), `not-after`, `not-before`, `equality`
and the custom messages for native errors. They are set, cleared and shown
only for that input. Checking an input never touches the own errors of
other inputs with the same name.
- **Group errors** belong to all inputs with the same name: `min-values`,
`max-values` and a required radio group. They only depend on the whole
group, so any input of the group computes the same result. Every check of
an input of the group computes the group error again and sets it on all
inputs of the group. `min-values` and `max-values` are read from the first
input of the group which defines them.

The custom validity of an input is its own error or, if there is none, the
group error.

An own error message is shown after its input (or its wrapping label). The
group error message is shown once, after the last input of the group. It is
not shown while an input of the group shows an own error. The user fixes
those first.

## Consequences

- Same-named inputs which are not a group, e.g. date and time inputs or Zope
`:list` fields, keep their own errors.
- The result does not depend on the order in which the inputs are checked.
- There is no list of input types which form a group. The group checks apply
to all inputs with the same name, as before.
- Checkbox groups with `required` on single checkboxes get one message per
invalid checkbox instead of one message per group. A required radio group
still gets one message.
- Breaking change for JavaScript users: `set_error()` and `remove_error()`
only affect the given input. `remove_error()` defaults to
`all_of_group = false`. The new methods `check_group()`,
`render_error_message()` and `render_group_error_message()` handle the
group errors and the messages.
11 changes: 10 additions & 1 deletion src/pat/validation/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,13 @@ In addition both the input element and its label will get an `warning` class.
</label>
```

Checkboxes and radio buttons are treated differently: The error message is always set after the last element of the inputs with the same name. If that input is wrapped in a label, the error message is set after the label.
Errors which belong to a group of inputs with the same name are treated differently: a required radio group and the `min-values` and `max-values` checks (see below).
The error message of the group is shown once, after the last input of the group. If that input is wrapped in a label, the error message is set after the label.
All inputs of the group are invalid.

All other errors belong to the single input, also if other inputs have the same name.
For example, a date input and a time input with the same name keep their own errors.
If an input of a group has its own error, the group error message is shown only after the own error is fixed.

```html
<fieldset>
Expand Down Expand Up @@ -129,6 +135,9 @@ This allows a hidden input with the same name to submit a default value if no ch
</fieldset>
```

It is enough to define `min-values` and `max-values` on one input of the group.
If multiple inputs of the group define them, the options of the first one are used.


### Form elements outside the form

Expand Down
Loading