Fix wonky form validation

This commit is contained in:
richard-dds 2019-06-13 13:18:18 -04:00
parent 75d56b4bd9
commit a6c015753a
4 changed files with 30 additions and 12 deletions

View File

@ -18,12 +18,17 @@ export default {
default: false, default: false,
}, },
optional: Boolean, optional: Boolean,
nullOption: {
type: String,
default: ''
}
}, },
created: function() { created: function() {
emitEvent('field-mount', this, { emitEvent('field-mount', this, {
optional: this.optional, optional: this.optional,
name: this.name, name: this.name,
valid: this._isValid(this.value)
}) })
}, },
@ -41,13 +46,18 @@ export default {
onInput: function(e) { onInput: function(e) {
this.showError = false this.showError = false
this.showValid = true this.showValid = true
this.value = e.target.value
emitEvent('field-change', this, { emitEvent('field-change', this, {
value: e.target.value, value: e.target.value,
name: this.name, name: this.name,
watch: this.watch, watch: this.watch,
valid: this.showValid, valid: this._isValid(e.target.value)
}) })
}, },
_isValid: function(value) {
return this.optional || value !== this.nullOption
}
}, },
} }

View File

@ -70,6 +70,7 @@ export default {
emitEvent('field-mount', this, { emitEvent('field-mount', this, {
optional: this.optional, optional: this.optional,
name: this.name, name: this.name,
valid: this._isValid(this.value)
}) })
}, },
@ -103,15 +104,7 @@ export default {
// //
_checkIfValid: function({ value, invalidate = false }) { _checkIfValid: function({ value, invalidate = false }) {
// Validate the value const valid = this._isValid(value)
let valid = this._validate(value)
if (!this.modified && this.initialErrors && this.initialErrors.length) {
valid = false
} else if (this.optional && value === '') {
valid = true
}
if (this.modified) { if (this.modified) {
this.validationError = inputValidations[this.validation].validationError this.validationError = inputValidations[this.validation].validationError
} }
@ -144,5 +137,19 @@ export default {
_validate: function(value) { _validate: function(value) {
return inputValidations[this.validation].match.test(this._rawValue(value)) return inputValidations[this.validation].match.test(this._rawValue(value))
}, },
_isValid: function(value) {
let valid = this._validate(value)
if (!this.modified && this.initialErrors && this.initialErrors.length) {
valid = false
} else if (this.optional && value === '') {
valid = true
} else if (!this.optional && value === '') {
valid = false
}
return valid
}
}, },
} }

View File

@ -25,8 +25,8 @@ export default {
}, },
handleFieldMount: function(event) { handleFieldMount: function(event) {
const { name, optional } = event const { name, optional, valid } = event
this.fields[name] = optional this.fields[name] = optional || valid
}, },
validateForm: function() { validateForm: function() {

View File

@ -10,6 +10,7 @@
key='{{ field.name }}' key='{{ field.name }}'
v-bind:watch='{{ watch | string | lower }}' v-bind:watch='{{ watch | string | lower }}'
v-bind:optional={{ optional|lower }} v-bind:optional={{ optional|lower }}
v-bind:null-option="'{{ field.default }}'"
> >
<div <div
v-bind:class="['usa-input', { 'usa-input--error': showError, 'usa-input--success': showValid }]"> v-bind:class="['usa-input', { 'usa-input--error': showError, 'usa-input--success': showValid }]">