Material-ui: How to underline the TextField only if an error occurs ?

Created on 12 Mar 2018  路  5Comments  路  Source: mui-org/material-ui

TextFields in v0.20.0 used to have an "ErrorText" property, which was very useful to display an error under a textfield and underline it in red at the same time.

<TextField
      hintText="Hint Text"
      errorText="This field is required"
      floatingLabelText="Floating Label Text"
/>

However, TextFields in material-ui@next don't have that property. In order to display an error and underline in red, a much more complex alternative is used

<FormControl className={classes.formControl} error aria-describedby="name-error-text">
          <InputLabel htmlFor="name-error">Name</InputLabel>
          <Input id="name-error" value={this.state.name} onChange={this.handleChange} />
          <FormHelperText id="name-error-text">Error</FormHelperText>
</FormControl>

Is this the only option ? Is there any other way to achieve it ?

question

All 5 comments

@ArthurAttout You need to use the helperText property of the TextField.

@oliviertassinari I tried to use helperText property of the TextField to display an error. But, the error shown just normal color. I mean, how to show error with the red color?

@ras24 can you post your Textfield ? Mine worked perfectly

@ArthurAttout I tried using material-ui@next

  <TextField
       className={classes.textField}
       name='name'
       label='Name'
       placeholder='Type your name'
       value={this.state.name}
       onChange={this.onChange}
       helperText={this.state.error}
  />

If error occur, helperText just shown error messege with the gray color not a red color.

@ras24 I think there should be an error boolean prop which you can set. Which changes the color of the helperText to red <TextField error />

Was this page helpful?
0 / 5 - 0 ratings

Related issues

reflog picture reflog  路  3Comments

mb-copart picture mb-copart  路  3Comments

chris-hinds picture chris-hinds  路  3Comments

sys13 picture sys13  路  3Comments

finaiized picture finaiized  路  3Comments