Inputs

Repeater Free Pro

FormKit Pro Quick Installation Guide 🚀

Basic example

The repeater input is for repeating groups of inputs. You can use any number of FormKit inputs within a repeater, and repeaters themselves can be infinitely nested.

By default, the repeater input has the ability to shift, add, and remove items:

<FormKit  id="repeater"  name="users"  type="repeater"  label="Users"  draggable="true"  #default="{ index }">  <FormKit    type="email"    :label="`${index + 1}. Email`"    name="email"    validation="required|email"    placeholder="Add email address..."  /></FormKit>
Users
{
  "users": [
    {
      "email": "[email protected] "
    }
  ]
}

Unlike most other FormKit input types, the repeater input is of type list. You can see above that the users property (our repeater's value) is an array.

Schema example

Like all other FormKit inputs, the Repeater is able to be used in a form written in FormKit schema.

{  $formkit: 'repeater',  name: 'users',  children: [    {      $formkit: 'text',      name: 'email',      // $index is available to children.      label: '$: ($index + 1) + ". Email"',      validation: 'required|email',    },  ],},
{
  "users": [
    {
      "email": "[email protected] "
    }
  ]
}

Add button attrs

In the previous example, the 'Add Users' button is provided by default, and the button label is derived from the repeater's label. We're able to customize the label by using the add-button prop and setting it to a string, or remove it completely by setting it to false. Alternatively, similar to the submit-attrs prop on the FormKit's forms, we can provide an object of attributes to be applied to the button with add-attrs. In this example, we change the label of the repeater from Add Users to My custom label using the add-label prop:

<FormKit  id="repeater"  name="users"  type="repeater"  label="Philosophy Department Teaching Staff"  add-label="My custom 'Add Professor' button"  help="Edit the staff page here."><FormKit  label="Professor Name"  name="email"  validation="required|email"  placeholder="Add professor name..."/><FormKit  name="bio"  type="textarea"  label="Professor Bio"  placeholder="Enter bio here..."/></FormKit>
Philosophy Department Teaching Staff
Edit the staff page here.

Min/max

Like many other FormKit inputs, the repeater comes with a min and max prop. You can use these props to limit the number of items to be added or removed.

<template>  <FormKit    #default="{ value }"    type="form"    :actions="false"  >    <FormKit      id="repeater"      name="users"      type="repeater"      label="Users"      min="2"      max="3"    >      <FormKit        type="email"        label="Email"        name="email"        validation="required|email"      />    </FormKit>    <pre wrap>{{ value }}</pre>  </FormKit></template>
Users
{}

Empty state

When a repeater is allowed to be empty by having its min prop set to 0 and having no values, you can control the content rendered in the empty state using the empty slot.

<template>  <FormKit #default="{ value }" type="form" :actions="false">    <FormKit      id="repeater"      name="users"      type="repeater"      label="Users"      min="0"    >      <template #empty>        <h2 class="text-xl font-bold mb-4">Please add a user to get started.</h2>      </template>      <FormKit        type="email"        label="Email"        name="email"        validation="required|email"      />    </FormKit>    <pre wrap>{{ value }}</pre>  </FormKit></template>
Users

Please add a user to get started.

{}

Controls

The repeater input by default comes with a set of controls that allow you to shift, add, and remove items. You can control the visibility of these controls by setting the upControl, downControl, insertControl, and removeControl props to true or false.

<template>  <FormKit    type="group"    :actions="false"    #default="{ value }"  >    <FormKit      id="no-model"      type="repeater"      label="Ice cream orders"      help="Add as many ice cream orders as you want."      :insert-control="true"    >      <FormKit        name="name"        label="Person this order is for"      />      <FormKit        type="radio"        label="Flavors"        :options="['Vanilla', 'Chocolate', 'Strawberry']"      />    </FormKit>  </FormKit></template>
Ice cream orders
Add as many ice cream orders as you want.
  • Flavors

Custom controls

With FormKit's repeater, you are not restricted to our default controls. You can manipulate the repeater in anyway you want:

repeater-controls
controls
<!-- Imports hidden for brevity --><FormKit #default="{ value }" v-model="values" type="group">  <FormKit    id="repeater"    name="users"    type="repeater"    label="Users"    :insert-control="true"    add-label="+ Add user"    max="5"  >    <FormKit type="text" label="name" name="name" />  </FormKit>  <table>    <thead>      <tr>        <th>Add</th>        <th>Modify</th>        <th>Remove</th>      </tr>    </thead>    <tbody>      <tr>        <td><button id="push" @click="pushLast">Push a value</button></td>        <td>          <button id="assign" @click="changeFirst">Assign index 1</button>        </td>        <td><button id="shift" @click="shiftFirst">Shift a value</button></td>      </tr>      <tr>        <td>          <button id="unshift" @click="unshiftValue">Unshift a value</button>        </td>        <td>          <button id="splice" @click="spliceIt">            splice to assign (0, 2, [a, b])          </button>        </td>        <td>          <button id="spliceRemove" @click="spliceRemove">            splice to delete (1, 4)          </button>        </td>      </tr>      <tr>        <td>          <button id="inject" @click="injectAtIndexOne">            Inject at index 1          </button>        </td>        <td>          <button id="replace" @click="replaceIt">Replace it all</button>        </td>        <td><button id="pop" @click="popValue">Pop off value</button></td>      </tr>      <tr>        <td>&nbsp;</td>        <td><button id="sort" @click="sortIt">Sort it</button></td>        <td>&nbsp;</td>      </tr>    </tbody>  </table>  <pre wrap>{{ value }}</pre></FormKit>
import { ref } from 'vue'const values = ref({  users: [{ name: 'foofoo' }, { name: 'barbar' }],})const unshiftValue = () => {  values.value.users.unshift({ name: 'first' })}const pushLast = () => {  values.value.users.push({ name: 'moomoo' })}const changeFirst = () => {  values.value.users[1] = { name: 'booboo' }}const spliceIt = () => {  values.value.users.splice(    1,    2,    { name: 'spliced' },    { name: 'double splice' }  )}const shiftFirst = () => {  values.value.users.shift()}const popValue = () => {  values.value.users.pop()}const spliceRemove = async () => {  values.value.users.splice(1, 4)  await new Promise((r) => setTimeout(r, 200))}const replaceIt = () => {  values.value.users = [    { name: 'first' },    { name: 'second' },    { name: 'third' },  ]}const injectAtIndexOne = () => {  values.value.users.splice(1, 0, { name: 'injected' })}const sortIt = () => {  values.value.users.sort((a, b) => (a.name > b.name ? 1 : -1))}export {  values,  unshiftValue,  pushLast,  changeFirst,  spliceIt,  shiftFirst,  popValue,  spliceRemove,  replaceIt,  injectAtIndexOne,  sortIt,}
Users
AddModifyRemove
  
{
  "users": [
    {
      "name": "foofoo"
    },
    {
      "name": "barbar"
    }
  ]
}

Setting errors

You can set errors on a repeater or any repeatable item using dot notation. Here we are using the setErrors helper, but there are other methods as well:

// the 2nd argument of setErrors is input-level errors
formNode.setErrors( null, // no form errors,
  {
    // error on the repeater field:
    'teamMembers': ['There was a problem with 1 of your team members.']

    // error on a specific repeater item:
    'teamMembers.1.email': ['[email protected] is already on a team.'],
  }
)

Remember, your submit handler is passed the form's core node and can be used to conveniently set errors at depth. Read more about error handling here. Here's an example of a fake backend returning errors for both the repeater and one child:

<script setup>// This is just a mock of an actual axios instance.const axios = {  post: () => new Promise((r) => setTimeout(r, 2000)),}async function submitTeamMembers(teamMembers, formNode) {  const res = await axios.post(teamMembers)  formNode.setErrors(    null, // no form errors    {      // THE MAGIC IS HERE      teamMembers: ['There was a problem with 1 of your team members.'],      'teamMembers.1.email': [        '[email protected] is already on a team. Please remove her from that team before adding her to this team.',      ],    }  )}</script><template>  <p><em>Submit to see the errors.</em></p>  <FormKit    #default="{ value }"    type="form"    @submit="submitTeamMembers"    id="teamMemberForm"    submit-label="Submit to see the errors"    help="hello"  >    <FormKit      id="repeater"      name="teamMembers"      type="repeater"      label="Additional Team Members"      add-label="Add Team Member"      :value="[{ email: '[email protected]' }, { email: '[email protected]' }]"    >      <FormKit        type="email"        label="Team Member email"        name="email"        validation="required|email"        placeholder="Add email address..."      />    </FormKit>    <pre wrap>{{ value }}</pre>  </FormKit></template><style scoped>#teamMemberForm pre {  margin-bottom: 20px;}</style>

Submit to see the errors.

Additional Team Members
{}

Props & Attributes

PropType Default Description
add-labelstringnullUsed to change the label of the add button.
add-attrsobject{}Used to apply attributes to the add button element.
add-buttonbooleantrueConditional for whether to show the add button.
up-controlbooleantrueConditional for whether to show the up control.
down-controlbooleantrueConditional for whether to show the down control.
insert-controlbooleanfalseConditional for whether to show the insert control.
remove-controlbooleantrueConditional for whether to show the remove control.
minNumber1The minimum number of children.
maxNumbernullThe maximum number of children.
Show Universal props
configObject{}Configuration options to provide to the input’s node and any descendent node of this input.
delayNumber20Number of milliseconds to debounce an input’s value before the commit hook is dispatched.
dirtyBehaviorstringtouchedDetermines how the "dirty" flag of this input is set. Can be set to touched or comparetouched (the default) is more performant, but will not detect when the form is once again matching its initial state.
errorsArray[]Array of strings to show as error messages on this field.
helpString''Text for help text associated with the input.
idStringinput_{n}The unique id of the input. Providing an id also allows the input’s node to be globally accessed.
ignoreBooleanfalsePrevents an input from being included in any parent (group, list, form etc). Useful when using inputs for UI instead of actual values.
indexNumberundefinedAllows an input to be inserted at the given index if the parent is a list. If the input’s value is undefined, it inherits the value from that index position. If it has a value it inserts it into the lists’s values at the given index.
labelString''Text for the label element associated with the input.
nameStringinput_{n}The name of the input as identified in the data object. This should be unique within a group of fields.
parentFormKitNodecontextualBy default the parent is a wrapping group, list or form — but this props allows explicit assignment of the parent node.
prefix-iconString''Specifies an icon to put in the prefixIcon section.
preserveBooleanfalsePreserves the value of the input on a parent group, list, or form when the input unmounts.
preserve-errorsBooleanfalseBy default errors set on inputs using setErrors are automatically cleared on input, setting this prop to true maintains the error until it is explicitly cleared.
sections-schemaObject{}An object of section keys and schema partial values, where each schema partial is applied to the respective section.
suffix-iconString''Specifies an icon to put in the suffixIcon section.
typeStringtextThe type of input to render from the library.
validationString, Array[]The validation rules to be applied to the input.
validation-visibilityStringblurDetermines when to show an input's failing validation rules. Valid values are blur, dirty, and live.
validation-labelString{label prop}Determines what label to use in validation error messages, by default it uses the label prop if available, otherwise it uses the name prop.
validation-rulesObject{}Additional custom validation rules to make available to the validation prop.
valueAnyundefinedSeeds the initial value of an input and/or its children. Not reactive. Can seed entire groups (forms) and lists..

Sections & slot data

You can target a specific section of an input using that section's "key", allowing you to modify that section's classes, HTML (via :sections-schema, or content (via slots)). Read more about sections here.

Guests
Note guests you will bring to the race track.
Repeated fields here.
Move up
Remove
🗑
Insert
Move down
Add guest
Sorry, you may not add more than 3 guests.
Section-key Description
fieldsetA fieldset element that acts as the repeater’s wrapper.
legendA legend element that renders the label.
itemsA ul element that wraps the repeater’s items.
itemA li element that is rendered for each repeater item.
contentA container for the group section.
groupDoes not render an element. It structures the default slot into an object data structure.
controlsA ul element that wraps the repeater’s controls.
controlLabelA span element that renders the label of the given control.
upA li element that renders the up control.
upControlA button element that renders the up control.
moveUpIconA span element that renders the upControl’s icon.
downA li element that renders the down control.
downControlA button element that renders the down control.
moveDownIconA span element that renders the downControl’s icon.
insertA li element that renders the insert control.
insertControlA button element that renders the insert control.
insertIconA span element that renders the insertControl’s icon.
removeA li element that renders the remove control.
removeControlA button element that renders the remove control.
removeIconA span element that renders the removeControl’s icon.
Show Universal section keys
outerThe outermost wrapping element.
wrapperA wrapper around the label and input.
labelThe label of the input.
prefixHas no output by default, but allows content directly before an input element.
prefixIconAn element for outputting an icon before the prefix section.
innerA wrapper around the actual input element.
suffixHas no output by default, but allows content directly after an input element.
suffixIconAn element for outputting an icon after the suffix section.
inputThe input element itself.
helpThe element containing help text.
messagesA wrapper around all the messages.
messageThe element (or many elements) containing a message — most often validation and error messages.

Accessibility

All FormKit inputs are designed with the following accessibility considerations in mind. Help us continually improve accessibility for all by filing accessibility issues here:

  • Semantic markup
  • ARIA attributes
  • Keyboard accessibility
  • Focus indicators
  • Color contrast with the provided theme
  • Accessible labels, help text, and errors

Accessibility attributes

Section KeyAttributeValueDescription
itemsrolelistIndicates to assistive technologies that this element functions as a list.
itemrolelistitemIndicates to assistive technologies that this element functions as a list.
Universal Accessibility Attributes
labellabelforAssociates a label to an input element. Users can click on the label to focus the input or to toggle between states.
inputinputdisabledDisables an HTML element, preventing user interaction and signaling a non-interactive state.
aria-describedbyAssociates an element with a description, aiding screen readers.
aria-requiredAdded when input validation is set to required.
iconiconforLinks icon to input element when icon in rendered as a label.

Keyboard Interactions

Keyboard EventDescription
EnterInvokes the currently selected UI control.
SpaceInvokes the currently selected UI control.
Universal Keyboard Events
TabMoves the focus to the next focusable element on the page.
Shift+TabMoves the focus to the previous focusable element on the page.