Skip to content

Commit

Permalink
Added validation rules.
Browse files Browse the repository at this point in the history
New validation ruless (equals, required_if and url) added.
  • Loading branch information
amostajo committed Sep 7, 2016
1 parent 51d5c2a commit 6733a96
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 13 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,15 +364,18 @@ List of available rules to use:

Rule | Params | Sample | Description
---------------- | ------------------------------------------------ | ---------------------- | -----------
`required` | | `required` | Validates that value is not empty.
`email` | | `email` | Validates that value has a valid email format.
`number` | | `number` | Validates that value is numeric.
`min` | 1) minimum string length | `min:2` | Validates that value's length is not lower than the minimum value set.
`min_number` | 1) minimum number | | `min_number:10` | Validates that value is not lower than the minimum value set.
`max` | 1) maximum string length | `max:10` | Validates that value's length is not bigger than the maximum value set.
`max_number` | 1) maximum number length | `max_number:15` | Validates that value is not bigger than the maximum value set.
`between` | 1) minimum string length 2) maximum string length | `between:5:10` | Validates that value's length is in between the number range set.
`between_number` | 1) minimum number 2) maximum number | `between_number:1:100` | Validates that value is in between the number range set.
`required` | | `required` | Validates if value is not empty.
`required_if` | 1) comparison field | `required_if:email` | Validates if value is not empty only if comparison field is not empty.
`email` | | `email` | Validates if value has a valid email format.
`number` | | `number` | Validates if value is numeric.
`min` | 1) minimum string length | `min:2` | Validates if value's length is not lower than the minimum value set.
`min_number` | 1) minimum number | `min_number:10` | Validates if value is not lower than the minimum value set.
`max` | 1) maximum string length | `max:10` | Validates if value's length is not bigger than the maximum value set.
`max_number` | 1) maximum number length | `max_number:15` | Validates if value is not bigger than the maximum value set.
`between` | 1) minimum string length 2) maximum string length | `between:5:10` | Validates if value's length is in between the number range set.
`between_number` | 1) minimum number 2) maximum number | `between_number:1:100` | Validates if value is in between the number range set.
`equals` | 1) comparison field | `equals:password` | Validates if value is the same as comparison field's value.
`url` | | `url` | Validates if value has a valid url format.

### Events

Expand Down
39 changes: 38 additions & 1 deletion dist/vue.form.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @author Alejandro Mostajo <http://about.me/amostajo>
* @copyright 10Quality <http://www.10quality.com>
* @license MIT
* @version 1.0.5
* @version 1.0.7
*/
Vue.component('vform', Vue.extend({
props:
Expand Down Expand Up @@ -84,6 +84,7 @@ Vue.component('vform', Vue.extend({
/**
* List of default errors.
* @since 1.0.2
* @since 1.0.7 Added equal,required_if,url
* @var object
*/
errors:
Expand All @@ -100,6 +101,9 @@ Vue.component('vform', Vue.extend({
max_number: 'Value must be no more than %1%.',
between: 'Value must have between %1% to %2% characters.',
between_number: 'Value must be between %1% to %2%.',
equals: 'Value must be equal to %1%.',
required_if: 'Required field.',
url: 'Url value is invalid.',
};
},
},
Expand Down Expand Up @@ -396,6 +400,7 @@ Vue.component('vform', Vue.extend({
/**
* Validates input.
* @since 1.0.2
* @since 1.0.7 Added equal,required_if,url
*
* @return bool
*/
Expand Down Expand Up @@ -499,6 +504,38 @@ Vue.component('vform', Vue.extend({
success = false;
}
break;
case 'equals':
if (options.length < 2)
throw 'Comparison field is not defined in validation rules';
if (this.$parent.request[this.listen] !== undefined
&& this.$parent.request[this.listen] !== this.$parent.request[options[1]]
) {
this.addError(options);
success = false;
}
break;
case 'required_if':
if (options.length < 2)
throw 'Comparison field is not defined in validation rules';
if (this.$parent.request[options[1]] !== undefined
&& this.$parent.request[options[1]].length > 0
&& (this.$parent.request[this.listen] === undefined
|| this.$parent.request[this.listen].length === 0
)
) {
this.addError(options);
success = false;
}
break;
case 'url':
var regex = /(https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})/;
if (this.$parent.request[this.listen] !== undefined
&& !regex.test(this.$parent.request[this.listen])
) {
this.addError(options);
success = false;
}
break;
}
}
return success;
Expand Down
Loading

0 comments on commit 6733a96

Please sign in to comment.