【发布时间】:2021-01-19 12:31:29
【问题描述】:
这里显示表单字段的表单项是我的代码
import { Form, Input, Button, Select, } from 'antd';
import axios from "axios";
render() {
const isLoading = this.state.isLoading;
return (
<>
<Form className={'auth-form'} onSubmit={(e) => { this.onSubmit(e) }}>
<h3 className={'mb-5 text-center'}>Candidate Sign Up</h3>
<Form.Item
label=""
name="email"
rules={[{ required: true, type: 'email', message: 'Please enter email address'}]}
>
<Input className={'ks-form-control'} placeholder={'Enter Email'} onChange={this.onChangehandler} />
</Form.Item>
<Form.Item
label=""
name="password"
rules={[{ required: true, message: 'Please input your password!' }]}
>
<Input.Password className={'ks-form-control'} placeholder={'Password'} />
</Form.Item>
<Form.Item
label=""
name="confirmPassword"
rules={[{ required: true, message: 'Please confirm your password!' }]}
>
<Input.Password className={'ks-form-control'} placeholder={'Confirm Password'} />
</Form.Item>
<Form.Item >
<Button className={'btn-custom px-4 py-2 d-block w-100'} type="primary" htmlType="submit">
Create an account
</Button>
</Form.Item>
</Form>
</>
)
}
这里是提交处理程序的代码。我想在 rules={[{}]}
处显示来自 api 的消息和使用以下代码的自定义消息msg: response.data.message
提交处理程序
onSubmitHandler = (e) => {
e.preventDefault();
this.setState({ isLoading: true });
axios
.post("http://127.0.0.1:8000/api/user-signup", this.state.signupData)
.then((response) => {
this.setState({ isLoading: false });
if (response.data.status === 200) {
this.setState({
msg: response.data.message // message comming from api
});
}
if (response.data.status === "failed") {
this.setState({ msg: response.data.message }); // message comming from api
}
});
}
所有字段的验证都与 rules={[]} 一起正常工作。但是我想根据 api 响应显示错误,例如如果电子邮件已经注册,那么这将显示消息“电子邮件已存在” 请让我知道我该怎么做
【问题讨论】:
标签: reactjs