errors: {
"user": [ "The user field is required." ],
"toy.0": [ "Toy #1 must be a string." ]
},
I can access them by calling errors.user, errors["toy.0"]. I'm having trouble with errors["toy.0"] because it could be "toy.6", or "toy.15". I don't want it to be harcoded. Maybe a way to do the $errors->get('toy.*') command in Vue, like errors["toy.*"]?
<template>
<div v-if="errors.user" class="error">{{ errors.user }}</div>
<div v-for="errorMessage in dynamicToyErrors">
<div v-if="errorMessage" class="error">{{ errorMessage }}</div>
</div>
</template>
<script setup>
import { computed } from 'vue';
const errors = {
"user": "The user field is required.",
"toy.0": "Toy #1 must be a string.",
"toy.6": "Toy #7 must be a string.",
"toy.15": "Toy #16 must be a string."
};
const dynamicToyErrors = computed(() => {
const dynamicErrors = [];
for (const field in errors) {
if (field.includes(".")) {
const [prefix, index] = field.split(".");
dynamicErrors.push(errors[field]);
}
}
return dynamicErrors;
});
</script>