vuelidate-property-decorators
Version:
A thin wrapper of vuelidate library to simplify its usage with vue-class-component.
122 lines (82 loc) • 2.38 kB
Markdown
# vuelidate property decorators
This library provides a thin wrapper of
[vuelidate](https://vuelidate.netlify.com/)
library to simplify its usage with `vue-class-component`
or `vue-property-decorator`.
## Installation
```bash
yarn add vuelidate-property-decorators
```
## Usage
Set up `vuelidate` library as described in (https://vuelidate.netlify.com/#sub-installation).
Then on your component:
### Validating single field
To set per-field validation, use the `` decorator:
```javascript
import {Validate} from 'vuelidate-property-decorators';
import {required} from 'vuelidate/lib/validators'
export default class AddressForm extends Vue {
firstName = '';
lastName = '';
}
```
Template (pug in this case) looks the same way as in pure `vuelidate`:
```pug
.form-group
q-input(v-model="$v.firstName.$model")
.error(v-if="!$v.firstName.required") Field is required
.form-group
q-input(v-model="$v.lastName.$model")
.error(v-if="!$v.lastName.required") Field is required
```
### Setting validation for all fields at once
To set the validation for all fields at once, use `` decorator:
```javascript
import {Validations} from 'vuelidate-property-decorators';
import {required} from 'vuelidate/lib/validators'
export default class AddressForm extends Vue {
firstName = '';
lastName = '';
validations = {
firstName: {required},
lastName: {required}
}
}
```
## Dynamic validations
Both the argument of `` and the value of ``
can be a function that is called (reactively)
with `this` set to the component instance.
Example:
```javascript
import {Validate, Validations} from 'vuelidate-property-decorators';
import {required} from 'vuelidate/lib/validators'
export default class AddressForm extends Vue {
firstName = '';
lastName = '';
isRequired = false;
validations() {
if (this.isRequired) {
return {
firstName: {required},
lastName: {required}
}
}
return {}
}
test = '';
}
```