1.3.1 • Published 5 years ago

laravel-veevalidate v1.3.1

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

🚨 Laravel VeeValidate

The super simple JS package that will handle your Laravel errors and automatically inject them into a VeeValidate instance ✨

Example Image

Installation

npm i laravel-veevalidate

or

yarn add laravel-veevalidate

Usage

Assuming you already have VeeValidate installed, you can use Laravel VeeValidate as a Vue Prototype.

import LaravelValidator from 'laravel-veevalidate';

Vue.prototype.$laravelValidator = LaravelValidator;

Alternatively, you can use it on a on a component-to-component basis.

import LaravelValidator from 'laravel-veevalidate';

Using it from with Axios

To use the error handler with Axios, you will need to place the handleError method inside of the catch() block of the Axios promise.

The first parameter you must provide is the VeeValidate instance you would like to use. It's recommended to use the global directive ($validator). The second parameter is the response callback which Axios provides.

axios(...)
    .catch(error_response => {
       this.$laravelValidator.handleError(this.$validator, error_response) 
    })

Using it with Fetch

To use the error handler with Fetch, you will need to place the handleFetchError method inside of the then() block of the Fetch promise.

The first parameter you must provide is the VeeValidate instance you would like to use. It's recommended to use the global directive ($validator). The second parameter is the response callback which Fetch provides.

fetch(...)
    .then(response => {
       this.$laravelValidator.handleFetchError(this.$validator, response) 
    })

Clearing Errors

Important: Laravel VeeValidate does not clear your VeeValidate errors. Before your request you should clear the error bag from your VeeValidate Instance:

// e.g: 
this.$validator.errors.clear();

Custom field mapping

Sometimes your Request/Eloquent attributes won't match your VeeValidate fields/names. You can really easily "map" this by passing a key value object as a third parameter.

    // Axios
    this.$laravelValidator.handleError(this.$validator, error_response, {
           'email_address': 'email address', // Attribute => Field Name
       }) 
    })
    
    // Fetch
    this.$laravelValidator.handleFetchError(this.$validator, response, {
           'email_address': 'email address', // Attribute => Field Name
       }) 
    })

Options

Laravel VeeValidate provides some simple options which you can pass as the fourth parameter of the handleError and handleFetchError methods.

OptionDescriptionTypeDefault ValueChoices
driverRequest driver you are using.Stringaxiosaxios fetch
show_errorsDisplay console errors while in Development modeBooleantruetrue false

Applying an option

this.$laravelValidator.handleError(this.$validator, error_response, {}, {
    show_errors: false
}) 

And that's it! ✨

This is my first JS/NPM package, I really hope it's been useful to you, if you like my work and want to show some love, consider buying me some coding fuel (Coffee) ❤

https://ko-fi.com/sammyjo20

1.3.1

5 years ago

1.3.0

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.22-g

5 years ago

1.0.21

5 years ago

1.0.21-g

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago