Build dynamic forms that show/hide fields based on conditions and create cascading dropdown menus.
Show or hide form fields based on the value of other fields:
<template>
<UForm>
<UFormField label="Product Type" name="type">
<USelectMenu
v-model="state.type"
:options="['physical', 'digital']"
/>
</UFormField>
<!-- Show only for physical products -->
<UFormField v-if="state.type === 'physical'" label="Weight" name="weight">
<UInput v-model.number="state.weight" type="number" />
</UFormField>
<!-- Show only for digital products -->
<UFormField v-if="state.type === 'digital'" label="Download URL" name="downloadUrl">
<UInput v-model="state.downloadUrl" />
</UFormField>
</UForm>
</template>
Create cascading dropdowns where options in one field depend on the selection in another:
useCollectionQuery patterns, see Querying Data.<script setup lang="ts">
const { items: categories } = await useCollectionQuery('shopCategories')
const selectedCategory = ref<string | null>(null)
// Fetch subcategories when category changes
const { items: subcategories } = await useCollectionQuery('shopSubcategories', {
query: computed(() => ({
categoryId: selectedCategory.value
}))
})
</script>
<template>
<UFormField label="Category" name="categoryId">
<USelectMenu
v-model="selectedCategory"
:options="categories"
option-attribute="name"
/>
</UFormField>
<UFormField v-if="selectedCategory" label="Subcategory" name="subcategoryId">
<USelectMenu
v-model="state.subcategoryId"
:options="subcategories"
option-attribute="name"
/>
</UFormField>
</template>
v-if directive to conditionally render form fieldsFor common patterns like loading data from related collections, Nuxt Crouton can generate dependent field logic automatically through schema configuration.
Use Auto-Generated Dependent Fields when:
slotButtonGroupUse Manual Conditional Fields when:
Instead of manually writing:
<script setup>
const { data: locationData } = await useFetch(() =>
state.value.location
? `/api/teams/${teamId}/bookingsLocations/${state.value.location}`
: null
, {
watch: [() => state.value.location],
immediate: false
})
watch(() => state.value.location, () => {
state.value.slot = 0
})
</script>
Simply configure your schema:
{
"slot": {
"type": "number",
"meta": {
"dependsOn": "location",
"dependsOnField": "slots",
"dependsOnCollection": "locations",
"displayAs": "slotButtonGroup"
}
}
}
The generator creates all the fetch logic, watchers, and conditional UI automatically.
See Schema Format - Dependent Fields for complete configuration details.