Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material Ui - Add required attribute to a TextField in "select" mode

I am trying to make "required" a TextField in select mode. I tried to add required prop like in this snippet, but this does not block the submit event if I haven't select anything. Although it adds the '*' to the label.

Please check this sandbox

<TextField
  id="select-currency"
  select
  label="Select"
  value={this.state.currency}
  onChange={this.handleChange("currency")}
  required
>
  {currencies.map(option => (
    <MenuItem key={option.value} value={option.value}>
      {option.label}
    </MenuItem>
  ))}
</TextField>

UPDATE: (Clarification really) I am talking about html5 validation. In the sandbox example there are Select and Text fields, setting the text field as required will block the submit event and displays a native html5 error saying "this field is required", this is not the case if the field is "select".

like image 354
Bichoy Messiha Avatar asked Sep 01 '18 22:09

Bichoy Messiha


2 Answers

Material Ui provides another component Native Select to handle this kind of native validation.

Please check this example

Edit Material demo

like image 137
Bichoy Messiha Avatar answered Oct 20 '22 16:10

Bichoy Messiha


It was recently implemented in material ui. If you upgrade @material-ui/core to version 4.11.0 it will work https://github.com/mui-org/material-ui/issues/20402

like image 28
Vincente Avatar answered Oct 20 '22 17:10

Vincente