ประกาศข้อผิดพลาดที่เข้าถึงได้

ฟีเจอร์บนเว็บ: :user-valid และ :user-invalid

ปัญหา

การตรวจสอบ HTML5 มาตรฐานจะให้การตอบกลับด้วยภาพ (ผ่าน :invalid หรือ :user-invalid) แต่จะไม่ซิงค์กับแอตทริบิวต์การช่วยเหลือพิเศษ เช่น aria-invalid โดยอัตโนมัติ

หากคุณใช้การจัดรูปแบบ :invalid มาตรฐาน โปรแกรมอ่านหน้าจออาจอ่านว่า "รายการไม่ถูกต้อง" ทันทีที่ผู้ใช้กด Tab ไปยังช่องที่ต้องกรอกซึ่งว่างเปล่าอยู่ ซึ่งจะสร้างประสบการณ์ที่รบกวนผู้ใช้ที่ใช้เทคโนโลยีความช่วยเหลือพิเศษ เนื่องจากระบบจะประกาศข้อผิดพลาดก่อนที่จะมีการโต้ตอบเกิดขึ้น

วิธีแก้ปัญหา

เราต้องการให้ใช้สถานะแบบเป็นโปรแกรม (aria-invalid="true") ในเวลาเดียวกันกับที่ใช้สถานะภาพ (:user-invalid) เนื่องจาก :user-invalid อาศัยแฟล็ก "ผู้ใช้โต้ตอบ" ภายในของเบราว์เซอร์ เราจึงใช้ JavaScript เพื่อตรวจสอบว่าตัวเลือกนี้ตรงกันระหว่างเหตุการณ์การโต้ตอบมาตรฐานได้

ดูรายละเอียดเพิ่มเติมได้ที่ MDN aria-invalid

กลยุทธ์การติดตั้งใช้งาน

  1. เลเยอร์ภาพ: ใช้ CSS :user-invalid เพื่อแสดงเส้นขอบ/ไอคอน
  2. เลเยอร์การช่วยเหลือพิเศษ: ใช้ aria-invalid และ aria-errormessage เพื่อสื่อสารสถานะกับเทคโนโลยีความช่วยเหลือพิเศษ (AT)
  3. Bridge Visual & Accessibility Layer: สร้างยูทิลิตี JavaScript ขนาดเล็กที่คอยฟังเหตุการณ์ blur และ input ตรวจสอบว่าองค์ประกอบตรงกับ :user-invalid หรือไม่ และอัปเดตแอตทริบิวต์ ARIA ตามความเหมาะสม

คู่มือการติดตั้งใช้งาน

1. โครงสร้าง HTML

ลิงก์อินพุตกับข้อความแสดงข้อผิดพลาดโดยใช้ aria-errormessage (หรือ aria-describedby เพื่อการรองรับที่กว้างขึ้น)

<form>
  <div class="field">
    <label for="email">Email</label>
    <input 
      type="email" 
      id="email" 
      required 
      aria-errormessage="email-error"
    >
    <span id="email-error" class="error-msg">
      Please enter a valid email address.
    </span>
  </div>
</form>

2. CSS

ควบคุมการแสดงข้อความแสดงข้อผิดพลาดโดยใช้คลาสเทียมเนทีฟ :user-invalid

.error-msg {
  display: none;
  color: #d93025;
}

/* Show error message when input is user-invalid */
input:user-invalid ~ .error-msg {
  display: block;
}

/* Optional: Visual cues on the input itself */
input:user-invalid {
  border-color: #d93025;
}

3. JavaScript

เนื่องจากไม่มีเหตุการณ์ "UserInvalidChanged" ให้เชื่อมโยงกับเหตุการณ์แบบฟอร์มมาตรฐานเพื่อตรวจสอบสถานะ

const updateAriaState = (event) => {
  const input = event.target;
  if (!input.matches?.('input, textarea, select')) return;

  // Check if the browser currently considers this input "user-invalid"
  const isUserInvalid = input.matches(':user-invalid');
  
  if (isUserInvalid) {
    input.setAttribute('aria-invalid', 'true');
  } else {
    input.removeAttribute('aria-invalid');
  }
};

// Listen on the document to handle dynamically added fields.
// 'blur' and 'focus' do not bubble, so we must use the capture phase (true).
document.addEventListener('blur', updateAriaState, true);
document.addEventListener('focus', updateAriaState, true);

