mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
feat(Form): resetValidation support multiple names (#7811)
This commit is contained in:
parent
d92d29c885
commit
1fac5c5f4f
@ -473,7 +473,7 @@ Use [ref](https://vuejs.org/v2/api/#ref) to get Form instance and call instance
|
|||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| submit | Submit form | - | - |
|
| submit | Submit form | - | - |
|
||||||
| validate | Validate form | _name?: string \| string[]_ | _Promise_ |
|
| 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_ | - |
|
| scrollToField `v2.8.3` | Scroll to field | _name: string, alignToTop: boolean_ | - |
|
||||||
|
|
||||||
### Slots
|
### Slots
|
||||||
|
@ -509,8 +509,8 @@ export default {
|
|||||||
| 方法名 | 说明 | 参数 | 返回值 |
|
| 方法名 | 说明 | 参数 | 返回值 |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| submit | 提交表单,与点击提交按钮的效果等价 | - | - |
|
| submit | 提交表单,与点击提交按钮的效果等价 | - | - |
|
||||||
| validate | 验证表单,支持传入 `name` 来验证单个或多个表单项 | _name?: string \| string[]_ | _Promise_ |
|
| validate | 验证表单,支持传入 `name` 来验证单个或部分表单项 | _name?: string \| string[]_ | _Promise_ |
|
||||||
| resetValidation | 重置表单项的验证提示,支持传入 `name` 来重置单个表单项 | _name?: string_ | - |
|
| resetValidation | 重置表单项的验证提示,支持传入 `name` 来重置单个或部分表单项 | _name?: string \| string[]_ | - |
|
||||||
| scrollToField `v2.8.3` | 滚动到对应表单项的位置,默认滚动到顶部,第二个参数传 false 可滚动至底部 | _name: string, alignToTop: boolean_ | - |
|
| scrollToField `v2.8.3` | 滚动到对应表单项的位置,默认滚动到顶部,第二个参数传 false 可滚动至底部 | _name: string, alignToTop: boolean_ | - |
|
||||||
|
|
||||||
### Slots
|
### Slots
|
||||||
|
@ -124,10 +124,13 @@ export default createComponent({
|
|||||||
|
|
||||||
// @exposed-api
|
// @exposed-api
|
||||||
resetValidation(name) {
|
resetValidation(name) {
|
||||||
this.fields.forEach((item) => {
|
if (name && !Array.isArray(name)) {
|
||||||
if (!name || item.name === name) {
|
name = [name];
|
||||||
item.resetValidation();
|
}
|
||||||
}
|
|
||||||
|
const fields = this.getFieldsByNames(name);
|
||||||
|
fields.forEach((item) => {
|
||||||
|
item.resetValidation();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -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) => {
|
test('resetValidation method - reset one field', (done) => {
|
||||||
const wrapper = mountSimpleRulesForm({
|
const wrapper = mountSimpleRulesForm({
|
||||||
mounted() {
|
mounted() {
|
||||||
|
2
types/form.d.ts
vendored
2
types/form.d.ts
vendored
@ -5,7 +5,7 @@ export class Form extends VanComponent {
|
|||||||
|
|
||||||
validate(name?: string | string[]): Promise<void>;
|
validate(name?: string | string[]): Promise<void>;
|
||||||
|
|
||||||
resetValidation(name?: string): void;
|
resetValidation(name?: string | string[]): void;
|
||||||
|
|
||||||
scrollToField(name: string, options?: boolean | ScrollIntoViewOptions): void;
|
scrollToField(name: string, options?: boolean | ScrollIntoViewOptions): void;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user