Skip to content

Nested Form ​

The nestedform field renders a whole form inside one field. On a JSON attribute it saves the inner fields as one object, and on a relation it edits the related record.

JSON object ​

The form option takes the same definition as any form, inline or as the path to a YAML file, and the inner fields can be of any type. Each inner field is named inside the field, such as address[line1], and the field saves them together as one object, such as {"recipient": "...", "line1": "..."}.

yaml
fields:
    address:
        label: Address
        type: nestedform
        comment: Saved on the sample as one JSON object.
        form:
            fields:
                recipient:
                    label: Recipient
                line1:
                    label: Street
                suburb:
                    label: Suburb
                    span: left
                region_code:
                    label: Postal Code
                    span: right

The attribute must hold JSON, so give it an array cast, as the Sample model does for every structured field on these pages. See JSON attributes.

php
/**
 * casts stores the structured fields as JSON through array casts, and reads the switch and checkbox columns as booleans.
 *
 * @return array<string, string>
 */
protected function casts(): array
{
    return [
        'features' => 'array',
        'permissions' => 'array',
        'interests' => 'array',
        'tags' => 'array',
        'topics' => 'array',
        'hashtags' => 'array',
        'content' => 'array',
        'steps' => 'array',
        'address' => 'array',
        'line_items' => 'array',
        'meta' => 'array',
        'shipping' => 'array',
        'contact' => 'array',
        'faqs' => 'array',
        'highlights' => 'array',
        'milestones' => 'array',
        'dimensions' => 'array',
        'sections' => 'array',
        'specs' => 'array',
        'itinerary' => 'array',
        'languages' => 'array',
        'deliveries' => 'array',
        'variants' => 'array',
        'stockists' => 'array',
        'parts' => 'array',
        'price_breaks' => 'array',
        'bundle_items' => 'array',
        'suppliers' => 'array',
        'components' => 'array',
        'stock_levels' => 'array',
        'reviews' => 'array',
        'spec_sheet' => 'array',
        'certifications' => 'array',
        'awards' => 'array',
        'seo' => 'array',
        'packaging' => 'array',
        'is_featured' => 'boolean',
        'has_discount' => 'boolean',
    ];
}

Tabs ​

The inner form can have tabs and secondaryTabs of its own, beside or instead of its fields. Its tabs are styled as nested tabs, so they read as part of the field rather than the page.

yaml
fields:
    shipping:
        label: Shipping
        type: nestedform
        comment: The nested form brings its own tabs.
        form:
            fields:
                recipient:
                    label: Recipient
            tabs:
                fields:
                    line1:
                        label: Street
                        tab: Street
                    suburb:
                        label: Suburb
                        span: left
                        tab: Street
                    region_code:
                        label: Postal Code
                        span: right
                        tab: Street
                    instructions:
                        label: Delivery Instructions
                        type: textarea
                        size: tiny
                        tab: Delivery
                    leave_at_door:
                        label: Leave at the door
                        type: switch
                        tab: Delivery

Without the panel ​

The inner form sits in a panel by default, with a border and a background. showPanel: false drops the panel, so the inner fields line up with the rest of the form while still saving as one object.

yaml
fields:
    contact:
        label: Contact
        type: nestedform
        showPanel: false
        form:
            fields:
                person:
                    label: Person
                    span: left
                phone:
                    label: Phone
                    span: right

Relation ​

When the field name is a relation on the model, the inner form edits the related record instead of a JSON value. Here primary_group is a belongsTo relation, so the inner fields are the group's own name and code. Saving the form saves the group as well: the field saves the related record itself and posts no value of its own, so the save code has nothing to map for it.

yaml
fields:
    primary_group:
        label: Primary Group
        type: nestedform
        comment: Editing these fields changes the group itself, which saves with the sample.
        form:
            fields:
                name:
                    label: Name
                    span: left
                code:
                    label: Code
                    span: right
php
/**
 * user is the owner, picked by the relation dropdown and the record finder.
 */
public function user(): BelongsTo
{
    return $this->belongsTo(User::class);
}

/**
 * reviewer is a second user, picked by the record finder.
 */
public function reviewer(): BelongsTo
{
    return $this->belongsTo(User::class);
}

/**
 * primary_group is the group picked by the relation dropdown and edited in place by the nested form.
 */
public function primary_group(): BelongsTo
{
    return $this->belongsTo(UserGroup::class);
}

/**
 * primary_category is a category of the tree, picked by the tree dropdown and the record finder.
 */
public function primary_category(): BelongsTo
{
    return $this->belongsTo(Category::class);
}

The field suits a singular relation, such as belongsTo or hasOne. While the relation is empty, the inner fields start blank. defaultCreate: true instead creates a record as the form loads and links it to the relation, so the inner form always edits a record the relation holds. A hasOne stores the link straight away, and a belongsTo stores it when the record saves.

Preview ​

disabled renders the inner form in preview mode, and the field is left out of the save. This copy reads the address through valueFrom.

yaml
fields:
    address_preview:
        label: Address
        type: nestedform
        valueFrom: address
        disabled: true
        form:
            fields:
                recipient:
                    label: Recipient
                line1:
                    label: Street
                suburb:
                    label: Suburb
                    span: left
                region_code:
                    label: Postal Code
                    span: right

Options ​

OptionDescription
formthe inner form, with fields, tabs and secondaryTabs, inline or as the path to a YAML file.
showPanelwraps the inner form in a panel. Default: true.
defaultCreateon a relation, creates and attaches a related record when there is none. Default: false.

The field also takes the common field properties, such as disabled and valueFrom.