Skip to content

Loss of object fields after calling setFieldValue #4999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
2 of 5 tasks
julienguillot77 opened this issue Mar 5, 2025 · 0 comments
Open
2 of 5 tasks

Loss of object fields after calling setFieldValue #4999

julienguillot77 opened this issue Mar 5, 2025 · 0 comments

Comments

@julienguillot77
Copy link

julienguillot77 commented Mar 5, 2025

What happened?

I'm using vee-validate with Zod on my Nuxt 3 application.

I have an array of objects that represents some items.

Here is my Zod validation model :

z.object({
    id: z.number().readonly(),
    // some fields are omitted
    items_attributes: z.array(
      z.object({
        id: z.number().nullable().optional().or(z.number().positive()),
       // Some fields are omitted
        _destroy: z.string().default("0"),
      })
    ),
  })

items_attributes are the array of object I'm talking about.

So the _destroy field must be set to "1" to be defined as "marked for destruction". I'm using Rails as backend API.

In order to do that, I click to a button that triggers a function

function onDeleteItem(index: number) {
  let items = values.items_attributes;
  const item = items![index];

  if (item.id) {
    setFieldValue(`items_attributes.${index}._destroy`, "1");
  } else {
    // Remove the item if it doesn't have an id
    items = items!.filter((_, i) => i !== index);

    setFieldValue("items_attributes", items);
  }
}

setFieldValue(items_attributes.${index}._destroy, "1"); This is where the issue occurs.
The object at index x has become partial and some fields are missing !

As long as do not edit _destroy value, the issue does not occurs (even if I set it to "0").

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

NO

Code of Conduct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant