useFieldArray with Pinia store #4233
-
Hello, Wondering how to use My store: import { defineStore } from "pinia";
import { useForm } from "vee-validate";
import { array, object, string } from "yup";
import { useSettingsStore } from "~/stores/settings";
const recipeSchema = object().shape({
ingredients: array().of(
object().shape({
name: string(),
volume: string().required(),
unit: string(),
abv: string(),
cost: string(),
})
)
});
export const useRecipeStore = defineStore("recipe", () => {
const settings = useSettingsStore();
const defaultIngredient = {
name: "",
volume: "",
unit: settings.defaultUnitOption,
abv: "",
cost: "",
};
const { values } = useForm({
validationSchema: recipeSchema,
});
const {
remove: removeIngredient,
push: addIngredient,
fields: ingredients,
} = useFieldArray("ingredients");
return {
ingredients,
defaultIngredient,
addIngredient,
removeIngredient
};
}); Accessing the array: <template>
<RecipeIngredient
v-for="(ingredient, i) in ingredients"
:key="ingredient.key"
:index="i"
:ingredient="ingredient.value"
@remove="removeIngredient(i)"
/>
<button
type="button"
@click="addIngredient(defaultIngredient)"
>
<PlusIcon class="h-8 w-8" />
Add Ingredient
</button>
</template>
<script setup>
import PlusIcon from "~icons/material-symbols/add";
import { useRecipeStore } from "~/stores/recipe";
const { ingredients, addIngredient, removeIngredient, defaultIngredient } = useRecipeStore();
</script> Any ideas what to do? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 2 replies
-
Anyone? 😭 |
Beta Was this translation helpful? Give feedback.
-
Sorry for the delay, does it work if you take it out of the Pinia store? I can try to setup a sample within the week to test it out but would be great if you can provide an example that I can fork/download. |
Beta Was this translation helpful? Give feedback.
-
@logaretm No worries. Looks like you've been busy with the new release 😁. I'll try and get a stackblitz set up tonight. |
Beta Was this translation helpful? Give feedback.
-
@logaretm Ok. I put this together somewhat quickly so I can get to sleep 😪 . Hopefully, it does the trick. It's entirely possible I'm totally misunderstanding how to use this properly. Anyways... If you enter some values, add an "ingredient", etc; you can see the values and array items updating successfully in the Vue devtools. However, upon changing to "another page", the values become undefined. I think I was having other problems as well before, but I've since refactored some things in my app so I can't recall exactly. But, any insight into how to integrate useFieldArray with Pinia properly would be great in general. |
Beta Was this translation helpful? Give feedback.
-
Sorry for the delay, again.... I took a look and seems exactly the same issue as in #4269. If you add const { values } = useForm({
validationSchema: recipeSchema,
keepValuesOnUnmount: true,
initialValues: {
ingredients: [
{
name: '',
volume: '',
},
],
},
}); By default vee-validate removes fields that get unmounted from the values object. |
Beta Was this translation helpful? Give feedback.
Sorry for the delay, again....
I took a look and seems exactly the same issue as in #4269. If you add
keepValuesOnUnmount
touseForm
it will work.By default vee-validate removes fields that get unmounted from the values object.