nextjs-cms

checkboxField

An on/off switch stored as a boolean.

import { checkboxField } from 'nextjs-cms/core/fields'

Basic usage

checkboxField({ name: 'is_featured' })

Rendered as a switch, stored as BOOLEAN (1 or 0).

With every option

checkboxField({
    name: 'is_featured',
    label: { en: 'Featured on the homepage', ar: 'مميز في الصفحة الرئيسية' },
    defaultValue: true,
    order: 10,
    localized: false,
    conditionalRules: [{ field: status, condition: 'equals', value: 'published' }],
})

Options

OptionTypeDefaultDescription
defaultValueboolean | positive integernone (off)Initial state on the create form, and the column default. 1 is treated as true.

Plus the common options. required has no effect: an unchecked box is a valid false.

Options in detail

Truthy values

When the form is submitted, the value counts as checked if it is true, 1, '1', 'true', or 'on' (what HTML checkboxes send). Anything else is false. That's also what the server uses to evaluate conditional rules, so this works:

const hasDiscount = checkboxField({ name: 'has_discount' })

const discount = numberField({
    name: 'discount_percent',
    minValue: 1,
    maxValue: 90,
    required: true,
    conditionalRules: [{ field: hasDiscount, condition: 'equals', value: true }],
})

The discount input appears when the switch is turned on, and is required only then.

Examples

Visibility flags

checkboxField({ name: 'is_published', label: 'Published', defaultValue: true })
checkboxField({ name: 'show_in_menu', label: 'Show in navigation' })
checkboxField({ name: 'noindex', label: 'Hide from search engines' })

Reading on your website

import { and, eq } from 'drizzle-orm'

const posts = await db
    .select()
    .from(PostsTable)
    .where(and(eq(PostsTable.isPublished, true), eq(PostsTable.isFeatured, true)))

Storage

ColumnBOOLEAN (TINYINT(1) in MySQL)
Value1 (checked) or 0