Skip to content

Commit 0d598d7

Browse files
committed
Fix: Add attribute to persist server validation messages
1 parent 0280553 commit 0d598d7

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,17 @@ Add text content to the `<validation-output>` element to display server-side val
7272
```html
7373
<input type="email" id="email" value="known@email" />
7474
<validation-output for="email">
75-
Email <b>known@email</b> already registered
75+
Invalid credentials. Please try again.
76+
</validation-output>
77+
78+
<input type="text" id="persistent" value="rejected-value" />
79+
<validation-output for="persistent" persistent>
80+
This error will be show again if the value is changed back to the rejected value
7681
</validation-output>
7782
```
7883

7984
When the user modifies the input field, the server-side message is hidden.
80-
If the user changes the value back to the original, the message will be shown again.
85+
If the `persistent` attribute is set and the user changes the value back to the rejected value, the message will be shown again.
8186

8287
**Note:** When displaying server-side validation errors, the input element won't have the `:user-invalid` pseudo-class applied.
8388
To style server-validated inputs, you can use the `data-server-invalid="true"` attribute selector instead.
@@ -108,7 +113,8 @@ validation-output:state(has-error) {
108113

109114
### Attributes
110115

111-
| Attribute | Required | Description |
112-
|------------|----------|----------------------------------------------------------|
113-
| `for` | Yes | ID of the input element to validate |
114-
| `template` | No | ID of the template containing custom validation messages |
116+
| Attribute | Required | Description |
117+
|--------------|----------|-----------------------------------------------------------------------------------|
118+
| `for` | Yes | ID of the input element to validate |
119+
| `template` | No | ID of the template containing custom validation messages |
120+
| `persistent` | No | Show the server validation error again if the value reverts to the rejected value |

demo.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@
5757
<label for="decline" class="label mt-8">Server declined the text "decline"</label>
5858
<div class="relative">
5959
<input type="text" id="decline" class="input" value="decline" aria-describedby="decline-validation-error" placeholder="Enter everything except 'decline'">
60-
<validation-output for="decline" class="validation-output" id="decline-validation-error">The word <b>"decline"</b> was declined on the server.</validation-output>
60+
<validation-output for="decline" class="validation-output" id="decline-validation-error" persistent>
61+
The word <b>"decline"</b> was declined on the server.
62+
</validation-output>
6163
</div>
6264

6365
<!-- Custom validation message -->

validation-output.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ class ValidationOutput extends HTMLElement {
4343

4444
this.#serverError = this.innerHTML;
4545
this.#serverErrorValue = this.#for.value;
46-
this.#for.setCustomValidity(this.#serverError);
46+
47+
// We only set the custom validity if the element is persistent.
48+
// If we set this while not persistent, the client will not be able to submit the form
49+
if (this.hasAttribute("persistent")) {
50+
this.#for.setCustomValidity(this.#serverError);
51+
}
4752

4853
// If the element is added to the DOM, we need to wait until the element is rendered.
4954
// Otherwise, transitions/animations will not work correctly.
@@ -131,7 +136,11 @@ class ValidationOutput extends HTMLElement {
131136
}
132137

133138
// Check if the user reverted to the original server-rejected value
134-
if (this.#serverError && this.#for.value === this.#serverErrorValue) {
139+
if (
140+
this.#serverError &&
141+
this.#for.value === this.#serverErrorValue &&
142+
this.hasAttribute("persistent")
143+
) {
135144
this.#for.setCustomValidity(this.#serverError);
136145
this.#setErrorMessage(this.#serverError);
137146
return;

0 commit comments

Comments
 (0)