-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decouple the validation from the presentation of invalid fields
- Loading branch information
Showing
7 changed files
with
39 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* Here we turn off the style for the valid fields, so it only shows invalid */ | ||
.was-validated { | ||
.form-control, | ||
.form-select { | ||
--bs-form-valid-border-color: var(--bs-border-color); | ||
--bs-success-rgb: var(--stanford-digital-blue-rgb); | ||
|
||
&:valid { | ||
background-image: none; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,14 @@ | ||
import { Controller } from '@hotwired/stimulus' | ||
|
||
// Validates free-text tags fields and sets the .invalid class on elements | ||
// that aren't well formed tags | ||
// Alerts the form to display errors if the tags aren't well formed | ||
export default class extends Controller { | ||
connect () { | ||
this.validate() | ||
} | ||
|
||
validate () { | ||
const parts = this.element.value.trim().split(/\s*:\s*/) | ||
this.element.value = parts.join(' : ') | ||
this.element.classList.toggle('is-invalid', this.is_invalid(parts)) | ||
} | ||
|
||
is_invalid (parts) { | ||
return (parts.length === 1 && parts[0] !== '') || | ||
(parts.length > 1 && parts.includes('')) | ||
if (this.element.validity.patternMismatch) { | ||
this.element.reportValidity() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters