Skip to content

Commit

Permalink
feat: field.reset() should reset the field to its initial value
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Nov 7, 2020
1 parent 28b6292 commit a11f1b7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
4 changes: 3 additions & 1 deletion packages/vee-validate/src/useField.ts
Expand Up @@ -234,7 +234,8 @@ function useValidationState({
valueProp: any;
}) {
const errors: Ref<string[]> = ref([]);
const initialValue = getFromPath(unref(inject(FormInitialValues, undefined)), unref(name)) ?? initValue;
const formInitialValues = inject(FormInitialValues, undefined);
const initialValue = getFromPath(unref(formInitialValues), unref(name)) ?? initValue;
const { reset: resetFlags, meta } = useMeta(initialValue);
const value = useFieldValue(initialValue, name, form);
if (hasCheckedAttr(type) && initialValue) {
Expand Down Expand Up @@ -285,6 +286,7 @@ function useValidationState({

// Resets the validation state
const reset = () => {
value.value = getFromPath(unref(formInitialValues), unref(name)) ?? initValue;
errors.value = [];
resetFlags();
};
Expand Down
5 changes: 3 additions & 2 deletions packages/vee-validate/tests/Field.spec.ts
Expand Up @@ -455,12 +455,13 @@ describe('<Field />', () => {

setValue(input, '');
await flushPromises();

expect(error.textContent).toBe(REQUIRED_MESSAGE);

setValue(input, '123');
await flushPromises();
wrapper.$el.querySelector('button').click();
await flushPromises();
expect(error.textContent).toBe('');
expect(input.value).toBe('');
});

test('yup abortEarly is set by bails global option', async () => {
Expand Down

0 comments on commit a11f1b7

Please sign in to comment.