[英]React ignore required on form input
我尝试验证表单输入,以便您无法在未完成描述文本输入的情况下提交表单
<input placeholder="Description" type='text' name="description" className="form-control" value={this.state.description} onChange={this.changeDescriptionHandler} required />
但是当我提交表单时,它让我用空输入提交它,即使我指定了“必需”。 可能是什么问题呢?
PS,状态看起来像这样
this.state = {
description: '',
name: '',
birthDate: '',
gender: 'MALE'
}
和提交
saveCasefile = (e) => {
e.preventDefault();
let patient = { name: this.state.name, birthDate: this.state.birthDate, gender: this.state.gender }
let casefile = { description: this.state.description, patient: patient };
CaseFilesService.createCasefile(casefile).then(res => {
this.props.history.push('/');
})
}
这个问题肯定需要更多的代码。 您的初始状态可能具有某种价值,并且正在传递。
在提交处理函数中设置条件。
if(this.state.description !== ''){ .... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.