Unconditional Conditional Field Love

Bytespun on March 5th, 2019

In Statamic v2, it's possible to conditionally show/hide fields. We wanted to unconditionally love conditional fields, but sometimes using 'em was kinda like trying to pay for a two-four while on pogey. It was just kinda limiting, eh? Well, in Statamic 3 we've supercharged 'em.

Imagine you've got raw moose power, more operators to work with, dot notation for comparing against nested data, etc. Heck, imagine this stuff even works within replicators and grids 🔥

Like maybe you're headin' out for a rip on the old snowmobile, but you also gotta write this blog post. The snowmobile keeps overheating and melting the igloo, and you're itching to go enjoy the Great White North. So you holler for your keener neighbour and the hoser agrees to cover you for a toonie and a double double at Timmies. Beauty!

Anyways, how do we go aboot it? Let's grab the Canuck's info, only asking for his avatar if he's of legal avatar age:

-
  handle: has_custom_author
  field:
    type: toggle
-
  handle: author_info
  field:
    type: array
    keys:
      name: Name
      age: Age
      social: Social Media Handle
    if:
      has_custom_author: true
-
  handle: author_avatar
  field:
    type: assets
    container: main
    if:
      author_info.name: not empty
      author_info.age: '>= 16'

Then fire up the snowmobile to get her warmin' up, and let's test in the browser:

If you really need the extra moose power, you could define a custom javascript condition with fancier logic against any value in the VueX store. However, for our purposes this is just beauty!

Now don't be a city slickin' snowbird, take off and enjoy the Great White North. Statamic 3's got your arse!

Jesse Leite
Jesse Leite, The Canadian Developer @ Statamic
The Canadian Developer

Copyright 2019 ⚔️ Statamic