ant-design/components/form/demo/validate-basic.md

184 lines
4.6 KiB
Markdown
Raw Normal View History

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
2016-09-01 18:12:12 +08:00
Basic validation for form.
2016-07-31 09:53:51 +08:00
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() {
const { getFieldDecorator, getFieldError, isFieldValidating } = this.props.form;
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 (
<Form horizontal>
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(', ')}
>
{getFieldDecorator('name', {
rules: [
{ required: true, min: 5, message: 'User name for at least 5 characters' },
{ validator: this.userExists },
],
})(
<Input 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"
hasFeedback
>
{getFieldDecorator('email', {
validate: [{
rules: [
{ required: true },
],
trigger: 'onBlur',
}, {
rules: [
{ type: 'email', message: 'Please input the correct email' },
],
trigger: ['onBlur', 'onChange'],
}],
})(
<Input 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"
hasFeedback
>
{getFieldDecorator('passwd', {
rules: [
{ required: true, whitespace: true, message: 'Please enter your password' },
{ validator: this.checkPass },
],
})(
<Input type="password" autoComplete="off"
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"
hasFeedback
>
{getFieldDecorator('rePasswd', {
rules: [{
required: true,
whitespace: true,
message: 'Please confirm your password',
}, {
validator: this.checkPass2,
}],
})(
<Input type="password" autoComplete="off" placeholder="Two passwords that you enter must be consistent"
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"
>
{getFieldDecorator('textarea', {
rules: [
{ required: true, message: 'Really not supposed to write something?' },
],
})(
<Input 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
&nbsp;&nbsp;&nbsp;
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);
````