|
| 1 | +# vue-blob-forms |
| 2 | + |
| 3 | +`vue-blob-forms` is a simple frontend web form validation plugin for [Vue JS](https://vuejs.org/). It relies on the native HTML5 [Constraint API](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation), and is compatible with any custom constraints (`v-bind:` or otherwise) your code might contain. |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +## Table of Contents |
| 8 | + |
| 9 | + * [Installation](#installation) |
| 10 | + * [Form and Field Validation](#form-and-field-validation) |
| 11 | + * [Phone Number Formatting](#phone-number-formatting) |
| 12 | + * [Gravatar](#gravatar) |
| 13 | + * [License](#license) |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +`vue-blob-forms` requires [Vue 2+](https://github.com/vuejs/vue). |
| 20 | + |
| 21 | +Advanced phone number validation and formatting (if so desired) requires [blob-phone JS](https://github.com/Blobfolio/blob-phone#javascript). |
| 22 | + |
| 23 | +To use it, simply include it in your project after the dependencies: |
| 24 | + |
| 25 | +```html |
| 26 | +<script src="path/to/vue.js"></script> |
| 27 | +<script src="dist/vue-blob-forms.min.js"></script> |
| 28 | +``` |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +## Form and Field Validation |
| 33 | + |
| 34 | +### Directive: v-form |
| 35 | + |
| 36 | +This directive provides the bulk of `vue-blob-forms`'s goodies. Simply add `v-form` to any HTML `<form>` element to enable validation and all the related methods. |
| 37 | + |
| 38 | +Name attributes are required for all forms and form fields. Any element without a name will be ignored by the validation process. |
| 39 | + |
| 40 | +#### Example |
| 41 | + |
| 42 | +```html |
| 43 | +<form name="myForm" v-form> |
| 44 | + ... |
| 45 | +</form> |
| 46 | +``` |
| 47 | + |
| 48 | +### Method: formErrors() |
| 49 | + |
| 50 | +This method can be used to retrieve all errors for a given form, or an error for a single form field. |
| 51 | + |
| 52 | +#### Arguments |
| 53 | + |
| 54 | +| Type | Name | Description | |
| 55 | +| ---- | ---- | ----------- | |
| 56 | +| *string* | formName | The name of the form. | |
| 57 | +| *string* *optional* | fieldName | The name of the field. | |
| 58 | + |
| 59 | +#### Returns |
| 60 | + |
| 61 | +When only `formName` is passed, the method will return all form errors, or `FALSE` if there are none. Errors are returned as an object, each key representing `fieldName` and each value an error string. |
| 62 | + |
| 63 | +When `fieldName` is passed, the method will return the error corresponding to the field, or `FALSE` if there is no error. |
| 64 | + |
| 65 | +#### Example |
| 66 | + |
| 67 | +```html |
| 68 | +<div v-if="formErrors('myForm', 'myField')" class="error"> |
| 69 | + {{ formErrors('myForm', 'myField') }} |
| 70 | +</div> |
| 71 | +``` |
| 72 | + |
| 73 | +### Method: formTouched() |
| 74 | + |
| 75 | +This method can be used to determine whether or not a form or a specific field has been "touched". |
| 76 | + |
| 77 | +For the purposes of this plugin, a "touch" occurs when a field receives input or when the field's `blur` event triggers. |
| 78 | + |
| 79 | +#### Arguments |
| 80 | + |
| 81 | +| Type | Name | Description | |
| 82 | +| ---- | ---- | ----------- | |
| 83 | +| *string* | formName | The name of the form. | |
| 84 | +| *string* *optional* | fieldName | The name of the field. | |
| 85 | + |
| 86 | +#### Returns |
| 87 | + |
| 88 | +When only `formName` is passed, the method returns `TRUE` if any of its fields have been touched, otherwise `FALSE`. |
| 89 | + |
| 90 | +When `fieldName` is passed, `TRUE` or `FALSE` are returned based on the specific state of the field. |
| 91 | + |
| 92 | +#### Example |
| 93 | + |
| 94 | +```html |
| 95 | +<input type="text" v-bind:class="{ touched: formTouched('myForm', 'myField') }" name="myField" /> |
| 96 | +``` |
| 97 | + |
| 98 | +### Method: formChanged() |
| 99 | + |
| 100 | +This method works just like [formTouched()](#method-formtouched), but says whether or not field values have changed since the form was first initialized. |
| 101 | + |
| 102 | +### Method: formValid() |
| 103 | + |
| 104 | +This method works just like [formTouched()](#method-formtouched), but says whether or not field values are valid. |
| 105 | + |
| 106 | +Note: during automatic validation, if a field is required _and_ empty, it will only trigger an invalid state _if_ it has been touched. Because of this, it is recommended you call the [validateForm()](#method-validateform) method prior to submission, which will force-touch all fields and rerun validation accordingly. |
| 107 | + |
| 108 | +### Method: validateForm() |
| 109 | + |
| 110 | +This method force-touches all form fields, reruns validation, and returns `TRUE` if everything is happy, or `FALSE` if one or more fields are sad. |
| 111 | + |
| 112 | +#### Arguments |
| 113 | + |
| 114 | +| Type | Name | Description | |
| 115 | +| ---- | ---- | ----------- | |
| 116 | +| *string* | formName | The name of the form. | |
| 117 | + |
| 118 | +#### Returns |
| 119 | + |
| 120 | +`TRUE` if all form fields are valid, otherwise `FALSE`. |
| 121 | + |
| 122 | +Note: `FALSE` is also returned if the form name is invalid or not bound to the `v-form` directive. In other words, this should only be used for `v-form` forms. |
| 123 | + |
| 124 | +#### Example |
| 125 | + |
| 126 | +```html |
| 127 | +<form v-form name="myForm" v-on:submit.prevent="submitForm"> |
| 128 | + ... |
| 129 | +</form> |
| 130 | +``` |
| 131 | + |
| 132 | +```js |
| 133 | +submitForm = function() { |
| 134 | + if (!this.validateForm('myForm')) { |
| 135 | + return false; |
| 136 | + } |
| 137 | + |
| 138 | + // Submit the form... |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +### Custom Validation Callbacks |
| 143 | + |
| 144 | +Aside from the standard [Constraints](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation) like `required` and `minlength` and whatnot, you can also specify any arbitrary Vue callback function by adding `data-validation-callback="myVueMethod"` to a field. |
| 145 | + |
| 146 | +The callback function must be within the Vue scope and should accept a single argument representing the field value. |
| 147 | + |
| 148 | +If the value is valid, the function should return `TRUE`, otherwise it should return an error string. |
| 149 | + |
| 150 | +#### Example |
| 151 | + |
| 152 | +```html |
| 153 | +<input type="text" data-custom-callback="validateUsername" /> |
| 154 | +``` |
| 155 | + |
| 156 | +```js |
| 157 | +validateUsername = function(value) { |
| 158 | + if (this.users.indexOf(value) !== -1) { |
| 159 | + return true; |
| 160 | + } |
| 161 | + |
| 162 | + return 'Please enter a valid user name.'; |
| 163 | +} |
| 164 | +``` |
| 165 | + |
| 166 | + |
| 167 | +### Form Classes |
| 168 | + |
| 169 | +All forms with the `v-form` directive receive the following classes to aid with state-based styling or DOM queries: |
| 170 | + |
| 171 | +| Class | Description | |
| 172 | +| ----- | ----------- | |
| 173 | +| is-valid | All fields are valid*. | |
| 174 | +| is-invalid | One or more fields are invalid. | |
| 175 | +| is-touched | One or more fields have been touched. | |
| 176 | +| is-changed | One or more fields have different values from the ones at load time. | |
| 177 | + |
| 178 | +Note: during automatic validation, if a field is required _and_ empty, it will only trigger an invalid state _if_ it has been touched. Because of this, it is recommended you call the [validateForm()](#method-validateform) method prior to submission, which will force-touch all fields and rerun validation accordingly. |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | +## Phone Number Formatting |
| 183 | + |
| 184 | +`vue-blob-forms` comes with wrappers for [blob-phone JS](https://github.com/Blobfolio/blob-phone#javascript). To reduce overhead, `blob-phone` itself is not included; if missing, these directives and methods will have no effect. |
| 185 | + |
| 186 | +### Directive: v-phone |
| 187 | + |
| 188 | +This directive should be applied to an `<input type="tel" />` field. Phone numbers entered will then be validated according to the country's formatting rules, and rewritten into international standard (e.g. from `(123) 456-7890` to `+1 123-456-7890`). |
| 189 | + |
| 190 | +The directive optionally takes an ISO country code as an argument, which serves as the default country for validation/formatting purposes. Because a single, unformatted number might match multiple regions around the globe, it is highly recommended an appropriate country code be passed. If blank or invalid, `US` is assumed. |
| 191 | + |
| 192 | +If a phone number is valid, the model will be set to the formatted value. The country code will also be appended to the `DOMElement`'s `data-country` attribute, in case you wanted to style the input with a flag or something. |
| 193 | + |
| 194 | +#### Example |
| 195 | + |
| 196 | +```html |
| 197 | +<input type="tel" name="myPhone" v-phone="CA" /> |
| 198 | +``` |
| 199 | + |
| 200 | +### Filter: phone |
| 201 | + |
| 202 | +Format an arbitrary phone-like string to an international standard. |
| 203 | + |
| 204 | +Note: this can be used independently of any form or field elements. |
| 205 | + |
| 206 | +#### Arguments |
| 207 | + |
| 208 | +| Type | Name | Description | Default | |
| 209 | +| ---- | ---- | ----------- | ------- | |
| 210 | +| *string* | phone | Phone number. | | |
| 211 | +| *string* *optional* | country | A country code. | `US` | |
| 212 | + |
| 213 | +#### Returns |
| 214 | + |
| 215 | +If the phone number is valid and parseable, it will be returned in internationalized format. Otherwise the original value is returned. |
| 216 | + |
| 217 | +#### Example |
| 218 | + |
| 219 | +```html |
| 220 | +<div class="phone"> |
| 221 | + Phone Number: {{ myPhone | phone('GB') }} |
| 222 | +</div> |
| 223 | +``` |
| 224 | + |
| 225 | + |
| 226 | + |
| 227 | +## Gravatar |
| 228 | + |
| 229 | +`vue-blob-forms` comes with a couple [Gravatar](https://en.gravatar.com/) helpers in case you wanted to personalize email entries. |
| 230 | + |
| 231 | +### Directive: v-gravatar |
| 232 | + |
| 233 | +This directive will automatically apply an email address' corresponding Gravatar icon as a background image on a `<input type="email" />` field. |
| 234 | + |
| 235 | +The directive optionally takes a single argument representing the icon size to return. The default is `80` (pixels). |
| 236 | + |
| 237 | +If an address is invalid or has no Gravatar image, the result will be blank (a transparent PNG). |
| 238 | + |
| 239 | +#### Example |
| 240 | + |
| 241 | +```html |
| 242 | +<input type="email" v-gravatar="40" /> |
| 243 | +``` |
| 244 | + |
| 245 | +### Method: gravatarURL() |
| 246 | + |
| 247 | +Find a Gravatar image URL for a given email address. |
| 248 | + |
| 249 | +#### Arguments |
| 250 | + |
| 251 | +| Type | Name | Description | Default | |
| 252 | +| ---- | ---- | ----------- | ------- | |
| 253 | +| *string* | email | Email address. | | |
| 254 | +| *string* *optional* | Size | The pixel size to return. | `80` | |
| 255 | + |
| 256 | +#### Returns |
| 257 | + |
| 258 | +A URL from Gravatar is always returned. If the email address is invalid or not part of Gravatar, the URL will resolve to a transparent PNG. |
| 259 | + |
| 260 | +#### Example |
| 261 | + |
| 262 | +```html |
| 263 | +<div class="email"> |
| 264 | + Email Address: {{ myEmail }} |
| 265 | + <img v-bind:src="{{ gravatarURL(myEmail) }}" alt="User Icon" /> |
| 266 | +</div> |
| 267 | +``` |
| 268 | + |
| 269 | + |
| 270 | + |
| 271 | +## License |
| 272 | + |
| 273 | +Copyright © 2017 [Blobfolio, LLC ](https://blobfolio.com) <[email protected]> |
| 274 | + |
| 275 | +This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2. |
| 276 | + |
| 277 | + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE |
| 278 | + Version 2, December 2004 |
| 279 | + |
| 280 | + Copyright (C) 2004 Sam Hocevar <[email protected]> |
| 281 | + |
| 282 | + Everyone is permitted to copy and distribute verbatim or modified |
| 283 | + copies of this license document, and changing it is allowed as long |
| 284 | + as the name is changed. |
| 285 | + |
| 286 | + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE |
| 287 | + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION |
| 288 | + |
| 289 | + 0. You just DO WHAT THE FUCK YOU WANT TO. |
0 commit comments