- The browser's own form checks (required, minLength, e-mail format) show Hungarian text on every page (ValidationMessages in the root layout). - User-facing backend errors are Hungarian: e-mail/password login, password length, links, invitations, sharing, questions, limits. Google/Apple token failures log the reason and tell the user to retry. - errorText covers not_found, horoscope_unavailable and internal errors. - Input fields: lighter background and gold caret on focus, the example text steps aside, so a click visibly lands in the field. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
84 lines
3.4 KiB
TypeScript
84 lines
3.4 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect } from "react";
|
|
|
|
type Field = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement;
|
|
|
|
/** Hungarian text for a field the browser found invalid. */
|
|
function message(el: Field): string {
|
|
const v = el.validity;
|
|
if (v.valueMissing) {
|
|
if (el instanceof HTMLSelectElement) return "Válassz a listából.";
|
|
if (el instanceof HTMLInputElement && el.type === "checkbox") return "Jelöld be a folytatáshoz.";
|
|
if (el instanceof HTMLInputElement && el.type === "radio") return "Válassz egyet.";
|
|
return "Töltsd ki ezt a mezőt.";
|
|
}
|
|
if (v.typeMismatch) {
|
|
if (el.type === "email") return "Adj meg egy érvényes e-mail-címet.";
|
|
if (el.type === "url") return "Adj meg egy érvényes webcímet.";
|
|
}
|
|
if (v.tooShort && !(el instanceof HTMLSelectElement)) {
|
|
return `Legalább ${el.minLength} karakter legyen (most ${el.value.length}).`;
|
|
}
|
|
if (v.tooLong && !(el instanceof HTMLSelectElement)) return `Legfeljebb ${el.maxLength} karakter lehet.`;
|
|
if (el instanceof HTMLInputElement) {
|
|
if (v.rangeUnderflow) return `A legkisebb megengedett érték: ${el.min}.`;
|
|
if (v.rangeOverflow) return `A legnagyobb megengedett érték: ${el.max}.`;
|
|
if (v.patternMismatch) return el.title || "A formátum nem megfelelő.";
|
|
if (v.badInput && el.type === "number") return "Számot adj meg.";
|
|
}
|
|
return "Az érték nem megfelelő.";
|
|
}
|
|
|
|
function isField(t: EventTarget | null): t is Field {
|
|
return t instanceof HTMLInputElement || t instanceof HTMLTextAreaElement || t instanceof HTMLSelectElement;
|
|
}
|
|
|
|
/**
|
|
* The browser's own form checks (required, minLength, type="email"…) speak
|
|
* the browser's language. This puts Hungarian text into those bubbles on
|
|
* every page; the checks themselves stay the browser's.
|
|
*/
|
|
export function ValidationMessages() {
|
|
useEffect(() => {
|
|
// Fields whose message was set here, so it can be cleared again.
|
|
const marked = new WeakSet<Field>();
|
|
const clear = (el: Field) => {
|
|
if (marked.has(el)) {
|
|
el.setCustomValidity("");
|
|
marked.delete(el);
|
|
}
|
|
};
|
|
const onInvalid = (e: Event) => {
|
|
const el = e.target;
|
|
if (!isField(el)) return;
|
|
clear(el);
|
|
// A message the page set itself (setCustomValidity) is kept.
|
|
if (el.validity.customError || el.validity.valid) return;
|
|
el.setCustomValidity(message(el));
|
|
marked.add(el);
|
|
};
|
|
const onEdit = (e: Event) => {
|
|
if (isField(e.target)) clear(e.target);
|
|
};
|
|
// A submit attempt checks the fields afresh: a value set by code (no input event) is not stuck.
|
|
const onClick = (e: Event) => {
|
|
const button = (e.target as Element | null)?.closest?.("button, input[type=submit]");
|
|
const form = (button as HTMLButtonElement | HTMLInputElement | null)?.form;
|
|
if (!form || (button as HTMLButtonElement).type !== "submit") return;
|
|
for (const el of Array.from(form.elements)) if (isField(el)) clear(el);
|
|
};
|
|
document.addEventListener("invalid", onInvalid, true);
|
|
document.addEventListener("input", onEdit, true);
|
|
document.addEventListener("change", onEdit, true);
|
|
document.addEventListener("click", onClick, true);
|
|
return () => {
|
|
document.removeEventListener("invalid", onInvalid, true);
|
|
document.removeEventListener("input", onEdit, true);
|
|
document.removeEventListener("change", onEdit, true);
|
|
document.removeEventListener("click", onClick, true);
|
|
};
|
|
}, []);
|
|
return null;
|
|
}
|