Skip to content

Conditions & validation ​

Show or hide a tab with conditions ​

A Tab field has the usual Conditions settings (if, unless, if_any, …). When a tab's condition fails:

  • the tab disappears from the tab bar,
  • its fields are hidden too, whatever their own conditions say,
  • if it was the open tab, the first visible tab of the row opens instead.

When the condition passes again, the tab comes back.

yaml
- handle: product_type
  field:
    type: button_group
    display: Product type
    options: { physical: Physical, digital: Digital }
- handle: content_tab
  field: { type: tab, display: Content }
# …
- handle: shipping_tab
  field:
    type: tab
    display: Shipping
    if:
      product_type: 'equals physical'
- handle: weight
  field: { type: float, display: Weight (kg) }
- handle: download_tab
  field:
    type: tab
    display: Download
    if:
      product_type: 'equals digital'
- handle: download_file
  field: { type: assets, display: File, max_files: 1 }

Product type Physical: the tabs are Content, Media, Shipping and SEOProduct type Physical: the tabs are Content, Media, Shipping and SEO

Physical: a Shipping tab.

Product type Digital: the tabs are Content, Media, Download and SEOProduct type Digital: the tabs are Content, Media, Download and SEO

Digital: a Download tab instead.

Conditions on the individual fields inside a tab work as usual, including reserve space when hidden.

A tab's condition only hides its fields from view

The tab's condition is not copied to the fields inside it. Statamic still treats those fields as visible: their values are saved and validated as usual. If a field in a hidden tab has an error, the save fails and an extra toast names the field and the hidden tab, next to Statamic's own error toast. To avoid it, give required fields in a conditional tab the same condition as the tab.

Validation errors ​

When a save fails, every tab that contains a field with an error turns red and gets a dot, so editors can find the problem without clicking through all tabs. This works:

  • for nested rows: the outer tab is marked when one of its nested tabs has an error,
  • through sets: a tab that contains a Replicator, Bard or Grid field is marked when a field inside one of its sets or rows has an error,
  • inside sets: the set's own tabs are marked too.

After saving a new product without a gallery and meta tags, the Media and SEO tabs are red with a dotAfter saving a new product without a gallery and meta tags, the Media and SEO tabs are red with a dot

The Gallery (Media) and meta fields (SEO) are required.

The markers disappear as soon as the errors are fixed and saved.

Released under the MIT License. Made with fruit by kiwikiwi