1.antd里form表单需要点击提交按钮后才校验,如果想在这之前校验,可以通过获取值自定义校验是否满足。
// 点击提交前校验form表单是否都通过
hasErrors =()=>{
const {photosList} = this.state //photosList为上传图片的数量
const fieldsValue = this.props.form.getFieldsValue();
const errArr = Object.keys(fieldsValue).filter(item =>
{
return (['undefined', '', 'null', '[]'].includes(('' + fieldsValue[item]).trim()) == true)&&item!='noNeedKeyName'&&item!='photos'
//其中noNeedKeyName:不要需要校验的keyName。photos:为图片的keyName
})
return errArr.length>0?true:photosList.length==0?true:false;
}
<Button type="primary" htmlType="submit"
disabled = {this.hasErrors()}
onClick = {()=>{this.onSubmit()}}>
提交
</Button>