Skip to content

resetField is not properly resetting arrays #5053

Open
@danielalejandromatheus

Description

@danielalejandromatheus

What happened?

I have a use case where I'm resetting a single field that's being handled by a useFieldArray , when I use resetForm it's cleared like it should (along with other fields and their errors, which I'm trying to avoid), but in this particular use case I need to only clear said field and it's errors with resetField, however, what resetField('array') does right now is just clear the each value while retaining the number of elements in the array and it's errors, resetField('array', {value: undefined}) does the trick, but it keeps the errors.

I managed to make it work with a hacky reset function and exposing it to the parent component with defineExpose, something like

const reset = () => {
  const shallowCopy = [...fields.value];
  for(const field of shallowCopy) {
    remove(fields.value.indexOf(field));
  }
};

But I was thinking maybe the resetField approach is not behaving like it should, or at least it's not consistent with resetForm

Reproduction steps

  1. Have a form with useForm, have it define resetForm and resetField
  2. Have an array with useFieldArray and some simple validation (must check if it's a string for example)
  3. Try to reset the form programatically, it's expected behavior
  4. Try to reset the field 'array' programatically, it only reset fields and not errors
    https://stackblitz.com/edit/vite-jrpjdat4

Version

Vue.js 3.x and vee-validate 4.x

What browsers are you seeing the problem on?

  • Firefox
  • Chrome
  • Safari
  • Microsoft Edge

Relevant log output

Demo link

https://stackblitz.com/edit/vite-jrpjdat4

Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions