Fix input validations

This commit is contained in:
Andrew Croce 2018-08-10 08:58:31 -04:00
parent 1b2e091a69
commit db1dd062ee
2 changed files with 29 additions and 22 deletions

View File

@ -14,27 +14,37 @@ export default {
type: String, type: String,
default: () => 'anything' default: () => 'anything'
}, },
setValue: { initialValue: {
type: String, type: String,
default: () => '' default: () => ''
} },
initialErrors: Array
}, },
data: function () { data: function () {
return { return {
showError: false, showError: (this.initialErrors && this.initialErrors.length) || false,
showValid: false, showValid: false,
mask: inputValidations[this.validation].mask, mask: inputValidations[this.validation].mask,
pipe: inputValidations[this.validation].pipe || undefined, pipe: inputValidations[this.validation].pipe || undefined,
keepCharPositions: inputValidations[this.validation].keepCharPositions || false, keepCharPositions: inputValidations[this.validation].keepCharPositions || false,
value: this.setValue value: this.initialValue
}
},
computed:{
rawValue: function () {
return this._rawValue(this.value)
} }
}, },
mounted: function () { mounted: function () {
if (this.value && this.mask) { if (this.value) {
this._checkIfValid({ value: this.value, invalidate: true }) this._checkIfValid({ value: this.value, invalidate: true })
this.value = conformToMask(this.value, this.mask).conformedValue
if (this.mask) {
this.value = conformToMask(this.value, this.mask).conformedValue
}
} }
}, },

View File

@ -4,24 +4,22 @@
<textinput <textinput
name='{{ field.name }}' name='{{ field.name }}'
validation='{{ validation }}' validation='{{ validation }}'
{% if field.data %}set-value='{{ field.data }}'{% endif %} {% if field.data %}initial-value='{{ field.data }}'{% endif %}
{% if field.errors %}v-bind:initial-errors='{{ field.errors }}'{% endif %}
inline-template> inline-template>
<div <div
v-bind:class="['usa-input {% if field.errors %}usa-input--error{% endif %} usa-input--validation--' + validation, { 'usa-input--error': showError, 'usa-input--success': showValid }]"> v-bind:class="['usa-input usa-input--validation--' + validation, { 'usa-input--error': showError, 'usa-input--success': showValid }]">
<label for={{field.name}}> <label for={{field.name}}>
{{ field.label }} {{ field.label | striptags }}
{% if field.description %} {% if field.description %}
<span class='usa-input__help'>{{ field.description | safe }}</span> <span class='usa-input__help'>{{ field.description | safe }}</span>
{% endif %} {% endif %}
{% if field.errors %} <span v-show='showError'>{{ Icon('alert') }}</span>
{{ Icon('alert') }} <span v-show='showValid'>{{ Icon('ok') }}</span>
{% endif %}
<template v-if='showError'>{{ Icon('alert') }}</template>
<template v-if='showValid'>{{ Icon('ok') }}</template>
</label> </label>
@ -32,7 +30,6 @@
v-on:change='onChange' v-on:change='onChange'
v-bind:value='value' v-bind:value='value'
id='{{ field.name }}' id='{{ field.name }}'
name='{{ field.name }}'
ref='input' ref='input'
placeholder='{{ placeholder }}'> placeholder='{{ placeholder }}'>
</textarea> </textarea>
@ -47,7 +44,6 @@
v-bind:pipe='pipe' v-bind:pipe='pipe'
v-bind:keep-char-positions='keepCharPositions' v-bind:keep-char-positions='keepCharPositions'
id='{{ field.name }}' id='{{ field.name }}'
name='{{ field.name }}'
type='text' type='text'
ref='input' ref='input'
placeholder='{{ placeholder }}' placeholder='{{ placeholder }}'
@ -56,11 +52,12 @@
{% endif %} {% endif %}
{% if field.errors %} <input type='hidden' v-bind:value='rawValue' name='{{ field.name }}' />
{% for error in field.errors %}
<span class='usa-input__message'>{{ error }}</span> <template v-if='showError'>
{% endfor %} <span v-for='error in initialErrors' class='usa-input__message' v-html='error'></span>
{% endif %} </template>
</div> </div>
</textinput> </textinput>
{%- endmacro %} {%- endmacro %}