You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.0 KiB
67 lines
2.0 KiB
<field-error>
|
|
<div class="field__error" if={ state.errors.length > 0 }>
|
|
<ul>
|
|
<li each={ error in state.errors }>
|
|
{ error }
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
<script>
|
|
|
|
/**
|
|
* Shows errors of Validation
|
|
*
|
|
* <field-error name="name" errors={ errors }></field-error>
|
|
*
|
|
*/
|
|
|
|
export default {
|
|
|
|
state: {
|
|
errors: [
|
|
|
|
],
|
|
},
|
|
|
|
onMounted(props, state) {
|
|
|
|
const parent = this.root.closest('.field')
|
|
const element = document.querySelector('[name="' + props.name + '"]')
|
|
const form = element.closest('form')
|
|
|
|
// element and form set
|
|
if (element && form) {
|
|
element.addEventListener('keyup', function(event) {
|
|
|
|
const fieldUpdateEvent = new CustomEvent('field-update', {
|
|
'detail': {
|
|
'name': props.name,
|
|
'value': this.value
|
|
}
|
|
})
|
|
|
|
form.dispatchEvent(fieldUpdateEvent)
|
|
})
|
|
}
|
|
|
|
// add custom event to listen to form-validation
|
|
this.root.addEventListener('form-validation', (event) => {
|
|
|
|
// if detail is set with element name, get errors
|
|
if (event.detail) {
|
|
this.state.errors = event.detail
|
|
parent.classList.add('field--error')
|
|
parent.classList.remove('field--valid')
|
|
} else {
|
|
this.state.errors = []
|
|
parent.classList.remove('field--error')
|
|
parent.classList.add('field--valid')
|
|
}
|
|
|
|
this.update()
|
|
})
|
|
}
|
|
}
|
|
|
|
</script>
|
|
</field-error> |