Bad error when saving required toggle field

Update: I opened a GitHub issue for this, as distantnative suggested on Discord.

So here I was port some Kirby 2 code to Kirby 3 code, when I tried and save a page in the panel and it failed with a cryptic error.

Steps to Reproduce

  • Grab the starterkit
  • Edit the about.yml blueprint and add the lines from below
  • Open the About us page in the panel
  • Toggle the new toggle field on and off again
  • Click save the page
  • The form validation fails, as the only valid state of a toggle in Kirby 3, is the active state.
      certain:
        label: Are you sure?
        type: toggle
        default: false
        required: true
        text:
          - "No"
          - "Yes"

Actual Behavior

The validation error message returned form the endpoint doesn’t really make sense.

image

Expected Behavior

The validation error message explains that the toggle needs to be active.

HTTP 400 (Bad Request) response from the API endpoint

{
    "status": "error",
    "message": "Invalid form with errors",
    "code": 400,
    "exception": "Kirby\\Exception\\InvalidArgumentException",
    "key": "error.invalidArgument",
    "file": "\/kirby\/src\/Cms\/ModelWithContent.php",
    "line": 771,
    "details": {
        "certain": {
            "label": "Are you sure?",
            "message": {
                "required": "Invalid argument \"-\" in method \"-\""
            }
        }
    },
    "route": "pages\/([a-zA-Z0-9\\.\\-_%= \\+\\@\\(\\)]+)"
}

Hm, somehow a required state doesn’t make sense for a toggle field? This is the validator:

'validations' => [
        'boolean',
        'required' => function ($value) {
            if ($this->isRequired() && ($value === false || $this->isEmpty($value))) {
                throw new InvalidArgumentException([
                    'key' => 'form.field.required'
                ]);
            }
        },
    ]

So that means that if you require the field and the value is not set or empty, you get this error message.

It’s certainly questionable, since as I mentioned, the only valid state is to have it set. So you could never save a page where you didn’t set the toggle…

The behavior in Kirby 2 was different, as I simply copied that required field property from my old blueprint. Honestly don’t remember why I added it though :smiley:

I’m fine with either option a) removing the required property or b) fixing the validation error text.