2016-03-31 09:40:55 +08:00
|
|
|
---
|
|
|
|
order: 11
|
|
|
|
title: 表单校验
|
|
|
|
---
|
2016-01-21 16:23:35 +08:00
|
|
|
|
|
|
|
基本的表单校验例子。
|
|
|
|
|
|
|
|
````jsx
|
|
|
|
import { Button, Form, Input } from 'antd';
|
2016-01-22 14:31:08 +08:00
|
|
|
const createForm = Form.create;
|
2016-01-21 16:23:35 +08:00
|
|
|
const FormItem = Form.Item;
|
|
|
|
|
|
|
|
function noop() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-30 17:06:19 +08:00
|
|
|
let BasicDemo = React.createClass({
|
2016-01-21 16:23:35 +08:00
|
|
|
handleReset(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.props.form.resetFields();
|
2016-03-30 17:06:19 +08:00
|
|
|
},
|
2016-01-21 16:23:35 +08:00
|
|
|
|
|
|
|
handleSubmit(e) {
|
|
|
|
e.preventDefault();
|
2016-01-23 16:18:22 +08:00
|
|
|
this.props.form.validateFields((errors, values) => {
|
2016-01-21 16:23:35 +08:00
|
|
|
if (!!errors) {
|
|
|
|
console.log('Errors in form!!!');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log('Submit!!!');
|
|
|
|
console.log(values);
|
|
|
|
});
|
2016-03-30 17:06:19 +08:00
|
|
|
},
|
2016-01-21 16:23:35 +08:00
|
|
|
|
|
|
|
userExists(rule, value, callback) {
|
|
|
|
if (!value) {
|
|
|
|
callback();
|
|
|
|
} else {
|
2016-01-23 16:18:22 +08:00
|
|
|
setTimeout(() => {
|
2016-01-21 16:23:35 +08:00
|
|
|
if (value === 'JasonWood') {
|
|
|
|
callback([new Error('抱歉,该用户名已被占用。')]);
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}, 800);
|
|
|
|
}
|
2016-03-30 17:06:19 +08:00
|
|
|
},
|
2016-01-21 16:23:35 +08:00
|
|
|
|
|
|
|
checkPass(rule, value, callback) {
|
|
|
|
const { validateFields } = this.props.form;
|
|
|
|
if (value) {
|
2016-03-23 14:59:01 +08:00
|
|
|
validateFields(['rePasswd'], { force: true });
|
2016-01-21 16:23:35 +08:00
|
|
|
}
|
|
|
|
callback();
|
2016-03-30 17:06:19 +08:00
|
|
|
},
|
2016-01-21 16:23:35 +08:00
|
|
|
|
|
|
|
checkPass2(rule, value, callback) {
|
|
|
|
const { getFieldValue } = this.props.form;
|
|
|
|
if (value && value !== getFieldValue('passwd')) {
|
|
|
|
callback('两次输入密码不一致!');
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
2016-03-30 17:06:19 +08:00
|
|
|
},
|
2016-01-21 16:23:35 +08:00
|
|
|
|
|
|
|
render() {
|
2016-02-01 10:23:06 +08:00
|
|
|
const { getFieldProps, getFieldError, isFieldValidating } = this.props.form;
|
2016-02-25 14:34:31 +08:00
|
|
|
const nameProps = getFieldProps('name', {
|
|
|
|
rules: [
|
|
|
|
{ required: true, min: 5, message: '用户名至少为 5 个字符' },
|
|
|
|
{ validator: this.userExists },
|
|
|
|
],
|
|
|
|
});
|
|
|
|
const emailProps = getFieldProps('email', {
|
|
|
|
validate: [{
|
|
|
|
rules: [
|
|
|
|
{ required: true },
|
|
|
|
],
|
|
|
|
trigger: 'onBlur',
|
|
|
|
}, {
|
|
|
|
rules: [
|
|
|
|
{ type: 'email', message: '请输入正确的邮箱地址' },
|
|
|
|
],
|
|
|
|
trigger: ['onBlur', 'onChange'],
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
const passwdProps = getFieldProps('passwd', {
|
|
|
|
rules: [
|
|
|
|
{ required: true, whitespace: true, message: '请填写密码' },
|
2016-03-30 17:06:19 +08:00
|
|
|
{ validator: this.checkPass },
|
2016-02-25 14:34:31 +08:00
|
|
|
],
|
|
|
|
});
|
|
|
|
const rePasswdProps = getFieldProps('rePasswd', {
|
|
|
|
rules: [{
|
|
|
|
required: true,
|
|
|
|
whitespace: true,
|
|
|
|
message: '请再次输入密码',
|
|
|
|
}, {
|
2016-03-30 17:06:19 +08:00
|
|
|
validator: this.checkPass2,
|
2016-02-25 14:34:31 +08:00
|
|
|
}],
|
|
|
|
});
|
|
|
|
const textareaProps = getFieldProps('textarea', {
|
|
|
|
rules: [
|
|
|
|
{ required: true, message: '真的不打算写点什么吗?' },
|
|
|
|
],
|
|
|
|
});
|
|
|
|
const formItemLayout = {
|
|
|
|
labelCol: { span: 7 },
|
|
|
|
wrapperCol: { span: 12 },
|
|
|
|
};
|
2016-01-21 16:23:35 +08:00
|
|
|
return (
|
|
|
|
<Form horizontal form={this.props.form}>
|
|
|
|
<FormItem
|
2016-02-25 14:34:31 +08:00
|
|
|
{...formItemLayout}
|
2016-01-21 16:23:35 +08:00
|
|
|
label="用户名:"
|
2016-02-01 10:23:06 +08:00
|
|
|
hasFeedback
|
|
|
|
help={isFieldValidating('name') ? '校验中...' : (getFieldError('name') || []).join(', ')}>
|
2016-02-25 14:34:31 +08:00
|
|
|
<Input {...nameProps} placeholder="实时校验,输入 JasonWood 看看" />
|
2016-01-21 16:23:35 +08:00
|
|
|
</FormItem>
|
|
|
|
|
|
|
|
<FormItem
|
2016-02-25 14:34:31 +08:00
|
|
|
{...formItemLayout}
|
2016-01-21 16:23:35 +08:00
|
|
|
label="邮箱:"
|
2016-01-30 19:00:56 +08:00
|
|
|
hasFeedback>
|
2016-02-25 14:34:31 +08:00
|
|
|
<Input {...emailProps} type="email" placeholder="onBlur 与 onChange 相结合" />
|
2016-01-21 16:23:35 +08:00
|
|
|
</FormItem>
|
|
|
|
|
|
|
|
<FormItem
|
2016-02-25 14:34:31 +08:00
|
|
|
{...formItemLayout}
|
2016-01-21 16:23:35 +08:00
|
|
|
label="密码:"
|
2016-01-30 19:00:56 +08:00
|
|
|
hasFeedback>
|
2016-02-25 14:34:31 +08:00
|
|
|
<Input {...passwdProps} type="password" autoComplete="off"
|
2016-02-22 10:52:30 +08:00
|
|
|
onContextMenu={noop} onPaste={noop} onCopy={noop} onCut={noop} />
|
2016-01-21 16:23:35 +08:00
|
|
|
</FormItem>
|
|
|
|
|
|
|
|
<FormItem
|
2016-02-25 14:34:31 +08:00
|
|
|
{...formItemLayout}
|
2016-01-21 16:23:35 +08:00
|
|
|
label="确认密码:"
|
2016-01-30 19:00:56 +08:00
|
|
|
hasFeedback>
|
2016-02-25 14:34:31 +08:00
|
|
|
<Input {...rePasswdProps} type="password" autoComplete="off" placeholder="两次输入密码保持一致"
|
|
|
|
onContextMenu={noop} onPaste={noop} onCopy={noop} onCut={noop} />
|
2016-01-21 16:23:35 +08:00
|
|
|
</FormItem>
|
|
|
|
|
|
|
|
<FormItem
|
2016-02-25 14:34:31 +08:00
|
|
|
{...formItemLayout}
|
|
|
|
label="备注:">
|
|
|
|
<Input {...textareaProps} type="textarea" placeholder="随便写" id="textarea" name="textarea" />
|
2016-01-21 16:23:35 +08:00
|
|
|
</FormItem>
|
|
|
|
|
2016-02-25 14:34:31 +08:00
|
|
|
<FormItem wrapperCol={{ span: 12, offset: 7 }}>
|
2016-03-30 17:06:19 +08:00
|
|
|
<Button type="primary" onClick={this.handleSubmit}>确定</Button>
|
2016-01-21 16:23:35 +08:00
|
|
|
|
2016-03-30 17:06:19 +08:00
|
|
|
<Button type="ghost" onClick={this.handleReset}>重置</Button>
|
2016-01-21 16:23:35 +08:00
|
|
|
</FormItem>
|
|
|
|
</Form>
|
|
|
|
);
|
2016-03-30 17:06:19 +08:00
|
|
|
},
|
|
|
|
});
|
2016-01-21 16:23:35 +08:00
|
|
|
|
2016-01-30 19:00:56 +08:00
|
|
|
BasicDemo = createForm()(BasicDemo);
|
2016-01-21 16:23:35 +08:00
|
|
|
|
|
|
|
ReactDOM.render(<BasicDemo />, mountNode);
|
2016-01-29 10:32:52 +08:00
|
|
|
````
|