jsonform / jsonform

Build forms from JSON Schema. Easily template-able. Compatible with Bootstrap 3 out of the box.
https://jsonform.github.io/jsonform/playground/index.html
MIT License
2.72k stars 553 forks source link

Boolean options are always true #442

Open radiomime opened 4 months ago

radiomime commented 4 months ago

What I want to happen

I want two radio buttons to from, and I'd like the text in the box to be different than the value being sent. I'd also like the box to be preselected if it was presubmitted (by using the value key).

Issue

No matter which button I select before submitting, the value sent is true in this example:

{
  "schema": {
    "general": {
      "type": "object",
      "properties": {
        "is_cool": {
          "title": "Are you cool?",
          "type": "boolean"
        }
      }
    }
  },
  "form": [
    {
      "type": "fieldset",
      "title": "<b>General</b>",
      "items": [
        {
          "key": "general.is_cool",
          "type": "radiobuttons",
          "activeClass": "btn-success",
          "options": [
            {
              "title": "I'm not cool",
              "value": false
            },
            {
              "title": "I am, indeed, cool",
              "value": true
            }
          ]
        }
      ]
    },
    {
      "type": "submit",
      "title": "Submit"
    }
  ],
  "value": {
    "general": {
      "is_cool": true
      }
  }
}

The value sent should be false when selecting "I'm not cool" and hitting submit

What I'd expect

This exact example works as I'd expect using strings instead of booleans:

{
  "schema": {
    "general": {
      "type": "object",
      "properties": {
        "is_cool": {
          "title": "Are you cool?",
          "type": "string"
        }
      }
    }
  },
  "form": [
    {
      "type": "fieldset",
      "title": "<b>General</b>",
      "items": [
        {
          "key": "general.is_cool",
          "type": "radiobuttons",
          "activeClass": "btn-success",
          "options": [
            {
              "title": "I'm not cool",
              "value": "false"
            },
            {
              "title": "I am, indeed, cool",
              "value": "true"
            }
          ]
        }
      ]
    },
    {
      "type": "submit",
      "title": "Submit"
    }
  ],
  "value": {
    "general": {
      "is_cool": "true"
      }
  }
}
sdetweil commented 4 months ago

you might try "submit": true instead of "value":true

the doc says options are for string values not boolean

sdetweil commented 4 months ago

Screenshot_20231219_081615_Chrome

radiomime commented 4 months ago

Thanks for the reply @sdetweil.

I templated the above schema (with the value key) from this part of the projects wiki which gives a similar example, but with strings instead of boolean values.

If I don't include "value": true, both submitted values false. This is also the case if I use "submit": true instead.

sdetweil commented 4 months ago

ok, I'll look at it. the doc says "strings"

sdetweil commented 3 months ago

at this time, this is currently a restriction, only works with string entities..