Add event handlers for submit and invalid-submit events (VeeValidate)
This commit is contained in:
parent
a4da7fad51
commit
10f12c8f60
4 changed files with 33 additions and 4 deletions
|
|
@ -27,13 +27,14 @@ import { Form, defineRule } from "vee-validate";
|
||||||
import { required } from "@/helpers/validation-rules";
|
import { required } from "@/helpers/validation-rules";
|
||||||
import { errorMessages } from "@/constants/error-messages";
|
import { errorMessages } from "@/constants/error-messages";
|
||||||
import vehicleQuestionsMixin from "@/mixins/vehicle-questions-mixin";
|
import vehicleQuestionsMixin from "@/mixins/vehicle-questions-mixin";
|
||||||
|
import BaseFormMixin from '@/mixins/base-form-mixin.js';
|
||||||
|
|
||||||
// DEFINE VALIDATION RULES
|
// DEFINE VALIDATION RULES
|
||||||
defineRule("questions-required", required(errorMessages.OPTION_REQUIRED));
|
defineRule("questions-required", required(errorMessages.OPTION_REQUIRED));
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "part-questions",
|
name: "part-questions",
|
||||||
mixins: [vehicleQuestionsMixin],
|
mixins: [BaseFormMixin, vehicleQuestionsMixin],
|
||||||
async beforeRouteEnter(to, from, next) {
|
async beforeRouteEnter(to, from, next) {
|
||||||
// Call APIs
|
// Call APIs
|
||||||
const cmsContentPromise = fetchCmsContentForPage(to.query.issPage);
|
const cmsContentPromise = fetchCmsContentForPage(to.query.issPage);
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,7 @@ import replaceOptionsQuestion from "@/layouts/vehicle-damage/replace-options-que
|
||||||
import alert from "@/ux-components/alert/alert";
|
import alert from "@/ux-components/alert/alert";
|
||||||
|
|
||||||
// Supporting files
|
// Supporting files
|
||||||
|
import BaseFormMixin from '@/mixins/base-form-mixin.js';
|
||||||
import { fetchCmsContentForPage } from "@/helpers/cms-content-helper";
|
import { fetchCmsContentForPage } from "@/helpers/cms-content-helper";
|
||||||
import { settleAllPromises } from "@/helpers/layout-helper";
|
import { settleAllPromises } from "@/helpers/layout-helper";
|
||||||
import { Form, defineRule } from "vee-validate";
|
import { Form, defineRule } from "vee-validate";
|
||||||
|
|
@ -82,6 +83,7 @@ defineRule("replace-options-required", required(errorMessages.REPLACE_OPTIONS_RE
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "vehicle-damage",
|
name: "vehicle-damage",
|
||||||
|
mixins: [BaseFormMixin],
|
||||||
async beforeRouteEnter(to, from, next) {
|
async beforeRouteEnter(to, from, next) {
|
||||||
// Call APIs
|
// Call APIs
|
||||||
const cmsContentPromise = fetchCmsContentForPage(to.query.issPage);
|
const cmsContentPromise = fetchCmsContentForPage(to.query.issPage);
|
||||||
|
|
|
||||||
|
|
@ -57,10 +57,11 @@ import { settleAllPromises } from "@/helpers/layout-helper";
|
||||||
import { issPageValues } from "@/router/router-constants/issPage-values";
|
import { issPageValues } from "@/router/router-constants/issPage-values";
|
||||||
import { Form } from "vee-validate";
|
import { Form } from "vee-validate";
|
||||||
import vehicleQuestionsMixin from "@/mixins/vehicle-questions-mixin";
|
import vehicleQuestionsMixin from "@/mixins/vehicle-questions-mixin";
|
||||||
|
import BaseFormMixin from '@/mixins/base-form-mixin.js';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "vehicle-parts",
|
name: "vehicle-parts",
|
||||||
mixins: [vehicleQuestionsMixin],
|
mixins: [BaseFormMixin, vehicleQuestionsMixin],
|
||||||
async beforeRouteEnter(to, from, next) {
|
async beforeRouteEnter(to, from, next) {
|
||||||
// Call APIs
|
// Call APIs
|
||||||
const cmsContentPromise = fetchCmsContentForPage(to.query.issPage);
|
const cmsContentPromise = fetchCmsContentForPage(to.query.issPage);
|
||||||
|
|
|
||||||
25
src/mixins/base-form-mixin.js
Normal file
25
src/mixins/base-form-mixin.js
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
export default {
|
||||||
|
methods: {
|
||||||
|
onSubmit() {
|
||||||
|
/** DO NOT REMOVE;
|
||||||
|
* Needed to prevent default form submit behavior.
|
||||||
|
* Cannot use .prevent modifier for vee-validate Form.
|
||||||
|
*/
|
||||||
|
},
|
||||||
|
onInvalidSubmit({ errors }) {
|
||||||
|
/**
|
||||||
|
* Identify the first error field and put focus on it
|
||||||
|
* get error names array
|
||||||
|
*/
|
||||||
|
const errorNames = errors ? Object.keys(errors) : [];
|
||||||
|
const firstErrorEl = errorNames[0];
|
||||||
|
if (firstErrorEl) {
|
||||||
|
const qsString = `[data-focus-target="${firstErrorEl}"]`;
|
||||||
|
const el = document.querySelector(qsString);
|
||||||
|
if (el !== null) {
|
||||||
|
el.focus();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
Loading…
Reference in a new issue