sveltia / sveltia-cms

Alternative to Netlify/Decap CMS. Fast, lightweight, Git-based headless CMS. Modern UX, first-class i18n support, open source & free. Made with Svelte.
MIT License
750 stars 34 forks source link

If `widget: object` is optional, it can't be saved empty if containing required fields #55

Closed frankbits closed 8 months ago

frankbits commented 10 months ago
      - name: "audio"
        label: "Audio"
        widget: object
        required: false
        collapsed: true
        fields:
          - { name: "src", label: "Source-URL", widget: "string" }
          - { name: "url", label: "Webpage-URL", widget: "string" }
          - { name: "img", label: "Image", widget: image, required: false }
          - { name: "descr", label: "Description", widget: "string", required: false }

src & url have to be set.

You should be able to save, if an optional object is empty

kyoshino commented 10 months ago

I was actually thinking about a slightly related thing yesterday: Do we need the “Required” label on the Object widget? And here comes the issue!

I just tested the config with Netlify/Decap CMS, which gave the same result, and found the same issue: https://github.com/decaporg/decap-cms/issues/2103

I think the current behaviour is okay because an Object field is just a grouping and conditionally bypassing nested required fields is rather confusing. A better UX might be to allow an optional Object field to be explicitly removed (and re-added if necessary) with a button, similar to a List field.

Screenshot 2023-09-07 at 8 32 49 PM

Thoughts?

frankbits commented 10 months ago

You mean, that currently it always tries to add the object, as it's not first checking for its content. And you now propose, an object-field would work like a list-field with max: 1. Sounds good.

Continuing to think in this direction, it may be possible to allow different object-types...🤔 I'm currently using a workaround for that, with widget: list where max: 1 and types: are set... don't really know how/if that could be implemented logically...😶

frankbits commented 10 months ago

but I think the object-field would still need the required-option deciding, if it can be saved if it's removed.

kyoshino commented 10 months ago

It’s a good idea to introduce variable types to the Object widget! One of my clients uses list widget variable types in Sveltia CMS and I know it works well. 👍🏻

For required: false on Object, I’d implement the Add/Remove button as proposed. And I need to rewrite the validation code, which is buggy especially on the List widget.

kyoshino commented 8 months ago

This will be shipped with the next minor release. I’ve realized that one of my clients has this use case — currently setting required: false to all the subfields to avoid validator errors, but I can remove the workaround once the fix is in 😄

kyoshino commented 8 months ago

Shipped 🚢