mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 07:42:23 +00:00
Show a refused profile field under the input it belongs to
The onboarding baseline step flashed a toast naming one refused field and walked on to the done screen, stamping the account as set up over a value the server never stored. The value the person typed was gone with the step. A refused field now writes its reason into that field's own error slot, marks the control invalid, and holds the step until the person corrects it. Every refused field is named, not only the first, and the reason comes from the validator code rather than the validator's own prose, in all seven shipped languages.
This commit is contained in:
1 parent
ae20ee3d9d
commit
36cbf48286
16 files changed
+438
-26
No files matched your search
@@ -60,3 +60,52 @@ export function describeRejectedProfileField(
|
||||
const labelKey = labelKeys[first.path];
|
||||
return labelKey ? t(labelKey) : first.path;
|
||||
}
|
||||
|
||||
/**
|
||||
* The sentence a person reads under the input, keyed by the validator
|
||||
* code the server sent back. The code is the only reason that crosses
|
||||
* the wire — `message` is the validator's own prose and is deliberately
|
||||
* never shown — so each code gets one plain sentence saying what was
|
||||
* wrong with the value, in the person's language.
|
||||
*
|
||||
* A code with no entry here falls back to `other`, which says the field
|
||||
* was not accepted without inventing a reason. New Zod codes therefore
|
||||
* degrade to something honest instead of to silence.
|
||||
*/
|
||||
const REJECTION_REASON_KEYS: Record<string, string> = {
|
||||
too_big: "settings.profileRejection.tooBig",
|
||||
too_small: "settings.profileRejection.tooSmall",
|
||||
invalid_value: "settings.profileRejection.invalidValue",
|
||||
invalid_format: "settings.profileRejection.invalidFormat",
|
||||
invalid_type: "settings.profileRejection.invalidType",
|
||||
};
|
||||
|
||||
const REJECTION_FALLBACK_KEY = "settings.profileRejection.other";
|
||||
|
||||
/**
|
||||
* Turn a rejection list into one sentence per field, keyed by the
|
||||
* schema path so a form can drop each one into the slot under the
|
||||
* input it belongs to.
|
||||
*
|
||||
* Every refused field is carried, not just the first: a submission with
|
||||
* two bad values that only names one sends the person round the loop
|
||||
* twice. Where a field is refused for several reasons at once the first
|
||||
* one wins — the others are restatements of the same wrong value.
|
||||
*
|
||||
* `labelKeys` is accepted for symmetry with
|
||||
* `describeRejectedProfileField` and for the caller that wants to know
|
||||
* which fields it can actually render; a path the calling screen has no
|
||||
* input for still gets an entry, so nothing is dropped on the floor.
|
||||
*/
|
||||
export function describeRejectedProfileFields(
|
||||
fields: RejectedProfileField[] | undefined,
|
||||
t: (key: string, params?: Record<string, string | number>) => string,
|
||||
): Record<string, string> {
|
||||
const out: Record<string, string> = {};
|
||||
for (const field of fields ?? []) {
|
||||
if (out[field.path]) continue;
|
||||
const key = REJECTION_REASON_KEYS[field.code] ?? REJECTION_FALLBACK_KEY;
|
||||
out[field.path] = t(key);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
Reference in new issue
Block a user