Skip to content

Checkbox Group ​

Notification settings where at least one channel must stay on.

Live exampleCheckboxes that fill one list
Notify me by

Output

Submit to see the output

Uncheck every channel and press Save to see the error of the group. Then check a few and save again, and they are listed in the output.

vue
<script setup lang="ts">
import type { OutputOf } from '@formtree/vue'

import { provideFormNode, useForm } from '@formtree/vue'
import { shallowRef } from 'vue'

import CheckboxGroupField from '../components/checkbox-group-field.vue'
import { CHANNEL, notificationsForm } from '../forms/notifications'

const CHANNEL_OPTIONS = [
  { value: CHANNEL.email, label: 'Email' },
  { value: CHANNEL.sms, label: 'SMS' },
  { value: CHANNEL.push, label: 'Push notification' },
]

const form = provideFormNode(
  useForm(notificationsForm, {
    initialValue: { channels: [CHANNEL.email] },
  }),
)

const output = shallowRef<OutputOf<typeof notificationsForm>>()

const saveSettings = form.handleSubmit(settings => {
  output.value = settings
})
</script>

<template>
  <div class="demo-columns">
    <form class="demo-form" novalidate @submit.prevent="saveSettings">
      <CheckboxGroupField name="channels" label="Notify me by" :options="CHANNEL_OPTIONS" />
      <div class="demo-row">
        <button type="submit">Save</button>
      </div>
    </form>

    <div class="demo-form">
      <p class="demo-hint">Output</p>
      <pre class="demo-output">{{ output ? JSON.stringify(output, null, 2) : 'Submit to see the output' }}</pre>
    </div>
  </div>
</template>
ts
import { defineGroup } from '@formtree/core'
import { z } from 'zod'

export const CHANNEL = {
  email: 'email',
  sms: 'sms',
  push: 'push',
} as const

export const notificationsForm = defineGroup({
  schema: z.object({
    channels: z
      .array(z.enum(CHANNEL), { error: 'Choose at least one channel' })
      .min(1, { error: 'Choose at least one channel' }),
  }),
})
vue
<script setup lang="ts">
import { useField } from '@formtree/vue'
import { computed } from 'vue'

interface CheckboxOption {
  readonly value: string
  readonly label: string
}

const props = defineProps<{
  name: string
  label: string
  options: ReadonlyArray<CheckboxOption>
}>()

const field = useField<Array<string>>(() => props.name)

const selected = computed({
  get: () => field.model ?? [],
  set: values => {
    field.model = props.options.map(option => option.value).filter(value => values.includes(value))
  },
})
</script>

<template>
  <fieldset v-bind="field.attrs" class="demo-choices" tabindex="-1">
    <legend>{{ label }}<span v-if="field.state.required" aria-hidden="true">*</span></legend>
    <label v-for="option in options" :key="option.value" class="demo-choice">
      <input v-model="selected" type="checkbox" :value="option.value" @blur="field.attrs.onBlur" />
      {{ option.label }}
    </label>
    <p :id="field.errorId" class="demo-error">{{ field.errorMessage }}</p>
  </fieldset>
</template>

Released under the MIT License.