├── .gitignore ├── Gruntfile.js ├── LICENSE ├── README.md ├── bower.json ├── dist ├── vue.form.js └── vue.form.min.js ├── package.json ├── src └── vue.form.js └── tests ├── test1.html └── test2.html /.gitignore: -------------------------------------------------------------------------------- 1 | # Dependencies 2 | /bower_components 3 | /node_modules 4 | 5 | # Tmp files 6 | package-lock.json 7 | npm-debug.log -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Grunt tasks. 3 | * 4 | * @author Alejandro Mostajo 5 | * @copyright 10Quality 6 | * @license MIT 7 | * @version 1.0.0 8 | */ 9 | module.exports = function(grunt) 10 | { 11 | // Project configuration. 12 | grunt.initConfig({ 13 | pkg: grunt.file.readJSON('package.json'), 14 | uglify: { 15 | dist: { 16 | files: { 17 | 'dist/vue.form.min.js': [ 18 | 'src/vue.form.js' 19 | ], 20 | } 21 | }, 22 | }, 23 | copy: { 24 | dist: { 25 | files: [ 26 | { 27 | src: 'src/vue.form.js', 28 | dest: 'dist/vue.form.js' 29 | }, 30 | ], 31 | }, 32 | }, 33 | }); 34 | 35 | // Load 36 | grunt.loadNpmTasks('grunt-contrib-uglify'); 37 | grunt.loadNpmTasks('grunt-contrib-copy'); 38 | 39 | // Default task(s). 40 | grunt.registerTask('default', [ 41 | 'copy', 42 | 'uglify', 43 | ]); 44 | }; 45 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 10Quality 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 | # Form Component (for [Vue](http://vuejs.org/)) 2 | 3 | [![GitHub version](https://badge.fury.io/gh/10quality%2Fvue-form.svg)](https://badge.fury.io/gh/10quality%2Fvue-form) 4 | [![Bower version](https://badge.fury.io/bo/10q-vue-form.svg)](https://badge.fury.io/bo/10q-vue-form) 5 | 6 | Form handler component for [Vue](http://vuejs.org/) v2. 7 | 8 | **NOTE** Use [v1.0 branch](https://github.com/10quality/vue-form/tree/v1.0) to use this with Vue v1. 9 | 10 | View [DEMO](http://codepen.io/amostajo/pen/vKdPbj). 11 | 12 | ## Package index 13 | - [Installation](#installation) 14 | - [Usage](#usage) 15 | - [Props](#props) 16 | - [Request](#request) 17 | - [Response](#response) 18 | - [Redirection](#redirection) 19 | - [Results](#results) 20 | - [Props](#props-1) 21 | - [Input handling](#input-handling) 22 | - [Props](#props-2) 23 | - [Validations](#validations) 24 | - [Events](#events) 25 | - [License](#license) 26 | 27 | ## Installation 28 | 29 | Several installation options are available: 30 | 31 | - [Download the latest release](https://github.com/10quality/vue-form/releases). 32 | - Install with [Bower](http://bower.io): `bower install 10q-vue-form`. 33 | 34 | ## Usage 35 | 36 | Add the following resources for the component to function correctly. 37 | 38 | ```html 39 | 40 | 41 | 42 | 43 | ``` 44 | 45 | Add the component in your vue view. 46 | 47 | ```html 48 | 49 |
50 | 51 | 55 | 56 | 57 | 58 | 59 | 60 | Loading... 61 | 62 | 63 | 64 |
65 | ``` 66 | 67 | *NOTE:* `inline-template` must be present. 68 | 69 | ### Props 70 | 71 | List of available props to use in component: 72 | 73 | Prop | Data Type | Default | Description 74 | --------------- | ---------- | -------- | ----------- 75 | `action` | String | | Action URL where to send form data. 76 | `method` | String | POST | Request method (i.e. GET or POST). 77 | `headers` | Object | | Request headers ([reference](https://github.com/vuejs/vue-resource/blob/master/docs/http.md#options)). 78 | `timeout` | Number | | Request headers ([reference](https://github.com/vuejs/vue-resource/blob/master/docs/http.md#options)). 79 | `credentials` | Boolean | | Flag that indicates if request has credentials ([reference](https://github.com/vuejs/vue-resource/blob/master/docs/http.md#options)). 80 | `emulate-http` | Boolean | | Flag that indicates if request should emulate HTTP ([reference](https://github.com/vuejs/vue-resource/blob/master/docs/http.md#options)). 81 | `emulate-json` | Boolean | | Flag that indicates if request should emulate JSON ([reference](https://github.com/vuejs/vue-resource/blob/master/docs/http.md#options)). 82 | `errors` | Object | Object | List of default validation rules error messages. 83 | `id` | String | | Form given ID. 84 | `key` | String | | Form given loop key (i.e. in case of being used inside a v-for). 85 | `response-json` | Boolean | false | Forces response to be returned and parsed as JSON. 86 | `response-blob` | Boolean | false | Forces response to be returned as Blob. 87 | 88 | ### Request 89 | 90 | Data sent by form should be binded to the `request` data model. In other words, all the inputs within the form should be binded to request. 91 | 92 | As reference, a basic contact form sample: 93 | 94 | ```html 95 |
96 | 97 | 98 | 99 | 100 | 104 | 105 | 106 | 110 | 111 | 112 | 115 | 116 | 117 | 118 | 119 | 120 |
121 | ``` 122 | 123 | ### Response 124 | 125 | Any response obtained from a request is set to the `response` data model. 126 | 127 | If the following data is found, the form will *auto-process* the response (json) to facilitate its handling: 128 | 129 | ```json 130 | { 131 | "error": true, 132 | "message": "Contact information not sent." 133 | } 134 | ``` 135 | 136 | This response can be displayed in the template like: 137 | 138 | ```html 139 |
140 | 141 | 142 | 143 |
146 | {{response.message}} 147 |
148 | 149 |
150 | 151 |
152 | ``` 153 | 154 | Computed properties to use in template: 155 | 156 | Property | Data Type | Description 157 | -------------- | ---------- | ------------------- 158 | `isLoading` | Boolean | Flag that indicates if form is loading, processing or waiting for response. 159 | `hasMessage` | Boolean | Flag that indicates if form response returned a message to display. 160 | `hasError` | Boolean | Flag that indicates if form response returned as error. 161 | 162 | Another example using Bootstrap: 163 | 164 | ```html 165 |
166 | 167 | 168 | 169 |
173 | {{response.message}} 174 |
175 | 176 |
177 | 178 |
179 | ``` 180 | 181 | #### Redirection 182 | 183 | If the following data is found, the form will redirect the current window to the value set in `response.redirect`: 184 | 185 | ```json 186 | { 187 | "error": false, 188 | "message": "Information sent.", 189 | "redirect": "http://some.url.com" 190 | } 191 | ``` 192 | 193 | ### Results 194 | 195 | Form comes with a child component called `results`. This component will facilitate the handling of data returned by request (thought for searches). 196 | 197 | ```html 198 |
199 | 200 | 201 | 202 | 203 | 207 | 208 | 213 | 214 |
215 |
216 | {{record | json}} 217 |
218 |
219 | 220 |
221 | 222 |
223 | 224 |
225 | ``` 226 | 227 | In the example above, `results` child component is handling search results returned by the response (assuming `response` contains only results) and it is computing them into a property called `records`. 228 | 229 | *NOTE:* Results will compute records only if the model is an array. 230 | *NOTE:* `inline-template` must be present. 231 | 232 | #### Props 233 | 234 | List of available props to use in child component: 235 | 236 | Prop | Data Type | Default | Description 237 | ---------------- | ---------- | -------- | ----------- 238 | `model` | Array | | Data to compute for results (mostly required). 239 | `request` | Object | | If form request is needed to be binded to results. 240 | `fetch-onready` | Boolean | false | Flag that forces form to submit and return response when `document` is *ready*. 241 | `clear-on-fetch` | Boolean | true | Flag that indicates if records should stack on every submission or not. (used for eager loading) 242 | 243 | Another example: 244 | 245 | ```html 246 |
247 | 248 | 249 | 250 | 251 | 252 |
253 | {{record | json}} 254 |
255 | 256 |
257 | 258 |
259 | 260 |
261 | ``` 262 | 263 | ### Input handling 264 | 265 | Form comes with a second child component called `input-handler`. This component will facilitate the display of errors per input, improving UX. 266 | 267 | Example using Bootstrap: 268 | 269 | Response: 270 | 271 | ```json 272 | { 273 | "errors": [ 274 | "email": [ 275 | "Email is invalid.", 276 | "Fields is required." 277 | ], 278 | "name": [ 279 | "Too short". 280 | ] 281 | ] 282 | } 283 | ``` 284 | 285 | In template: 286 | 287 | ```html 288 |
289 | 290 | 291 | 292 | 297 | 298 | 303 | 304 | 305 | 310 | 311 | 316 | 317 | 318 | 319 | 320 |
321 | ``` 322 | 323 | In the example above, the response returned a list of errors per input. `input-handler` will process the response and if errors are found (response must be passed as `v-model`), it will add an error class to the input wrapper and will list the erros under the input using a `