├── ErrorComponent.vue ├── Errors.js ├── LICENSE.md ├── README.md ├── index.js └── package.json /ErrorComponent.vue: -------------------------------------------------------------------------------- 1 | 6 | -------------------------------------------------------------------------------- /Errors.js: -------------------------------------------------------------------------------- 1 | class Errors { 2 | 3 | constructor(){ 4 | this.errors = {}; 5 | } 6 | 7 | has(key){ 8 | return this.errors[key] !== undefined 9 | } 10 | 11 | first(key){ 12 | if(this.has(key)){ 13 | return this.errors[key][0] 14 | } 15 | } 16 | 17 | get(key){ 18 | if(this.has(key)){ 19 | return this.errors[key] 20 | } 21 | } 22 | 23 | all(){ 24 | return this.errors 25 | } 26 | 27 | fill(values){ 28 | this.errors = values 29 | } 30 | 31 | flush() { 32 | this.errors = {}; 33 | } 34 | 35 | } 36 | 37 | export default new Errors() -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016-2017 Val from Bubbleflat (@val-bubbleflat) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Vue Validator 2 | 3 | By [bubbleflat.com](https://bubbleflat.com) 4 | 5 | This package allow to display errors from laravel validation rules 6 | 7 | ! This package needs vue-resource to work ! 8 | 9 | # Installation 10 | 11 | ``` 12 | npm install --save laravel-vue-validator 13 | ``` 14 | 15 | ```javascript 16 | import LaravelVueValidator from 'laravel-vue-validator' 17 | 18 | Vue.use(LaravelVueValidator) 19 | ``` 20 | 21 | # Usage Example 22 | 23 | If you have in your laravel validation rule : 24 | 25 | `'name' => 'required|min:2|max:20'` 26 | 27 | You can display the error using in vue : 28 | 29 | `` 30 | 31 | This error will only be displayed if a 422 error is produced by laravel when the form is submited (when the rule is not satisfied) 32 | 33 | To flush errors in a vue component: 34 | 35 | `this.$errors.flush()` 36 | 37 | # Full Example 38 | 39 | ```html 40 | 49 | 67 | ``` -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import Errors from './Errors' 2 | import ErrorComponent from './ErrorComponent.vue' 3 | 4 | 5 | class Validator{ 6 | 7 | install(Vue){ 8 | 9 | Vue.component('error', ErrorComponent); 10 | 11 | if(Vue.http){ 12 | Vue.http.interceptors.push((request, next) => { 13 | next(response => { 14 | if(response.status === 422){ 15 | Errors.fill(response.body) 16 | } 17 | }); 18 | }); 19 | } 20 | 21 | if (axios) { 22 | axios.interceptors.response.use((response) => { 23 | return response; 24 | }, (error) => { 25 | if (error.response.status === 422) { 26 | Errors.fill(error.response.data.errors) 27 | } 28 | 29 | return Promise.reject(error); 30 | }); 31 | } 32 | 33 | Vue.mixin({ 34 | 35 | beforeCreate(){ 36 | //errors 37 | this.$options.$errors = {}; 38 | Vue.util.defineReactive(this.$options, '$errors', Errors); 39 | if(!this.$options.computed){ 40 | this.$options.computed = {} 41 | } 42 | this.$options.computed["$errors"] = function() { 43 | return this.$options.$errors; 44 | }; 45 | }, 46 | 47 | }) 48 | 49 | } 50 | 51 | } 52 | 53 | export default new Validator() -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "laravel-vue-validator", 3 | "version": "1.0.7", 4 | "description": " Simple package to display error in vue from laravel validation", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/val-bubbleflat/laravel-vue-validator.git" 12 | }, 13 | "keywords": [ 14 | "laravel", 15 | "vue", 16 | "validate", 17 | "validator", 18 | "validation" 19 | ], 20 | "author": "val bubbleflat (bubbleflat.com)", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/val-bubbleflat/laravel-vue-validator/issues" 24 | }, 25 | "homepage": "https://github.com/val-bubbleflat/laravel-vue-validator#readme" 26 | } 27 | --------------------------------------------------------------------------------