In structure field, if not empty, default required

Ok I am unsure how to better solve the following.

I have a structure field called “sizes”, which holds possible sizes for a product.

“Sizes” may be empty, but if it is NOT empty, then one “size” (and not more than one) must be marked as “default” somehow.

      sizes:
        label: Sizes 
        type: structure      
        fields:
          size:
            label: size
            type: text

First I tried to add a “default” toggle to the size structureItem. But then the user may select more than one default, which is a little bit confusing:

      sizes:
        label: Sizes 
        type: structure
        help: Only one size can be default                 
        fields:
          size:
            label: size
            type: text
          default: 
            label: Default?
            type: toggle
            text: 
              - "no"
              - "yes"     

…as far as I know I can’t have a trans structure items radio button. As in a radio button on each structure item, that acts as a single radio group…

So my next attempt is a separate radio field that is populated via the contents of the structure field:

      sizes:
        label: Sizes 
        type: structure       
        fields:
          size:
            label: size
            type: text   
      defaultsize:
        label: Default size
        type: radio
        options: query
        columns: 6
        query: 
          fetch: page.sizes.toStructure
          text: "{{ structureItem.size }}"
          value: "{{ structureItem.size }}"

This has two downsides.

First, after adding structure items, the user has to reload in order to see them as selectable radio buttons.

Second, as far as I understand, I cannot “require” this radio field, because 1) user adds structure items 2) user tries to save 3) can’t save, “defaultsize” is required 4) can’t select anything in “defaultsize” because it is empty 5) panic

So… how would you suggest solving this?

Could I perhaps use when then condition, in a way that “defaultsize” is only required if not empty?

Although I would still have the problem of the user having to reload the page, after filling “sizes” in order to see “defaultsize” radio options

Thanks

Hi. I would go with your structure field that holds 2 fields - a text field for inputting the size label (e.g. small) and a toggle field to mark it as default. If they mistakenly assign two sizes as default the template just assigns the last one as default. We can’t always save clients from themselves.

Yes. This isn’t 100% bulletproof but it’s still a good setup for what they need. Plus there’s a bonus when you apply a toggle to a structure item. It can be toggled inline.

1 Like

Thank you, and yes, I came to the same conclusion. There does not seem to be a better approach.

Thanks!