Added 'eager' validation to textbox-question component

This commit is contained in:
Leah Schumann 2022-05-03 10:59:33 -04:00
parent 3a3bbf738a
commit b5aa014e85

View file

@ -1,8 +1,11 @@
<template>
<div class="textbox-question" :class="(errors && errors.length) || hasError ? 'has-error' : ''">
<label :for="inputId" :aria-label="questionText" class="form-label" v-html="labelText"></label>
<input v-model="value"
<input
v-on="validationListeners"
v-model="value"
v-maska="mask"
:type="type"
class="form-control"
:ref="inputId"
@ -14,9 +17,7 @@
:aria-required="isRequired"
autocomplete="off"
:class="[hasIcon ? 'has-icon' : '', iconRight ? 'icon-right' : '']"
:validationRules="validationRules"
@change="handleChange"
@blur="handleBlur" />
:validationRules="validationRules" />
<div v-show="errorMessage" class="row mt-2 form-test-error">
<span role="alert">{{ errorMessage }}</span>
</div>
@ -24,7 +25,7 @@
</template>
<script>
import { computed } from 'vue';
import { useField } from "vee-validate";
export default {
@ -57,6 +58,7 @@ export default {
const fieldOptions = {
type: "text",
value: props.modelValue,
validateOnValueUpdate: false,
};
const {
@ -68,6 +70,25 @@ export default {
errors,
} = useField(props.inputId, props.validationRules, fieldOptions);
const validationListeners = computed(() => {
// If the field is valid or have not been validated yet
// lazy
if (!errorMessage.value) {
return {
blur: handleChange,
change: handleChange,
// disable `shouldValidate` to avoid validating on input
input: e => handleChange(e, false),
};
}
// Aggressive
return {
blur: handleChange,
change: handleChange,
input: handleChange, // only switched this
};
});
return {
errorMessage,
handleBlur,
@ -75,6 +96,7 @@ export default {
validate,
meta,
errors,
validationListeners
};
},
computed: {
@ -110,6 +132,11 @@ export default {
}
}
},
watch: {
value(newValue) {
this.handleChange(newValue);
}
}
};
</script>