Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 3287610

Browse files
authored
Merge pull request #22 from alvarosaburido/feature/allow_valdiation_on_change
Input autovalidation on blur
2 parents 249fbc4 + 6a4640a commit 3287610

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

dev/App.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ const data = () => ({
150150
}),
151151
],
152152
options: {
153+
// autoValidate: true,
153154
customClass: 'row',
154155
netlify: true,
155156
},

src/components/dynamic-input/DynamicInput.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,25 @@ const watch = {
7171
};
7272

7373
const computed = {
74+
getClasses() {
75+
return [
76+
'dynamic-input',
77+
'form-group',
78+
{
79+
'form-group--error': this.showErrors,
80+
},
81+
`${this.formControl.customClass || ''}`,
82+
];
83+
},
7484
hasValue() {
7585
const { value } = this.formControl;
7686
return value !== null && value !== undefined;
7787
},
78-
hasErrors() {
88+
showErrors() {
7989
return (
8090
this.formControl.errors &&
8191
Object.keys(this.formControl.errors).length > 0 &&
82-
this.submited
92+
(this.submited || this.autoValidate)
8393
);
8494
},
8595
errorMessages() {
@@ -92,6 +102,11 @@ const computed = {
92102
submited() {
93103
return this.$parent.submited;
94104
},
105+
autoValidate() {
106+
return (
107+
this.$parent.options.autoValidate && this.formControl.touched === true
108+
);
109+
},
95110
};
96111

97112
const DynamicInput = {

src/components/dynamic-input/DynamicInput.vue

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
<template>
2-
<div
3-
class="dynamic-input form-group"
4-
:class="({ 'form-group--error': hasErrors }, `${formControl.customClass || ''}`)"
5-
>
2+
<div :class="getClasses">
63
<label
74
class="form-label"
85
:for="formControl.name"
@@ -47,7 +44,7 @@
4744
:onFocus="onFocus"
4845
:onBlur="onBlur"
4946
/>
50-
<div v-if="hasErrors">
47+
<div v-if="showErrors">
5148
<p
5249
v-for="(errorText, $index) in errorMessages"
5350
:key="`${$index}`"

src/core/utils/form-control.model.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ export function FormValidation(
6161
export function FormOptions({
6262
customClass = '',
6363
method = 'POST',
64+
autoValidate = false,
6465
netlify = false,
6566
netlifyHoneypot = null,
6667
}) {
6768
this.customClass = customClass;
6869
this.method = method;
70+
this.autoValidate = autoValidate;
6971
this.netlify = netlify;
7072
this.netlifyHoneypot = netlifyHoneypot;
7173
}

0 commit comments

Comments
 (0)