mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
feat(Form): rule validator add rule param (#5704)
This commit is contained in:
parent
529e117096
commit
1b3bcf27ee
@ -147,9 +147,9 @@ export default createComponent({
|
||||
}
|
||||
},
|
||||
|
||||
runValidator(validator) {
|
||||
runValidator(rule) {
|
||||
return new Promise(resolve => {
|
||||
const returnVal = validator(this.formValue);
|
||||
const returnVal = rule.validator(this.formValue, rule);
|
||||
|
||||
if (isPromise(returnVal)) {
|
||||
return returnVal.then(resolve);
|
||||
@ -183,7 +183,7 @@ export default createComponent({
|
||||
}
|
||||
|
||||
if (rule.validator) {
|
||||
return this.runValidator(rule.validator).then(result => {
|
||||
return this.runValidator(rule).then(result => {
|
||||
if (result === false) {
|
||||
this.validateMessage = rule.message;
|
||||
}
|
||||
|
@ -439,7 +439,7 @@ export default {
|
||||
|------|------|------|
|
||||
| message | Error message | *string* |
|
||||
| required | Whether to be a required field | *boolean* |
|
||||
| validator | Custom validator | *() => boolean \| Promise* |
|
||||
| validator | Custom validator | *(value, rule) => boolean \| Promise* |
|
||||
| pattern `v2.5.3` | Regex pattern | *RegExp* |
|
||||
| trigger `v2.5.2` | When to validate the form,can be set to `onChange`、`onBlur` | *string* |
|
||||
|
||||
|
@ -478,7 +478,7 @@ export default {
|
||||
|------|------|------|
|
||||
| message | 错误提示文案 | *string* |
|
||||
| required | 是否为必选字段 | *boolean* |
|
||||
| validator | 通过函数进行校验 | *() => boolean \| Promise* |
|
||||
| validator | 通过函数进行校验 | *(value, rule) => boolean \| Promise* |
|
||||
| pattern `v2.5.3` | 通过正则表达式进行校验 | *RegExp* |
|
||||
| trigger `v2.5.2` | 本项规则的触发时机,可选值为`onChange`、`onBlur` | *string* |
|
||||
|
||||
|
@ -72,7 +72,11 @@ test('rules prop - async validator', async () => {
|
||||
return {
|
||||
rules: [
|
||||
{
|
||||
validator: () => new Promise(resolve => resolve(true)),
|
||||
validator: (value, rule) => {
|
||||
expect(value).toEqual('123');
|
||||
expect(typeof rule).toEqual('object');
|
||||
return new Promise(resolve => resolve(true));
|
||||
},
|
||||
message: 'should pass',
|
||||
},
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user