Skip to content

Conditional Fields ​

A conditional part applies only after a certain answer. While it does not, it is neither validated nor submitted.

Making a Part Conditional ​

Fields that apply only after a certain answer, such as card details when the customer pays by card, go in a child, and the activeWhen option of the group states when it applies:

ts
const cardGroup = defineGroup({
  schema: z.object({
    holder: z.string({ error: 'Cardholder name is required' }),
    number: z.string({ error: 'Card number is required' }),
  }),
})

export const paymentForm = defineGroup({
  schema: z.object({
    method: z.enum(['card', 'invoice'], { error: 'Choose a payment method' }),
  }),
  children: { card: cardGroup },
  activeWhen: {
    card: ({ value }) => value.method == 'card',
  },
})

Each function receives the value of the schema of the group and runs again after every change.

Option Order

activeWhen goes after schema and children, because its functions are typed from them, as TypeScript explains.

Inactive Parts ​

An inactive part is not validated and is left out of the submitted values, so card is optional in the submit function:

ts
const placeOrder = form.handleSubmit(async payment => {
  if (payment.card) {
    await chargeCard(payment.card)
  }
})

Its value is kept, so a customer who switches back to card finds the details as they left them. Conditional Checkout shows this live.

Showing Active Parts ​

state.active tells the page whether a part applies:

vue
<script setup lang="ts">
import { useForm } from '@formtree/vue'
import { paymentForm } from './forms/payment'

const form = useForm(paymentForm)
const card = form.group('card')
</script>

<template>
  <fieldset v-if="card.state.active">
    <legend>Card</legend>
  </fieldset>
</template>
tsx
import { useForm, useNodeState } from '@formtree/react'
import { paymentForm } from './forms/payment'

export function PaymentForm() {
  const form = useForm(paymentForm)
  const card = form.group('card')
  const cardActive = useNodeState(card, node => node.state.active)

  return cardActive ? (
    <fieldset>
      <legend>Card</legend>
    </fieldset>
  ) : null
}
ts
import { createForm } from '@formtree/core'
import { paymentForm } from './forms/payment'

const form = createForm(paymentForm)
const card = form.group('card')
const cardFieldset = document.querySelector<HTMLFieldSetElement>('#card')!

toggleCardFieldset()
card.subscribe(toggleCardFieldset)

function toggleCardFieldset() {
  cardFieldset.hidden = !card.state.active
}

Whether an inactive part is hidden is a decision of the page alone, because the form never depends on what is rendered.

Nesting Conditions ​

Conditions nest with their groups, so a part inside an inactive part is inactive too:

ts
const stateField = defineField({
  schema: z.string({ error: 'State is required' }),
})

const addressGroup = defineGroup({
  schema: z.object({
    street: z.string({ error: 'Street is required' }),
    country: z.string({ error: 'Country is required' }),
  }),
  children: { state: stateField },
  activeWhen: {
    state: ({ value }) => value.country == 'US',
  },
})

const deliveryGroup = defineGroup({
  schema: z.object({
    method: z.enum(['shipping', 'pickup'], {
      error: 'Choose a delivery method',
    }),
  }),
  children: { address: addressGroup },
  activeWhen: {
    address: ({ value }) => value.method == 'shipping',
  },
})

Single Values

activeWhen accepts only keys of children, so a single value that comes and goes, such as a region that only some countries have, gets a definition of its own, as Choosing Between a Key and a Child explains.

Clearing a Part ​

An inactive part keeps its values, and reset clears them:

ts
form.group('card').reset({ value: {} })

The card fields then come back empty if the customer picks card again.

Released under the MIT License.