Is there any way to make React Material-UI radio buttons required
Asked Answered
S

2

7

I'm new at React JS. currently I'm using Material-UI for building small reusable components. Is there any way to make Material-UI radio buttons required?

like an input field showing "This field is required" text when empty

Stevens answered 25/2, 2019 at 9:30 Comment(0)
V
12

You can use name property and add the required property to radio component (change the values to your value property):

<RadioGroup name="nameRadio" value={''}>
    <FormControlLabel
          value={'value1'}
          control={<Radio required={true} />}
          label={'Label 1'}
        />
     <FormControlLabel
          value={'value2'}
          control={<Radio required={true} />}
          label={'Label 2'}
        />
  </RadioGroup>
Vaughnvaught answered 3/3, 2020 at 2:57 Comment(0)
E
-3

Assuming that you have multiple radio buttons, you can make one of them selected by default. In the end, radio buttons indicate a choice, the user must choose one of them.

If this does not feel right to you, you can do custom validation.

Edmundson answered 25/2, 2019 at 10:0 Comment(1)
Not sure why this is the accepted answer. @Vaughnvaught provides exactly what was asked for, without having to do "custom validation".Barabbas

© 2022 - 2024 — McMap. All rights reserved.