2016-03-31 09:40:55 +08:00
|
|
|
---
|
|
|
|
order: 11
|
2016-08-23 21:00:35 +08:00
|
|
|
title:
|
2016-07-31 09:53:51 +08:00
|
|
|
zh-CN: 表单校验
|
|
|
|
en-US: Basic validate
|
2016-03-31 09:40:55 +08:00
|
|
|
---
|
2016-01-21 16:23:35 +08:00
|
|
|
|
2016-07-31 09:53:51 +08:00
|
|
|
## zh-CN
|
|
|
|
|
2016-01-21 16:23:35 +08:00
|
|
|
基本的表单校验例子。
|
|
|
|
|
2016-07-31 09:53:51 +08:00
|
|
|
## en-US
|
|
|
|
|
|
|
|
Basic validatation for form.
|
|
|
|
|
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-08-23 21:00:35 +08:00
|
|
|
if (errors) {
|
2016-01-21 16:23:35 +08:00
|
|
|
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') {
|
2016-07-31 09:53:51 +08:00
|
|
|
callback([new Error('Sorry, the user name is already in use.')]);
|
2016-01-21 16:23:35 +08:00
|
|
|
} 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')) {
|
2016-07-31 09:53:51 +08:00
|
|
|
callback('The two passwords you enter is inconsistent!');
|
2016-01-21 16:23:35 +08:00
|
|
|
} 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: [
|
2016-07-31 09:53:51 +08:00
|
|
|
{ required: true, min: 5, message: 'User name for at least 5 characters' },
|
2016-02-25 14:34:31 +08:00
|
|
|
{ validator: this.userExists },
|
|
|
|
],
|
|
|
|
});
|
|
|
|
const emailProps = getFieldProps('email', {
|
|
|
|
validate: [{
|
|
|
|
rules: [
|
|
|
|
{ required: true },
|
|
|
|
],
|
|
|
|
trigger: 'onBlur',
|
|
|
|
}, {
|
|
|
|
rules: [
|
2016-07-31 09:53:51 +08:00
|
|
|
{ type: 'email', message: 'Please input the correct email' },
|
2016-02-25 14:34:31 +08:00
|
|
|
],
|
|
|
|
trigger: ['onBlur', 'onChange'],
|
2016-05-11 09:32:33 +08:00
|
|
|
}],
|
2016-02-25 14:34:31 +08:00
|
|
|
});
|
|
|
|
const passwdProps = getFieldProps('passwd', {
|
|
|
|
rules: [
|
2016-07-31 09:53:51 +08:00
|
|
|
{ required: true, whitespace: true, message: 'Please enter your password' },
|
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,
|
2016-07-31 09:53:51 +08:00
|
|
|
message: 'Please confirm your password',
|
2016-02-25 14:34:31 +08:00
|
|
|
}, {
|
2016-03-30 17:06:19 +08:00
|
|
|
validator: this.checkPass2,
|
2016-02-25 14:34:31 +08:00
|
|
|
}],
|
|
|
|
});
|
|
|
|
const textareaProps = getFieldProps('textarea', {
|
|
|
|
rules: [
|
2016-07-31 09:53:51 +08:00
|
|
|
{ required: true, message: 'Really not supposed to write something?' },
|
2016-02-25 14:34:31 +08:00
|
|
|
],
|
|
|
|
});
|
|
|
|
const formItemLayout = {
|
|
|
|
labelCol: { span: 7 },
|
|
|
|
wrapperCol: { span: 12 },
|
|
|
|
};
|
2016-01-21 16:23:35 +08:00
|
|
|
return (
|
2016-07-29 13:59:37 +08:00
|
|
|
<Form horizontal form={this.props.form}>
|
2016-01-21 16:23:35 +08:00
|
|
|
<FormItem
|
2016-02-25 14:34:31 +08:00
|
|
|
{...formItemLayout}
|
2016-07-31 09:53:51 +08:00
|
|
|
label="User name"
|
2016-02-01 10:23:06 +08:00
|
|
|
hasFeedback
|
2016-07-31 09:53:51 +08:00
|
|
|
help={isFieldValidating('name') ? 'validating...' : (getFieldError('name') || []).join(', ')}
|
2016-06-06 13:54:10 +08:00
|
|
|
>
|
2016-07-31 09:53:51 +08:00
|
|
|
<Input {...nameProps} placeholder="Real-tiem validation, try to input JasonWood" />
|
2016-01-21 16:23:35 +08:00
|
|
|
</FormItem>
|
|
|
|
|
|
|
|
<FormItem
|
2016-02-25 14:34:31 +08:00
|
|
|
{...formItemLayout}
|
2016-07-31 09:53:51 +08:00
|
|
|
label="Email"
|
2016-06-06 13:54:10 +08:00
|
|
|
hasFeedback
|
|
|
|
>
|
2016-07-31 09:53:51 +08:00
|
|
|
<Input {...emailProps} type="email" placeholder="This control uses onBlur and onChange" />
|
2016-01-21 16:23:35 +08:00
|
|
|
</FormItem>
|
|
|
|
|
|
|
|
<FormItem
|
2016-02-25 14:34:31 +08:00
|
|
|
{...formItemLayout}
|
2016-07-31 09:53:51 +08:00
|
|
|
label="Password"
|
2016-06-06 13:54:10 +08:00
|
|
|
hasFeedback
|
|
|
|
>
|
2016-02-25 14:34:31 +08:00
|
|
|
<Input {...passwdProps} type="password" autoComplete="off"
|
2016-06-06 13:54:10 +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-07-31 09:53:51 +08:00
|
|
|
label="Confirm password"
|
2016-06-06 13:54:10 +08:00
|
|
|
hasFeedback
|
|
|
|
>
|
2016-07-31 09:53:51 +08:00
|
|
|
<Input {...rePasswdProps} type="password" autoComplete="off" placeholder="Two passwords that you enter must be consistent"
|
2016-06-06 13:54:10 +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-07-31 09:53:51 +08:00
|
|
|
label="remark"
|
2016-06-06 13:54:10 +08:00
|
|
|
>
|
2016-07-31 09:53:51 +08:00
|
|
|
<Input {...textareaProps} type="textarea" placeholder="Please write something" 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-07-31 09:53:51 +08:00
|
|
|
<Button type="primary" onClick={this.handleSubmit}>OK</Button>
|
2016-01-21 16:23:35 +08:00
|
|
|
|
2016-07-31 09:53:51 +08:00
|
|
|
<Button type="ghost" onClick={this.handleReset}>Reset</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
|
|
|
````
|