diff --git a/src/form/README.md b/src/form/README.md index e71665ea1..df0627506 100644 --- a/src/form/README.md +++ b/src/form/README.md @@ -473,7 +473,7 @@ Use [ref](https://vuejs.org/v2/api/#ref) to get Form instance and call instance | --- | --- | --- | --- | | submit | Submit form | - | - | | validate | Validate form | _name?: string \| string[]_ | _Promise_ | -| resetValidation | Reset validation | _name?: string_ | - | +| resetValidation | Reset validation | _name?: string \| string[]_ | - | | scrollToField `v2.8.3` | Scroll to field | _name: string, alignToTop: boolean_ | - | ### Slots diff --git a/src/form/README.zh-CN.md b/src/form/README.zh-CN.md index 2285aa97f..89491f4ae 100644 --- a/src/form/README.zh-CN.md +++ b/src/form/README.zh-CN.md @@ -509,8 +509,8 @@ export default { | 方法名 | 说明 | 参数 | 返回值 | | --- | --- | --- | --- | | submit | 提交表单,与点击提交按钮的效果等价 | - | - | -| validate | 验证表单,支持传入 `name` 来验证单个或多个表单项 | _name?: string \| string[]_ | _Promise_ | -| resetValidation | 重置表单项的验证提示,支持传入 `name` 来重置单个表单项 | _name?: string_ | - | +| validate | 验证表单,支持传入 `name` 来验证单个或部分表单项 | _name?: string \| string[]_ | _Promise_ | +| resetValidation | 重置表单项的验证提示,支持传入 `name` 来重置单个或部分表单项 | _name?: string \| string[]_ | - | | scrollToField `v2.8.3` | 滚动到对应表单项的位置,默认滚动到顶部,第二个参数传 false 可滚动至底部 | _name: string, alignToTop: boolean_ | - | ### Slots diff --git a/src/form/index.js b/src/form/index.js index 363de6eda..225b910a6 100644 --- a/src/form/index.js +++ b/src/form/index.js @@ -124,10 +124,13 @@ export default createComponent({ // @exposed-api resetValidation(name) { - this.fields.forEach((item) => { - if (!name || item.name === name) { - item.resetValidation(); - } + if (name && !Array.isArray(name)) { + name = [name]; + } + + const fields = this.getFieldsByNames(name); + fields.forEach((item) => { + item.resetValidation(); }); }, diff --git a/src/form/test/methods.spec.js b/src/form/test/methods.spec.js index 1e58b9937..a91700c9e 100644 --- a/src/form/test/methods.spec.js +++ b/src/form/test/methods.spec.js @@ -104,6 +104,19 @@ test('resetValidation method - reset all fields', (done) => { }); }); +test('resetValidation method - reset two fields', (done) => { + const wrapper = mountSimpleRulesForm({ + mounted() { + this.$refs.form.validate().catch(() => { + this.$refs.form.resetValidation(['A', 'B']); + const errors = wrapper.findAll('.van-field__error-message'); + expect(errors.length).toEqual(0); + done(); + }); + }, + }); +}); + test('resetValidation method - reset one field', (done) => { const wrapper = mountSimpleRulesForm({ mounted() { diff --git a/types/form.d.ts b/types/form.d.ts index 2584f24a8..53f5417e0 100644 --- a/types/form.d.ts +++ b/types/form.d.ts @@ -5,7 +5,7 @@ export class Form extends VanComponent { validate(name?: string | string[]): Promise; - resetValidation(name?: string): void; + resetValidation(name?: string | string[]): void; scrollToField(name: string, options?: boolean | ScrollIntoViewOptions): void; }