opnform-host-nginx/client/components/forms/validation/HasError.vue

53 lines
1.2 KiB
Vue
Raw Normal View History

2023-12-09 15:47:03 +01:00
<template>
<transition name="fade">
<div
v-if="errorMessage"
class="has-error text-sm text-red-500 -bottom-3"
v-html="errorMessage"
2023-12-09 15:47:03 +01:00
/>
</transition>
</template>
<script>
export default {
name: "HasError",
2023-12-09 15:47:03 +01:00
props: {
form: {
type: Object,
required: true,
2023-12-09 15:47:03 +01:00
},
field: {
type: String,
required: true,
},
2023-12-09 15:47:03 +01:00
},
computed: {
errorMessage() {
if (!this.form || !this.form.errors || !this.form.errors.any())
return null
const subErrorsKeys = Object.keys(this.form.errors.all()).filter(
(key) => {
return key.startsWith(this.field) && key !== this.field
},
)
const baseError =
this.form.errors.get(this.field) ??
(subErrorsKeys.length ? "This field has some errors:" : null)
2023-12-09 15:47:03 +01:00
// If no error and no sub errors, return
if (!baseError) return null
return `<p class="text-red-500">${baseError}</p><ul class="list-disc list-inside">${subErrorsKeys.map(
(key) => {
return "<li>" + this.getSubError(key) + "</li>"
},
)}</ul>`
},
2023-12-09 15:47:03 +01:00
},
methods: {
getSubError(subErrorKey) {
return this.form.errors.get(subErrorKey).replace(subErrorKey, "item")
},
},
2023-12-09 15:47:03 +01:00
}
</script>