Skip to content

Commit

Permalink
feat: compute validation state after validate() calls closes #2686 and
Browse files Browse the repository at this point in the history
…closes #2781
  • Loading branch information
logaretm committed Sep 22, 2020
1 parent 8fed3a2 commit e6539b3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/components/Observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,13 @@ export const ValidationObserver = (Vue as withObserverNode).extend({
...this.observers.filter((o: any) => !o.disabled).map((obs: any) => obs.validate({ silent }))
]);

return results.every(r => r);
const isValid = results.every(r => r);
const { errors, flags, fields } = computeObserverState.call(this);
this.errors = errors;
this.flags = flags;
this.fields = fields;

return isValid;
},
async handleSubmit(cb: Function) {
const isValid = await this.validate();
Expand Down
30 changes: 30 additions & 0 deletions tests/providers/observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,33 @@ test('Sets errors for nested observer providers', async () => {
expect(wrapper.find('#error1').text()).toBe('wrong');
expect(wrapper.find('#error2').text()).toBe('whoops');
});

// #2686 and #2781
test('Errors are synced immediately after validation', async () => {
const wrapper = mount(
{
data: () => ({
email: '',
name: ''
}),
template: `
<ValidationObserver ref="obs" v-slot="{ errors }">
<ValidationProvider vid="name" rules="required" v-slot="ctx">
<input v-model="name" type="text">
</ValidationProvider>
<ValidationProvider vid="email" rules="required" v-slot="ctx">
<input v-model="email" type="text">
</ValidationProvider>
<p v-for="fieldErrors in errors">{{ fieldErrors[0] }}</p>
</ValidationObserver>
`
},
{ localVue: Vue, sync: false }
);

await flush();

await wrapper.vm.$refs.obs.validate();
expect(wrapper.vm.$refs.obs.errors.name).toHaveLength(1);
expect(wrapper.vm.$refs.obs.errors.email).toHaveLength(1);
});

0 comments on commit e6539b3

Please sign in to comment.