vue-laravel-error-validator
v2.0.0
Published
A Vue.JS plugin that adds a global function which can be used to fill and display errors from a laravel error response
Downloads
52
Maintainers
Readme
Vue Laravel Errors
This package allows to display errors from Laravel for Vue 3.
Installation
You can install the package via npm:
npm install --save vue-laravel-error-validatorSetup
import { createApp } from 'vue'
import LaravelErrorValidator from "vue-laravel-error-validator";
import App from './App.vue'
const app = createApp(App)
app.use(LaravelErrorValidator)
app.mount('#app')Usage
Options API (Vue 3)
Once the plugin is registered, you can access the error handler using $errors:
<template>
<div>
<input type="text" v-model="form.name">
<p class="error" v-if="$errors.has('name')">{{ $errors.first('name') }}</p>
<!-- Or use the included error component -->
<error field="name" />
</div>
</template>
<script>
export default {
data() {
return {
form: {
name: ''
}
}
},
methods: {
submitForm() {
axios.post('/api/endpoint', this.form)
.catch(error => {
// Validation errors are automatically captured by the axios interceptor
// if the status code is 422
});
}
}
}
</script>Composition API
You can also use the provided useErrors composable:
<template>
<div>
<input type="text" v-model="form.name">
<p class="error" v-if="errors.has('name')">{{ errors.first('name') }}</p>
</div>
</template>
<script setup>
import { ref } from 'vue';
import { useErrors } from 'vue-laravel-error-validator';
const errors = useErrors();
const form = ref({ name: '' });
function submitForm() {
axios.post('/api/endpoint', form.value);
}
</script>Available Methods
has(field): Check if a field has any errorsfirst(field): Get the first error message for a fieldget(field): Get all error messages for a fieldall(): Get all error messagesany(): Check if there are any errorsrecord(errors): Set the error objectclear(field): Clear a specific field's errors (or all if no field is specified)flush(): Clear all errorsonKeydown(event): Clear errors on keydown event
Automatic Error Handling
The plugin automatically intercepts 422 responses from Laravel backend and captures validation errors if Axios is available globally.
