diff --git a/src/components/ui/input.tsx b/src/components/ui/input.tsx
index acab65c..a26f9d0 100644
--- a/src/components/ui/input.tsx
+++ b/src/components/ui/input.tsx
@@ -1,18 +1,32 @@
import type { ComponentProps } from "solid-js";
-import { splitProps } from "solid-js";
+import { Show, splitProps } from "solid-js";
import { cn } from "@/lib/cn";
-export type InputProps = ComponentProps<"input">;
+export type InputProps = ComponentProps<"input"> & {
+ /**
+ * Message shown under the field. Marks it invalid and describes it for
+ * assistive tech; needs an `id` so the message can be linked.
+ */
+ error?: string;
+};
export function Input(props: InputProps) {
- const [local, rest] = splitProps(props, ["class"]);
+ const [local, rest] = splitProps(props, ["class", "error"]);
+ const errorId = () => (props.id ? `${props.id}-error` : undefined);
return (
+ <>
+
+ {local.error}
+
+ >
);
}
diff --git a/src/components/ui/textarea.tsx b/src/components/ui/textarea.tsx
index bd4f700..355e7db 100644
--- a/src/components/ui/textarea.tsx
+++ b/src/components/ui/textarea.tsx
@@ -1,18 +1,32 @@
import type { ComponentProps } from "solid-js";
-import { splitProps } from "solid-js";
+import { Show, splitProps } from "solid-js";
import { cn } from "@/lib/cn";
-export type TextareaProps = ComponentProps<"textarea">;
+export type TextareaProps = ComponentProps<"textarea"> & {
+ /**
+ * Message shown under the field. Marks it invalid and describes it for
+ * assistive tech; needs an `id` so the message can be linked.
+ */
+ error?: string;
+};
export function Textarea(props: TextareaProps) {
- const [local, rest] = splitProps(props, ["class"]);
+ const [local, rest] = splitProps(props, ["class", "error"]);
+ const errorId = () => (props.id ? `${props.id}-error` : undefined);
return (
+ <>
+
+ {local.error}
+
+ >
);
}
diff --git a/src/i18n/messages.ts b/src/i18n/messages.ts
index da2da1c..8fe18fd 100644
--- a/src/i18n/messages.ts
+++ b/src/i18n/messages.ts
@@ -616,6 +616,7 @@ export type MessageKey =
| "common.deleted"
| "common.saved"
| "common.loadAll"
+ | "form.fieldRequired"
| "common.close"
| "common.dismiss"
| "sidePanel.discardTitle"
@@ -2757,6 +2758,7 @@ const en: Dict = {
"common.deleted": "Deleted.",
"common.saved": "Saved successfully.",
"common.loadAll": "Load all",
+ "form.fieldRequired": "This field is required.",
"common.close": "Close",
"common.dismiss": "Dismiss",
"sidePanel.discardTitle": "Discard your changes?",
@@ -4900,6 +4902,7 @@ const tr: Dict = {
"common.deleted": "Silindi.",
"common.saved": "Başarıyla kaydedildi.",
"common.loadAll": "Tümünü yükle",
+ "form.fieldRequired": "Bu alan zorunlu.",
"common.close": "Kapat",
"common.dismiss": "Gizle",
"sidePanel.discardTitle": "Değişiklikler silinsin mi?",
diff --git a/src/pages/classes-page.test.tsx b/src/pages/classes-page.test.tsx
index 0e89f43..71c8471 100644
--- a/src/pages/classes-page.test.tsx
+++ b/src/pages/classes-page.test.tsx
@@ -86,3 +86,18 @@ test("a create whose blueprint left pairs behind holds the page until the report
expect(navigate).toHaveBeenCalledWith({ to: "/management/classes/$id", params: { id: "c-new" } }),
);
});
+
+test("a blank class name is flagged under the field, not sent", async () => {
+ getClasses.mockResolvedValue({ items: [] });
+ render(() => );
+
+ fireEvent.click(await screen.findByRole("button", { name: /New class/ }));
+ const field = await screen.findByLabelText(/^Name/);
+ fireEvent.input(field, { target: { value: " " } });
+ fireEvent.click(screen.getByRole("button", { name: "Create" }));
+
+ expect(await screen.findByText(/This field is required|Bu alan zorunlu/)).toBeTruthy();
+ expect(field.getAttribute("aria-invalid")).toBe("true");
+ expect(field.getAttribute("aria-describedby")).toBe("class-name-error");
+ expect(postClass).not.toHaveBeenCalled();
+});
diff --git a/src/pages/classes-page.tsx b/src/pages/classes-page.tsx
index 03401b8..898613c 100644
--- a/src/pages/classes-page.tsx
+++ b/src/pages/classes-page.tsx
@@ -57,6 +57,7 @@ function ClassesContent() {
const [yearId, setYearId] = createSignal("");
const [teacherId, setTeacherId] = createSignal("");
const [error, setError] = createSignal("");
+ const [nameError, setNameError] = createSignal("");
const [pending, setPending] = createSignal(false);
// A create whose grade's blueprint left pairs behind reports them here before
// it opens the class: the response is the only place those rows ever appear.
@@ -116,6 +117,13 @@ function ClassesContent() {
const createClass = async (event: SubmitEvent) => {
event.preventDefault();
setError("");
+ // Checked here rather than by the browser, so the message sits under the
+ // field in the app's own words — and a name of only spaces is caught too.
+ if (!name().trim()) {
+ setNameError(t("form.fieldRequired"));
+ document.getElementById("class-name")?.focus();
+ return;
+ }
setPending(true);
try {
const created = await postClass({
@@ -174,9 +182,9 @@ function ClassesContent() {
return (