Merge branch '2.x' into dev
This commit is contained in:
+2
-2
@@ -516,8 +516,8 @@ Use [ref](https://v3.vuejs.org/guide/component-template-refs.html) to get Form i
|
||||
| Name | Description | Attribute | Return value |
|
||||
| --- | --- | --- | --- |
|
||||
| submit | Submit form | - | - |
|
||||
| validate | Validate form | _name?: string_ | _Promise_ |
|
||||
| resetValidation | Reset validation | _name?: string_ | - |
|
||||
| validate | Validate form | _name?: string \| string[]_ | _Promise_ |
|
||||
| resetValidation | Reset validation | _name?: string \| string[]_ | - |
|
||||
| scrollToField `v2.8.3` | Scroll to field | _name: string, alignToTop: boolean_ | - |
|
||||
|
||||
### Slots
|
||||
|
||||
@@ -555,8 +555,8 @@ export default {
|
||||
| 方法名 | 说明 | 参数 | 返回值 |
|
||||
| --- | --- | --- | --- |
|
||||
| submit | 提交表单,与点击提交按钮的效果等价 | - | - |
|
||||
| validate | 验证表单,支持传入`name`来验证单个表单项 | _name?: 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
|
||||
|
||||
+22
-10
@@ -37,11 +37,19 @@ export default createComponent({
|
||||
setup(props, { emit, slots }) {
|
||||
const { children, linkChildren } = useChildren(FORM_KEY);
|
||||
|
||||
const validateSeq = () =>
|
||||
const getFieldsByNames = (names) => {
|
||||
if (names) {
|
||||
return children.filter((field) => names.indexOf(field.name) !== -1);
|
||||
}
|
||||
return children;
|
||||
};
|
||||
|
||||
const validateSeq = (names) =>
|
||||
new Promise((resolve, reject) => {
|
||||
const errors = [];
|
||||
const fields = getFieldsByNames(names);
|
||||
|
||||
children
|
||||
fields
|
||||
.reduce(
|
||||
(promise, field) =>
|
||||
promise.then(() => {
|
||||
@@ -64,9 +72,10 @@ export default createComponent({
|
||||
});
|
||||
});
|
||||
|
||||
const validateAll = () =>
|
||||
const validateAll = (names) =>
|
||||
new Promise((resolve, reject) => {
|
||||
Promise.all(children.map((item) => item.validate())).then((errors) => {
|
||||
const fields = getFieldsByNames(names);
|
||||
Promise.all(fields.map((item) => item.validate())).then((errors) => {
|
||||
errors = errors.filter((item) => item);
|
||||
|
||||
if (errors.length) {
|
||||
@@ -96,17 +105,20 @@ export default createComponent({
|
||||
};
|
||||
|
||||
const validate = (name) => {
|
||||
if (name) {
|
||||
if (name && !Array.isArray(name)) {
|
||||
return validateField(name);
|
||||
}
|
||||
return props.validateFirst ? validateSeq() : validateAll();
|
||||
return props.validateFirst ? validateSeq(name) : validateAll(name);
|
||||
};
|
||||
|
||||
const resetValidation = (name) => {
|
||||
children.forEach((item) => {
|
||||
if (!name || item.name === name) {
|
||||
item.resetValidation();
|
||||
}
|
||||
if (name && !Array.isArray(name)) {
|
||||
name = [name];
|
||||
}
|
||||
|
||||
const fields = getFieldsByNames(name);
|
||||
fields.forEach((item) => {
|
||||
item.resetValidation();
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ test('validate method - validate all fields', (done) => {
|
||||
});
|
||||
});
|
||||
|
||||
test('validate method - validate one field and passed', (done) => {
|
||||
test('validate method - validate one field and failed', (done) => {
|
||||
mountSimpleRulesForm({
|
||||
mounted() {
|
||||
this.$refs.form.validate('A').catch((err) => {
|
||||
@@ -48,7 +48,7 @@ test('validate method - validate one field and passed', (done) => {
|
||||
});
|
||||
});
|
||||
|
||||
test('validate method - validate one field and failed', (done) => {
|
||||
test('validate method - validate one field and passed', (done) => {
|
||||
mountForm({
|
||||
template: `
|
||||
<van-form ref="form" @failed="onFailed">
|
||||
@@ -64,6 +64,25 @@ test('validate method - validate one field and failed', (done) => {
|
||||
});
|
||||
});
|
||||
|
||||
test('validate method - validate two fields and failed', (done) => {
|
||||
mountForm({
|
||||
template: `
|
||||
<van-form ref="form" @failed="onFailed">
|
||||
<van-field name="A" :rules="rulesA" value="123" />
|
||||
<van-field name="B" :rules="rulesB" value="" />
|
||||
<van-button native-type="submit" />
|
||||
</van-form>
|
||||
`,
|
||||
data: getSimpleRules,
|
||||
mounted() {
|
||||
this.$refs.form.validate(['A', 'B']).catch((error) => {
|
||||
expect(error).toEqual([{ message: 'B failed', name: 'B' }]);
|
||||
done();
|
||||
});
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test('validate method - unexisted name', (done) => {
|
||||
mountSimpleRulesForm({
|
||||
mounted() {
|
||||
@@ -85,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() {
|
||||
|
||||
Reference in New Issue
Block a user