درس النماذج الميسّرة

النمط الذي يتبعه كل نموذج في Lily مهما كان إطار العمل: سمِّ كل عنصر تحكم، وتحقّق عند الإرسال، وضع كل خطأ بجانب حقله، ولخّص الأخطاء حيث يصل التركيز.

ما الذي ستبنيه

نموذج من حقلين، إذا أُرسل وفيه أخطاء، يقول ما الخطأ بجانب كل حقل وفي ملخص أعلاه، وينقل التركيز إليه، كي يجد مستخدم لوحة المفاتيح أو قارئ الشاشة الأخطاء فورًا. يُعرض بلغة HTML بسيطة مع أصناف Lily؛ وكل كتالوج لإطار عمل يُخرج الترميز نفسه من مكوّناته Field، Hint، ErrorMessage و ErrorSummary . نحو 20 دقيقة؛ ويفيد أن تبدأ بـ درس HTML أولًا.

الخطوة 1 — سمِّ كل عنصر تحكم، وقل ما تتوقعه

<form class="form" novalidate>
  <div class="field">
    <label class="label" for="name">Full name</label>
    <input class="text-input" type="text" id="name" name="name" autocomplete="name" required />
  </div>
  <div class="field">
    <label class="label" for="email">Email</label>
    <p class="hint" id="email-hint">We only use this to send your appointment.</p>
    <input class="email-input" type="email" id="email" name="email"
      autocomplete="email" aria-describedby="email-hint" required />
  </div>
  <button class="button" type="submit">Continue</button>
</form>
  • عنصر <label for> مرئي على كل عنصر تحكم، وليس نصًا نائبًا بدلًا منه أبدًا: فالنص النائب يختفي عند الكتابة.
  • يرتبط التلميح باسم الحقل عبر aria-describedby، فيقرؤه قارئ الشاشة مع الحقل.
  • autocomplete يتيح للمتصفح ملء ما يعرفه (WCAG 1.3.5).
  • novalidate يوقف فقاعات المتصفح الخاصة، لأنك ستعرض الأخطاء بنفسك، بكلماتك وبلغتك. وتبقى required و type : فالبرنامج النصي يقرؤها.

الخطوة 2 — تحقّق عند الإرسال، لا أثناء الكتابة

إخبار أحدهم بأن بريده خاطئ بعد حرف واحد مجرد إزعاج. تحقّق عند الإرسال. السمات الـ63 لا تُظهر الحقل غير صالح إلا بعد تعديله ومغادرته (:user-invalid)، أو عندما تضبط aria-invalid="true"، فلا يصبح شيء أحمر عند تحميل الصفحة.

لا تعطّل زر الإرسال حتى يصبح النموذج صالحًا: فالزر المعطّل لا يستطيع أن يقول السبب.

الخطوة 3 — ضع كل خطأ بجانب حقله

<div class="field">
  <label class="label" for="email">Email</label>
  <p class="hint" id="email-hint">We only use this to send your appointment.</p>
  <p class="error-message" id="email-error">Enter an email address in the right format, like name@example.com</p>
  <input class="email-input" type="email" id="email" name="email" autocomplete="email"
    aria-invalid="true" aria-describedby="email-hint email-error" required />
</div>
  • aria-invalid="true" يقول إن الحقل خاطئ؛ وتقول الرسالة السبب وكيفية الإصلاح.
  • الرسالة موجودة في aria-describedby، بعد التلميح، فتُقرأ عندما يصل التركيز إلى الحقل.
  • قل ما يجب فعله، لا ما الخطأ فقط: «أدخل عنوان بريد إلكتروني بالصيغة الصحيحة»، لا «غير صالح».
  • لا تعتمد على اللون وحده أبدًا: النص هو الخطأ؛ والإطار الأحمر يكرره فقط (WCAG 1.4.1).

الخطوة 4 — لخّص الأخطاء حيث يصل التركيز

<div class="error-summary" role="alert" tabindex="-1" aria-labelledby="error-summary-title">
  <h2 id="error-summary-title">There is a problem</h2>
  <ul>
    <li><a href="#name">Enter your full name</a></li>
    <li><a href="#email">Enter an email address in the right format, like name@example.com</a></li>
  </ul>
</div>

ضعه في أعلى النموذج وانقل التركيز إليه عند الإرسال. كل رابط يقود مباشرة إلى حقله. tabindex="-1" يتيح للملخص تلقي التركيز دون إضافة محطة تنقّل.

الخطوة 5 — البرنامج النصي الذي يربط كل شيء

const messages = {
  name: { valueMissing: "Enter your full name" },
  email: {
    valueMissing: "Enter your email address",
    typeMismatch: "Enter an email address in the right format, like name@example.com",
  },
};

const form = document.querySelector("form");
form.addEventListener("submit", (event) => {
  form.querySelector(".error-summary")?.remove();
  const errors = [];
  for (const field of form.querySelectorAll("input")) {
    const old = document.getElementById(field.id + "-error");
    old?.remove();
    const describedBy = (field.getAttribute("aria-describedby") ?? "")
      .split(" ").filter((id) => id && id !== field.id + "-error");
    const problem = Object.keys(messages[field.name] ?? {}).find((key) => field.validity[key]);
    if (!problem) {
      field.removeAttribute("aria-invalid");
      field.setAttribute("aria-describedby", describedBy.join(" "));
      continue;
    }
    const text = messages[field.name][problem];
    const message = document.createElement("p");
    message.className = "error-message";
    message.id = field.id + "-error";
    message.textContent = text;
    field.before(message);
    field.setAttribute("aria-invalid", "true");
    field.setAttribute("aria-describedby", [...describedBy, message.id].join(" "));
    errors.push({ id: field.id, text });
  }
  if (errors.length === 0) return; // valid: let it submit
  event.preventDefault();
  const summary = document.createElement("div");
  summary.className = "error-summary";
  summary.setAttribute("role", "alert");
  summary.tabIndex = -1;
  summary.setAttribute("aria-labelledby", "error-summary-title");
  summary.innerHTML = '<h2 id="error-summary-title">There is a problem</h2><ul></ul>';
  for (const { id, text } of errors) {
    const link = document.createElement("a");
    link.href = "#" + id;
    link.textContent = text;
    const item = document.createElement("li");
    item.append(link);
    summary.querySelector("ul").append(item);
  }
  form.prepend(summary);
  summary.focus();
});

الرسائل رسائلك، وبلغتك: خاصية validity في المتصفح تقول أي قاعدة فشلت، وجدولك يقول ما يُقال للشخص. لا يحمل Lily أي نص إنجليزي لتترجمه.

تحقّق من عملك: أرسل النموذج فارغًا بلوحة المفاتيح. ينتقل التركيز إلى الملخص ويقرأ قارئ الشاشة «There is a problem» والخطأين. اتبع رابطًا: يصل التركيز إلى الحقل، فيُقرأ مع تلميحه وخطئه. صحّح الاثنين وأرسل: يمر النموذج ولا يبقى أي خطأ.

ما يجب تجنبه

  • تقييد ما كتبه أحدهم أو تصحيحه دون إخباره؛ أخبره بدلًا من ذلك.
  • مسح النموذج عند الخطأ: احتفظ بكل ما أُدخل.
  • خطأ لا يظهر إلا عند التمرير، أو في تلميح منبثق فقط.
  • انتهاء مهلة النموذج دون تحذير؛ استخدم timeout-dialog للتحذير والتمديد.

الخطوات التالية