form表單自定義調介面校驗
阿新 • • 發佈:2020-07-10
// 校驗角色名字唯一 getName = (rule, value, callback) => { const { dispatch } = this.props; const { recodes } = this.state; if (value) { //這裡調後端介面 dispatch({ type: 'roleModel/getName', payload: { roleId: recodes ? recodes.roleId : '', roleName: value } }) .then((xhr)=>{ if(!xhr){ callback('該角色名稱已存在');//拋發異常資訊 }else{ callback()//必須返回,聯通表單err } }) }else{ callback()//返回聯通必填校驗 } } <FormItem {...formItemLayout} label="角色名稱" > {form.getFieldDecorator('roleNames', { initialValue: recodes?recodes.roleName:'', rules: [ { required: true, message: '請填寫角色名稱' }, { max: 30, message: '最長可輸入30個字元' }, { validator: this.getName } ] })( <Input /> )} </FormItem>