![](/img/trans.png)
[英]How to add required to input field and change the message error when it's empty?
[英]How to show error message when field is required and it's value is empty?
我正在使用此插件来验证我的表单。 当发生.click时,我想在button
上显示错误消息。 在我的表单中,所有字段都是required
字段,因此我需要验证我的表单。
我尝试使用下面的代码,但不起作用
<FormControl className={classes.formControl}>
<TextValidator
required
InputLabelProps={{
shrink: true
}}
id="name"
label="search value"
name="searchValue"
value={searchValue}
onChange={event => handleInput(event, "searchValue")}
validators={["required"]}
errorMessages={["this field is required"]}
margin="normal"
/>
</FormControl>
这是我的代码https://codesandbox.io/s/l40l795vx7
<SearchForm
handleInput={this.handleInputFieldChange}
submitClick={this.submitButtonClickHandle}
form={form}
/>
我不确定您要在其中验证的内容,因为我对插件不熟悉。 但是,如果表单无效,则很容易显示错误。
您已经在state
保存了来自inputs
的values
,因此在submitButtonClickHandle
函数中要做的所有事情都类似于:
if(this.state.form.searchValue !== '**some conditions**' &&
this.state.form.circle !== '**some condition**'){
//
alert('You did not meet the search criteria')
} else {
// DO WHAT YOU WANT IF ON THE SUBMIT
alert('everything is fine, here is your result')
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.