Search code examples
alpacajs

required radio field without default selection in alpaca form


I would like to create a required alpaca radio field where initially no option is selected (I feel that having a default option would influence users). I don't want to have a "None" option selected and than validated.

I couldn't figure out how to do this looking at the documentation: http://www.alpacajs.org/docs/fields/radio.html


Solution

  • To make your radio field required you should use the option "required" on your form schema configuration.

    "schema": {
      "required": true,
      "enum": ["Vanilla", "Chocolate", "Coffee"]
    }
    

    This will tell alpaca not to add a none option to the list and forces you to select an item from the list. If you don't select any option the submit button will always be disabled because alpaca knows that the form is invalid (the default behavior of alpaca form validation)

    If you want to simuate the validation when you didn't select any option try to change the submit button name from submit to add for exemple.

    "buttons": {
       "add": {
    

    Here's a working fiddle for that.