सुलभ फ़ॉर्म ट्यूटोरियल

हर Lily फ़ॉर्म यही पैटर्न अपनाता है, फ़्रेमवर्क चाहे जो हो: हर नियंत्रण को लेबल दें, सबमिट करने पर जाँचें, हर त्रुटि उसके फ़ील्ड के पास रखें, और जहाँ फ़ोकस पहुँचे वहाँ त्रुटियों का सारांश दें।

आप क्या बनाएँगे

दो फ़ील्ड वाला एक फ़ॉर्म, जो गलतियों के साथ सबमिट होने पर बताता है कि क्या गलत है, हर फ़ील्ड के पास और ऊपर एक सारांश में, और फ़ोकस वहीं ले जाता है, ताकि कीबोर्ड या स्क्रीन रीडर उपयोगकर्ता तुरंत त्रुटियाँ पा सके। इसे Lily क्लास हुक के साथ सादे HTML में दिखाया गया है; हर फ़्रेमवर्क कैटलॉग अपने 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 का उपयोग करें।

अगले कदम