-
Notifications
You must be signed in to change notification settings - Fork 28
Description
Hi all,
I'm looking to implement validation feedback as follows:
- only show errors when input value has changed and focus is lost e.g user moves to the next field.
1.1. when user returns to the field feedback becomes instant
1.2. however, if users tab from field to field without editing, no errors are shown
Out of the box, simple vue validator seems to support either 1 or 1.1 depending on if the lazy modifier is used. But switching between lazy and instant doesn't seem possible.
Some validation libraries provide flags for each validation binding e.g. Vee Validate has these flags:
from https://baianat.github.io/vee-validate/guide/flags.html:
touched: indicates that the field has been touched or focused.
untouched: indicates that the field has not been touched nor focused.
dirty: indicates that the field has been manipulated.
pristine: indicates that the field has not been manipulated.
valid: indicates that the field has passed the validation.
invalid: indicates that the field has failed the validation.
pending: indicates that the field validation is in progress.
validated: indicates that the field has been validated at least once by an event or manually using validate() or validateAll().
changed: indicates that the field value has been changed (strict check).
With these type of flags one can implement the above validation pattern like this :
// Only show error if the field hasbeen modified and visited (focus lost)
methods: canShowError(name) {
return validation.getField(name).flags.dirty &&
validation.getField(name).flags.visited &&
validation.getField(name).flags.invalid
}
<div class="message"v-if="canShowError('email')">
<div class="message">{{ validation.firstError('email') }}</div>
</div>
What do you think?
Kind regards
Bob