// Also update on input if we've already shown the error, 
// so the error clears immediately when fixed.
document.addEventListener('input', (event) => {
  const input = event.target;
  if (!input.matches?.('input, textarea, select')) return;

  const hasAriaInvalid = input.hasAttribute('aria-invalid');
  const ariaInvalid = input.getAttribute('aria-invalid');
  if (hasAriaInvalid && ariaInvalid === 'true') {
    updateAriaState(event);
  }
});

การสำรองและการรองรับเบราว์เซอร์

การตรวจจับฟีเจอร์

คุณตรวจสอบการรองรับได้ใน CSS และ JavaScript

การตรวจสอบ JavaScript:

if (!CSS.supports('selector(:user-invalid)')) {
  // Fallback logic here
}

CSS สำหรับโฆษณาสำรอง

เพื่อให้แน่ใจว่าตรรกะสำรองของคุณไม่สามารถแยกความแตกต่างจากลักษณะการทำงานแบบเนทีฟได้ คุณต้องใช้รูปแบบข้อผิดพลาดกับทั้งคลาสจำลองและคลาสสำรอง

/* Apply error styles to both native selector and fallback class */
input:user-invalid,
input.user-invalid-fallback {
  border-color: #d93025;
  background-color: #fce8e6;
}

/* Show error message for both cases */
input:user-invalid ~ .error-msg,
input.user-invalid-fallback ~ .error-msg {
  display: block;
}

ตรรกะสำรอง

หากไม่มี :user-invalid ให้ติดตามสถานะการโต้ตอบด้วยตนเองโดยใช้ WeakMap

const UserInvalidFallback = (() => {
  const dirtyState = new WeakMap();

  const updateState = (input) => {
    const isValid = input.checkValidity();

    // Update both visual and ARIA state
    input.classList.toggle('user-invalid-fallback', !isValid);
    input.classList.toggle('user-valid-fallback', isValid);

    if (!isValid) {
      input.setAttribute('aria-invalid', 'true');
    } else {
      input.removeAttribute('aria-invalid');
    }
  };

  const handleEvent = (event) => {
    const input = event.target;

    if (event.type === 'reset' && input.matches?.('form')) {
      const controls = input.elements || [];
      for (const control of controls) {
        dirtyState.delete(control);
        control.classList.remove('user-invalid-fallback');
        control.classList.remove('user-valid-fallback');
        control.removeAttribute('aria-invalid');
      }
      return;
    }

    if (!input.matches?.('input, textarea, select')) return;

    if (event.type === 'input' || event.type === 'change') {
      const state = dirtyState.get(input) || { hasInteracted: false, hasBlurred: false };
      state.hasInteracted = true;
      dirtyState.set(input, state);
      if (state.hasBlurred) {
        updateState(input);
      }
    } else if (event.type === 'blur') {
      const state = dirtyState.get(input) || { hasInteracted: false, hasBlurred: false };
      state.hasBlurred = true;
      dirtyState.set(input, state);
      if (state.hasInteracted) {
        updateState(input);
      }
    }
  };

  const init = () => {
    if (CSS.supports('selector(:user-invalid)')) return;

    document.addEventListener('blur', handleEvent, true); // Capture phase required
    document.addEventListener('input', handleEvent, true);
    document.addEventListener('change', handleEvent, true);
    document.addEventListener('reset', handleEvent, true); // Capture resets
  };

  return { init };
})();

// Initialize globally
UserInvalidFallback.init();

ปัจจัยอื่นๆ ที่ควรพิจารณา

  1. aria-live พบกับ aria-errormessage:

    • aria-errormessage เชื่อมต่ออินพุตกับข้อความ แต่โปรแกรมอ่านหน้าจออาจไม่อ่านออกเสียงทันทีที่ปรากฏ (เฉพาะเมื่อโฟกัสอินพุต)
    • หากต้องการให้มีการประกาศทันทีเมื่อเกิดข้อผิดพลาด (เช่น เมื่อเบลอ) ให้พิจารณาเพิ่ม role="alert" หรือ aria-live="polite" ลงในคอนเทนเนอร์ข้อความแสดงข้อผิดพลาด แต่ให้ทดสอบอย่างละเอียดเพื่อหลีกเลี่ยง "การประกาศซ้ำ" เมื่อผู้ใช้โฟกัสที่ช่องเพื่อแก้ไขข้อผิดพลาด
  2. การปรับให้เหมาะกับต่างประเทศ:

    • ตรวจสอบว่าเนื้อหาข้อความของข้อความแสดงข้อผิดพลาด (#email-error) ได้รับการแปลแล้ว โดยตรรกะยังคงเหมือนเดิม