trix icon indicating copy to clipboard operation
trix copied to clipboard

W3C Constraint Validation Violation: `setCustomValidity` Silently Erased on User Input

Open codeCraft-Ritik opened this issue 1 month ago • 0 comments

Location

  • File: src/trix/elements/trix_editor_element.js
  • Lines: 230–235, 244–256

Root Cause

In ElementInternalsDelegate:

setFormValue(value) {
  this.value = value
  this.#validate()
  this.#internals.setFormValue(this.element.disabled ? undefined : this.value)
}

setCustomValidity(validationMessage) {
  this.#validate(validationMessage)
}

#validate(customValidationMessage = "") {
  const { required, value } = this.element
  const valueMissing = required && !value
  const customError = !!customValidationMessage
  const input = makeElement("input", { required })
  const validationMessage = customValidationMessage || input.validationMessage

  this.#internals.setValidity({ valueMissing, customError }, validationMessage)
}
  1. Under the WHATWG HTML Constraint Validation Specification, calling element.setCustomValidity("Error message") sets the control's custom validity error state. This error state must persist until explicitly cleared by calling element.setCustomValidity("").
  2. In ElementInternalsDelegate, there is no private #customValidationMessage field stored on the instance.
  3. When setCustomValidity("Please enter at least 50 words") is called, #validate("...") sets customError: true.
  4. However, as soon as the user types a single character in the editor, EditorController.updateInputElement calls setFormValue(value).
  5. setFormValue invokes this.#validate() with no arguments, which evaluates the default parameter customValidationMessage = "".
  6. customError becomes false, immediately erasing the developer's custom validation message and marking the form valid on the user's first keystroke.

Reproduction

const el = document.querySelector("trix-editor")

el.setCustomValidity("Character limit not reached")
console.log(el.checkValidity()) // false
console.log(el.validationMessage) // "Character limit not reached"

// User types one character
el.editor.insertString("a")

// Custom error was wiped out!
console.log(el.checkValidity()) // true (Expected: false until cleared by setCustomValidity(""))
console.log(el.validationMessage) // "" (Expected: "Character limit not reached")

Proposed Patch

In src/trix/elements/trix_editor_element.js:

  class ElementInternalsDelegate {
    value = ""
    #internals
    #formDisabled
+   #customValidationMessage = ""

    constructor(element) {
      this.element = element
      this.#internals = element.attachInternals()
      this.#formDisabled = false
    }

    setCustomValidity(validationMessage) {
+     this.#customValidationMessage = validationMessage || ""
-     this.#validate(validationMessage)
+     this.#validate()
    }

-   #validate(customValidationMessage = "") {
+   #validate() {
      const { required, value } = this.element
      const valueMissing = required && !value
-     const customError = !!customValidationMessage
+     const customError = !!this.#customValidationMessage
      const input = makeElement("input", { required })
-     const validationMessage = customValidationMessage || input.validationMessage
+     const validationMessage = this.#customValidationMessage || input.validationMessage

      this.#internals.setValidity({ valueMissing, customError }, validationMessage)
    }

codeCraft-Ritik avatar Oct 06 '26 04:10 codeCraft-Ritik