fix(Form): incorrect validation order when add field dynamically

This commit is contained in:
chenjiahan
2020-04-01 21:39:08 +08:00
committed by neverland
parent 02e67fdd47
commit b8dea3c13b
5 changed files with 79 additions and 24 deletions
+10
View File
@@ -1,4 +1,5 @@
import { createNamespace } from '../utils';
import { sortChildren } from '../utils/vnodes';
const [createComponent, bem] = createNamespace('form');
@@ -124,6 +125,15 @@ export default createComponent({
});
},
addField(field) {
this.fields.push(field);
sortChildren(this.fields, this);
},
removeField(field) {
this.fields = this.fields.filter(item => item !== field);
},
getValues() {
return this.fields.reduce((form, field) => {
form[field.name] = field.formValue;
+38
View File
@@ -35,3 +35,41 @@ test('dynamic add/remove fileds', async () => {
await submitForm(wrapper);
expect(onSubmit).toHaveBeenCalledWith({ B: '' });
});
test('dynamic add fileds when validate-first', async () => {
const onFailed = jest.fn();
const wrapper = mountForm({
template: `
<van-form validate-first @failed="onFailed">
<van-field
v-if="show"
name="A"
value=""
:rules="[{ required: true, message: 'A' }]"
/>
<van-field
name="B"
value=""
:rules="[{ required: true, message: 'B' }]"
/>
<van-button native-type="submit" />
</van-form>
`,
data() {
return {
show: false,
};
},
methods: {
onFailed,
},
});
await submitForm(wrapper);
expect(onFailed.mock.calls[0][0].errors[0].name).toEqual('B');
wrapper.setData({ show: true });
await submitForm(wrapper);
expect(onFailed.mock.calls[1][0].errors[0].name).toEqual('A');
});