├── LICENSE ├── README.md ├── package.json └── src ├── index.js └── vueLaravelErrors.js /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Sohaib Sherif 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Installation 2 | 3 | 4 | ``` 5 | npm i vue-laravel-errors 6 | ``` 7 | 8 | ## Usage 9 | 10 | # As a plugin (globally) 11 | 12 | ```js 13 | import vue from "vue" 14 | import vueLaravelErrors from "vue-laravel-errors" 15 | 16 | Vue.use(vueLaravelErrors, { type : "object" || "array"})//if options are omitted returns default laravel error object 17 | ``` 18 | 19 | # As a mixin (can be used this way locally in a component) 20 | 21 | ```js 22 | import {vueLaravelErrors} from 'vue-laravel-errors/src/vueLaravelErrors' 23 | 24 | export default { 25 | mixins: [vueLaravelErrors("object" || "array")],//can be called without passing an argument to get default laravel error object 26 | name: "YourAwesomeComponent", 27 | ... 28 | } 29 | ``` 30 | 31 | if object is used the errors variable will be an object with the laravel validation as key and the message as value 32 | ```js 33 | { laravel_validation_key: laravel_validation_message } 34 | ``` 35 | if array is used the errors variable will be an array with all laravel validation messages 36 | ```js 37 | [laravel_validation_message, laravel_validation_message, laravel_validation_message] 38 | ``` 39 | 40 | # Usage in Component 41 | 42 | Consider we have a normal post request that sends some form data to be validated through an ajax request 43 | ```js 44 | axios.post('endpoint') 45 | .then(response) 46 | .catch( error => { 47 | this.fillErrors(error.response.data.errors) 48 | }) 49 | ``` 50 | we can then just use the fillErrors function and pass it the errors we got back from our backend. 51 | 52 | afterwards in the template we can access the errors variable as follows 53 | 54 | ## - if errors is an object 55 | 56 | ```vue 57 | 58 | 63 | ``` 64 | 65 | ## - if errors is an array we can loop 66 | ```vue 67 | 68 | 71 | ``` 72 | 73 | 74 | And that is it, as simple as that yes! 75 | 76 | 77 | ## Contribution 78 | 79 | All contributions are welcome just open a pull request and explain what you did :D 80 | 81 | ## License 82 | 83 | MIT License 84 | 85 | Copyright (c) 2020 Sohaib Sherif 86 | 87 | Permission is hereby granted, free of charge, to any person obtaining a copy 88 | of this software and associated documentation files (the "Software"), to deal 89 | in the Software without restriction, including without limitation the rights 90 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 91 | copies of the Software, and to permit persons to whom the Software is 92 | furnished to do so, subject to the following conditions: 93 | 94 | The above copyright notice and this permission notice shall be included in all 95 | copies or substantial portions of the Software. 96 | 97 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 98 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 99 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 100 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 101 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 102 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 103 | SOFTWARE. 104 | 105 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vue-laravel-errors", 3 | "version": "0.2.5", 4 | "description": "A Vue.JS plugin that adds a global function which can be used to fill an errors object from a laravel error response", 5 | "main": "src/index.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/Sohaib-Sherif/vue-laravel-errors.git" 9 | }, 10 | "keywords": [ 11 | "Vue", 12 | "laravel", 13 | "validation", 14 | "JS", 15 | "js", 16 | "Laravel", 17 | "Errors", 18 | "vue", 19 | "vuejs", 20 | "vue-mixin", 21 | "mixin" 22 | ], 23 | "author": "Sohaib Sherif", 24 | "license": "MIT", 25 | "bugs": { 26 | "url": "https://github.com/Sohaib-Sherif/vue-laravel-errors/issues" 27 | }, 28 | "homepage": "https://github.com/Sohaib-Sherif/vue-laravel-errors#readme" 29 | } 30 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import {vueLaravelErrors} from './vueLaravelErrors' 2 | 3 | export default { 4 | install(Vue, options) { 5 | Vue.mixin(vueLaravelErrors(options.type)) 6 | 7 | } 8 | }; -------------------------------------------------------------------------------- /src/vueLaravelErrors.js: -------------------------------------------------------------------------------- 1 | export const vueLaravelErrors = (type) => { 2 | return { 3 | data() { 4 | return { 5 | errors: {}, 6 | } 7 | }, 8 | methods: { 9 | fillErrors(errors) { 10 | if (type === 'array') { 11 | errors = Object.entries(errors); 12 | errors = errors.flatMap(error => { 13 | return [...error[1]]; 14 | }) 15 | } else if (type === 'object') { 16 | errors = Object.entries(errors); 17 | errors = errors.map(error => { 18 | let key = error[0]; 19 | let value = error[1][0]; 20 | return { 21 | [key]: value 22 | }; 23 | }) 24 | .reduce((acc, current) => { 25 | acc = Object.assign(acc, current); 26 | return acc; 27 | }); 28 | } 29 | this.errors = errors; 30 | } 31 | }, 32 | } 33 | } --------------------------------------------------------------------------------