我有一个材质 UI 文本字段,在输入某个值后,我想在自定义验证失败时设置一条错误消息。
仅当输入空白值时,我才能在错误消息中进行设置。因此,我想添加我自己的客户错误消息,而不是空白值错误消息。
const checkValue = (newValue) => {
var other = { 'a': 1 };
if(!_.isEqual(newValue, other)) {
alert('value is correct.');
//I want to set error message next to field value is not correct.
}
}
<TextField
id="form-value"
className={classes.textField}
margin="dense"
label={'value'}
value={value}
onKeyUp={e => { checkValue(e.target.value); }}
onChange={e => { checkValue(e.target.value); }}
variant="outlined"
error={value=== ""}
helperText={value=== "" ? 'Please enter a value!' : ' '}
fullWidth
>
Run Code Online (Sandbox Code Playgroud)