Skip to content

FieldArray

The <FieldArray> is a component for create repeatable array fields.

Basic usage

vue
<script lang="ts" setup>
import { Form, FieldArray, Field } from 'vue-formify';

const sendForm = (data) => {
	console.log(data);
};

</script>
<template>
	<Form @submit="sendForm">
        <FieldArray name="links" v-slot="{fields, add, remove}">
            <fieldset v-for="(field, index) of fields" :key="field.id">
                <Field :name="`links[${index}]`" />
                <button type="button" @click="remove(index)">Remove</button>
            </fieldset>
            <button type="button" @click="add">Add</button>
        </FieldArray>
		<button>Send</button>
	</Form>
</template>

Best practice

When you using v-for on the fields always use the field.id in the :key attribute and use the index as array index. Otherwise it won't works as expected.

Api reference

Props

PropDescription
nameField name
initial-valuesField default value
ignoreIgnore field when extract data
preservePreserve field value when field is unmounted

Slots

SlotParameterDescription
default{ fields, add, remove, error }fields: Generated fields for render
error: FieldArray error value
add: Function for add new field
remove: Remove field from array