react-hook-form-mui icon indicating copy to clipboard operation
react-hook-form-mui copied to clipboard

Boolean option for RadioButtonGroup

Open smeet2296 opened this issue 1 year ago • 4 comments

Duplicates

  • [X] I have searched the existing issues

Latest version

  • [X] I have tested the latest version

Summary 💡

Currently RadioButtonGroup Element supports string and number. But if I want to use a Yes/No radio button which should store true/false its not working.

Example optionsArray : [{"id":true,"value":"Yes"},{"id":false,"value":"No"}]

If for reducing complexity there is a way that the options have string true/false values and then the value gets stored as boolean that would also be fine

Examples 🌈

No response

smeet2296 avatar Jul 30 '24 12:07 smeet2296

any PR is welcome

dohomi avatar Jul 30 '24 14:07 dohomi

@dohomi

I found a workaround

For the example optionsArray mentioned above, we can do the following

transform={{ input: (value) => { return value != null ? value : ""; }, output: (_, value) => { return value === "true" ? true : value === "false" ? false : ""; }, }}

The only problem is https://github.com/react-hook-form/react-hook-form/issues/2323

For this, we can avoid using required in rule as well as prop and use validate function in the rule instead. We will have to add the asterisk in the label manually.

I wanted to add boolean type inbuilt but then due to the above mentioned git issue, required rule and prop will stop working

smeet2296 avatar Jul 31 '24 09:07 smeet2296

I really think you make yourself too much trouble: either use a checkbox / switch which handle true/false natively or use "0" "1" and just cast the value shortly before you persist it in the database inside your handleSubmit. I am currently loaded with work that I won't spend time on the issue. If you find something usable I am happy to review a PR

dohomi avatar Jul 31 '24 13:07 dohomi

Yes we can do that if there is only a form that we are building. But I am building a form configurator and I cannot have such restrictions that a user cannot configure boolean values through it. The above solution works for now.

smeet2296 avatar Aug 01 '24 05:08 smeet2296