2023-12-09 15:47:03 +01:00
|
|
|
<template>
|
|
|
|
|
<transition name="fade">
|
2024-04-15 19:39:03 +02:00
|
|
|
<div
|
|
|
|
|
v-if="errorMessage"
|
2024-10-23 10:34:51 +02:00
|
|
|
class="has-error text-xs text-red-500 mt-1"
|
2024-04-15 19:39:03 +02:00
|
|
|
v-html="errorMessage"
|
2023-12-09 15:47:03 +01:00
|
|
|
/>
|
|
|
|
|
</transition>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
export default {
|
2024-10-23 10:34:51 +02:00
|
|
|
name: 'HasError',
|
2023-12-09 15:47:03 +01:00
|
|
|
props: {
|
|
|
|
|
form: {
|
|
|
|
|
type: Object,
|
2024-04-15 19:39:03 +02:00
|
|
|
required: true,
|
2023-12-09 15:47:03 +01:00
|
|
|
},
|
2024-10-23 10:34:51 +02:00
|
|
|
fieldId: {
|
2023-12-09 15:47:03 +01:00
|
|
|
type: String,
|
2024-04-15 19:39:03 +02:00
|
|
|
required: true,
|
|
|
|
|
},
|
2024-10-23 10:34:51 +02:00
|
|
|
fieldName: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: false,
|
|
|
|
|
},
|
2023-12-09 15:47:03 +01:00
|
|
|
},
|
|
|
|
|
computed: {
|
2024-04-15 19:39:03 +02:00
|
|
|
errorMessage() {
|
2024-10-23 10:34:51 +02:00
|
|
|
if (!this.form.errors || !this.form.errors.any())
|
2024-04-15 19:39:03 +02:00
|
|
|
return null
|
|
|
|
|
const subErrorsKeys = Object.keys(this.form.errors.all()).filter(
|
|
|
|
|
(key) => {
|
2024-10-23 10:34:51 +02:00
|
|
|
return key.startsWith(this.fieldId) && key !== this.fieldId
|
2024-04-15 19:39:03 +02:00
|
|
|
},
|
|
|
|
|
)
|
2024-10-23 10:34:51 +02:00
|
|
|
let baseError
|
|
|
|
|
= this.form.errors.get(this.fieldId)
|
|
|
|
|
?? (subErrorsKeys.length ? 'This field has some errors:' : null)
|
2023-12-09 15:47:03 +01:00
|
|
|
// If no error and no sub errors, return
|
2024-10-23 10:34:51 +02:00
|
|
|
if (!baseError)
|
|
|
|
|
return null
|
2023-12-09 15:47:03 +01:00
|
|
|
|
2024-10-23 10:34:51 +02:00
|
|
|
// Check if baseError starts with "The {field.name} field" and replace if necessary
|
|
|
|
|
if (baseError.startsWith(`The ${this.fieldName} field`)) {
|
|
|
|
|
baseError = baseError.replace(`The ${this.fieldName} field`, 'This field')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const coreError = `<p class='text-red-500'>${baseError}</p>`
|
|
|
|
|
if (subErrorsKeys.length) {
|
|
|
|
|
return coreError + `<ul class='list-disc list-inside'>${subErrorsKeys.map(
|
|
|
|
|
(key) => {
|
|
|
|
|
return `<li>${this.getSubError(key)}</li>`
|
|
|
|
|
},
|
|
|
|
|
)}</ul>`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return coreError
|
2024-04-15 19:39:03 +02:00
|
|
|
},
|
2023-12-09 15:47:03 +01:00
|
|
|
},
|
|
|
|
|
methods: {
|
2024-04-15 19:39:03 +02:00
|
|
|
getSubError(subErrorKey) {
|
2024-10-23 10:34:51 +02:00
|
|
|
return this.form.errors.get(subErrorKey).replace(subErrorKey, 'item')
|
2024-04-15 19:39:03 +02:00
|
|
|
},
|
|
|
|
|
},
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
</script>
|