# vuelidate
> Simple, lightweight model-based validation for Vue.js 2.x & 3.0
Visit [Vuelidate Docs](https://vuelidate-next.netlify.app) for detailed instructions.
## Sponsors
<p align="center">
<a href="https://getform.io/" target="_blank">
<img src="https://cdn.discordapp.com/attachments/1002927810710605875/1034915542596845728/getform.png" alt="Get Form" width="240px">
</a>
</p>
<p align="center">
<a href="https://www.storyblok.com/developers?utm_source=newsletter&utm_medium=logo&utm_campaign=vuejs-newsletter" target="_blank">
<img src="https://a.storyblok.com/f/51376/3856x824/fea44d52a9/colored-full.png" alt="Storyblok" width="240px">
</a>
</p>
<p align="center">
<a href="https://www.vuemastery.com/" target="_blank">
<img src="https://cdn.discordapp.com/attachments/258614093362102272/557267759130607630/Vue-Mastery-Big.png" alt="Vue Mastery logo" width="180px">
</a>
</p>
## Installation
You can use Vuelidate just by itself, but we suggest you use it along `@vuelidate/validators`, as it gives a nice collection of commonly used
validators.
**Vuelidate supports both Vue 3.0 and Vue 2.x**
```bash
npm install @vuelidate/core @vuelidate/validators
# or
yarn add @vuelidate/core @vuelidate/validators
```
## Usage with Options API
To use Vuelidate with the Options API, you just need to return an empty Vuelidate instance from `setup`.
Your validation state lives in the `data` and the rules are in `validations` function.
```js
import { email, required } from '@vuelidate/validators'
import { useVuelidate } from '@vuelidate/core'
export default {
name: 'UsersPage',
data: () => ({
form: {
name: '',
email: ''
}
}),
setup: () => ({ v$: useVuelidate() }),
validations () {
return {
form: {
name: { required },
email: { required, email }
}
}
}
}
```
## Usage with Composition API
To use Vuelidate with the Composition API, you need to provide it a state and set of validation rules, for that state.
The state can be a `reactive` object or a collection of `refs`.
```js
import { reactive } from 'vue' // or '@vue/composition-api' in Vue 2.x
import { useVuelidate } from '@vuelidate/core'
import { email, required } from '@vuelidate/validators'
export default {
setup () {
const state = reactive({
name: '',
emailAddress: ''
})
const rules = {
name: { required },
emailAddress: { required, email }
}
const v$ = useVuelidate(rules, state)
return { state, v$ }
}
}
```
## Providing global config to your Vuelidate instance
You can provide global configs to your Vuelidate instance using the third parameter of `useVuelidate` or by using the `validationsConfig`. These
config options are used to change some core Vuelidate functionality, like `$autoDirty`, `$lazy`, `$scope` and more. Learn all about them
in [Validation Configuration](https://vuelidate-next.netlify.app/api.html#validation-configuration).
### Config with Options API
```vue
<script>
import { useVuelidate } from '@vuelidate/core'
export default {
data () {
return { ...state }
},
validations () {
return { ...validations }
},
setup: () => ({ v$: useVuelidate() }),
validationConfig: {
$lazy: true,
}
}
</script>
```
### Config with Composition API
```js
import { reactive } from 'vue' // or '@vue/composition-api' in Vue 2.x
import { useVuelidate } from '@vuelidate/core'
import { email, required } from '@vuelidate/validators'
export default {
setup () {
const state = reactive({})
const rules = {}
const v$ = useVuelidate(rules, state, { $lazy: true })
return { state, v$ }
}
}
```
## The validation object, aka `v$` object
```ts
interface ValidationState {
$dirty: false, // validations will only run when $dirty is true
$touch: Function, // call to turn the $dirty state to true
$reset: Function, // call to turn the $dirty state to false
$errors: [], // contains all the current errors { $message, $params, $pending, $invalid }
$error: false, // true if validations have not passed
$invalid: false, // as above for compatibility reasons
// there are some other properties here, read the docs for more info
}
```
## Validations rules are on by default
Validation in Vuelidate 2 is by default on, meaning validators are called on initialisation, but an error is considered active, only after a field is dirty, so after `$touch()` is called or by using `$model`.
If you wish to make a validation lazy, meaning it only runs validations once it a field is dirty, you can pass a `{ $lazy: true }` property to
Vuelidate. This saves extra invocations for async validators as well as makes the initial validation setup a bit more performant.
```js
const v = useVuelidate(rules, state, { $lazy: true })
```
### Resetting dirty state
If you wish to reset a form's `$dirty` state, you can do so by using the appropriately named `$reset` method. For example when closing a create/edit
modal, you dont want the validation state to persist.
```vue
<app-modal @closed="v$.$reset()">
<!-- some inputs -->
</app-modal>
```
## Displaying error messages
The validation state holds useful data, like the invalid state of each property validator, along with extra properties, like an error message or extra
parameters.
Error messages come out of the box with the bundled validators in `@vuelidate/validators` package. You can check how change those them over at
the [Custom Validators page](https://vuelidate-next.netlify.app/custom_validators.html)
The easiest way to display errors is to use the form's top level `$errors` property. It is an array of validation objects, that you can iterate over.
```vue
<p
v-for="(error, index) of v$.$errors"
:key="index"
>
<strong>{{ error.$validator }}</strong>
<small> on property</small>
<strong>{{ error.$property }}</strong>
<small> says:</small>
<strong>{{ error.$message }}</strong>
</p>
```
You can also check for errors on each form property:
```vue
<p
v-for="(error, index) of v$.name.$errors"
:key="index"
>
<!-- Same as above -->
</p>
```
For more info, visit the [Vuelidate Docs](https://vuelidate-next.netlify.app).
## Development
To test the package run
``` bash
# install dependencies
yarn install
# create bundles.
yarn build
# Create docs inside /docs package
yarn dev
# run unit tests for entire monorepo
yarn test:unit
# You can also run for same command per package
```
## Contributors
### Current
- [Damian Dulisz](https://github.com/shentao)
- [Natalia Tepluhina](https://github.com/NataliaTepluhina)
- [Dobromir Hristov](https://github.com/dobromir-hristov)
### Emeriti
Here we honor past contributors who have been a major part on this project.
- [Monterail](https://github.com/monterail)
- [Paweł Grabarz](https://github.com/Frizi)
## License
[MIT](http://opensource.org/licenses/MIT)
没有合适的资源?快使用搜索试试~ 我知道了~
简单、轻量级的基于模型的 Vue.js 验证.zip
共281个文件
js:121个
vue:33个
md:31个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 194 浏览量
2024-12-01
20:15:13
上传
评论
收藏 590KB ZIP 举报
温馨提示
维利达针对 Vue.js 2.x 和 3.0 的简单、轻量级的基于模型的验证访问Vuelidate Docs获取详细说明。赞助商安装您可以单独使用 Vuelidate,但我们建议您与一起使用@vuelidate/validators,因为它提供了常用验证器的集合。Vuelidate 同时支持 Vue 3.0 和 Vue 2.xnpm install @vuelidate/core @vuelidate/validators# oryarn add @vuelidate/core @vuelidate/validators与选项 API 结合使用要将 Vuelidate 与 Options API 一起使用,您只需从 返回一个空的 Vuelidate 实例setup。您的验证状态存在于data并且规则正在运行validations。import { email, required } from '@vuelidate/validators'import { useVuelidate } from '@vuelidate/core'exp
资源推荐
资源详情
资源评论
收起资源包目录
简单、轻量级的基于模型的 Vue.js 验证.zip (281个子文件)
.browserslistrc 95B
.browserslistrc 94B
.browserslistrc 41B
.editorconfig 147B
.eslintignore 27B
.gitignore 214B
.gitignore 118B
index-iife.html 2KB
index.html 390B
favicon.ico 15KB
favicon.ico 4KB
validation.spec.js 81KB
optionsApi.spec.js 20KB
core.js 18KB
prism.js 17KB
forEach.spec.js 8KB
createResults.js 7KB
createI18nMessage.spec.js 6KB
and.spec.js 5KB
index.js 4KB
or.spec.js 4KB
macAddress.spec.js 4KB
utils.js 3KB
forEach.js 3KB
validations.fixture.js 3KB
swap-vue.js 3KB
injectNestedComponentValidations.js 3KB
url.spec.js 3KB
between.spec.js 2KB
not.spec.js 2KB
email.spec.js 2KB
main.js 2KB
config.js 2KB
storage.js 2KB
withParams.spec.js 2KB
index.js 2KB
decimal.spec.js 2KB
ipAddress.spec.js 2KB
maxLength.spec.js 2KB
common.spec.js 2KB
requiredIf.spec.js 2KB
withMessage.spec.js 1KB
required.spec.js 1KB
integer.spec.js 1KB
common.js 1KB
requiredUnless.spec.js 1KB
numeric.spec.js 1KB
createI18nMessage.js 1KB
and.js 1KB
sortValidations.js 1KB
minLength.spec.js 1KB
core.spec.js 1KB
or.js 1KB
routes.js 1KB
core.js 1KB
fixtures.js 1KB
index.js 1KB
alpha.spec.js 1000B
withAsync.spec.js 996B
maxValue.spec.js 954B
index.js 954B
minValue.spec.js 950B
withMessage.js 913B
alphaNum.spec.js 895B
macAddress.js 832B
index.js 827B
withParams.js 785B
index.js 746B
i18n.js 703B
requiredUnless.js 679B
requiredIf.js 674B
ipAddress.js 656B
.eslintrc.js 624B
withAsync.js 618B
url.js 585B
not.js 576B
ValidateEach.js 544B
email.js 523B
ComputedProxyFactory.js 450B
between.js 432B
or.js 427B
between.js 423B
validators.fixture.js 422B
sameAs.js 410B
requiredUnless.js 400B
common.js 393B
minValue.js 381B
requiredIf.js 380B
and.js 380B
minLength.js 377B
babel.config.js 376B
minValue.js 367B
maxValue.js 357B
maxLength.js 346B
not.js 345B
maxValue.js 342B
maxLength.js 329B
macAddress.js 322B
minLength.js 316B
test-utils.js 310B
共 281 条
- 1
- 2
- 3
资源评论
徐浪老师
- 粉丝: 8552
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- MATLAB【面板】垃圾识别定位.zip
- MATLAB【面板】疲劳专注度检测系统.zip
- MATLAB【面板】疲劳检测GUI设计.zip
- MATLAB【面板】苹果水果分级.zip
- MATLAB【面板】脐橙水果分级.zip
- MATLAB【面板】漂浮物识别.zip
- MATLAB【面板】人脸门禁系统.zip
- MATLAB【面板】人脸考勤设计.zip
- MATLAB【面板】人脸购物系统.zip
- MATLAB【面板】人脸识别.zip
- MATLAB【面板】人脸识别系统.zip
- MATLAB【面板】手势控制系统.zip
- MATLAB【面板】手势识别设计.zip
- MATLAB【面板】手势识别.zip
- MATLAB【面板】手势识别系统.zip
- 云计算平台下Docker容器化技术介绍及其企业应用场景
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功