2016-01-21 16:23:35 +08:00
|
|
|
# Input 表单域校验
|
|
|
|
|
|
|
|
- order: 11
|
|
|
|
|
|
|
|
基本的表单校验例子。
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
````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;
|
|
|
|
}
|
|
|
|
|
|
|
|
class BasicDemo extends React.Component {
|
|
|
|
getValidateStatus(field) {
|
|
|
|
const { isFieldValidating, getFieldError, getFieldValue } = this.props.form;
|
|
|
|
|
|
|
|
if (isFieldValidating(field)) {
|
|
|
|
return 'validating';
|
|
|
|
} else if (!!getFieldError(field)) {
|
|
|
|
return 'error';
|
|
|
|
} else if (getFieldValue(field)) {
|
|
|
|
return 'success';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleReset(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.props.form.resetFields();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmit(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.props.form.validateFields(function(errors, values) {
|
|
|
|
if (!!errors) {
|
|
|
|
console.log('Errors in form!!!');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log('Submit!!!');
|
|
|
|
console.log(values);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
userExists(rule, value, callback) {
|
|
|
|
if (!value) {
|
|
|
|
callback();
|
|
|
|
} else {
|
|
|
|
setTimeout(function () {
|
|
|
|
if (value === 'JasonWood') {
|
|
|
|
callback([new Error('抱歉,该用户名已被占用。')]);
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}, 800);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkPass(rule, value, callback) {
|
|
|
|
const { validateFields } = this.props.form;
|
|
|
|
if (value) {
|
|
|
|
validateFields(['rePasswd']);
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
checkPass2(rule, value, callback) {
|
|
|
|
const { getFieldValue } = this.props.form;
|
|
|
|
if (value && value !== getFieldValue('passwd')) {
|
|
|
|
callback('两次输入密码不一致!');
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { isFieldValidating, getFieldError, getFieldProps } = this.props.form;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Form horizontal form={this.props.form}>
|
|
|
|
<FormItem
|
|
|
|
label="用户名:"
|
|
|
|
id="name"
|
|
|
|
labelCol={{span: 7}}
|
|
|
|
wrapperCol={{span: 12}}
|
|
|
|
hasFeedback
|
|
|
|
name="name"
|
|
|
|
options={{
|
|
|
|
rules: [
|
|
|
|
{required: true, min: 5, message: '用户名至少为 5 个字符'},
|
|
|
|
{validator: this.userExists},
|
|
|
|
],
|
|
|
|
}}
|
|
|
|
help={isFieldValidating('name') ? '正在校验中..' : (getFieldError('name') || []).join(', ')}>
|
|
|
|
<Input id="name" placeholder="实时校验,输入 JasonWood 看看" />
|
|
|
|
</FormItem>
|
|
|
|
|
|
|
|
<FormItem
|
|
|
|
label="邮箱:"
|
|
|
|
id="email"
|
|
|
|
labelCol={{span: 7}}
|
|
|
|
wrapperCol={{span: 12}}
|
|
|
|
hasFeedback
|
|
|
|
name="email"
|
|
|
|
options={{
|
|
|
|
validate: [{
|
|
|
|
rules: [
|
|
|
|
{required: true},
|
|
|
|
],
|
|
|
|
trigger: 'onBlur',
|
|
|
|
}, {
|
|
|
|
rules: [
|
|
|
|
{type: 'email', message: '请输入正确的邮箱地址'},
|
|
|
|
],
|
|
|
|
trigger: ['onBlur', 'onChange'],
|
|
|
|
}]
|
|
|
|
}}>
|
|
|
|
<Input type="email" id="email" placeholder="onBlur 与 onChange 相结合" />
|
|
|
|
</FormItem>
|
|
|
|
|
|
|
|
<FormItem
|
|
|
|
label="密码:"
|
|
|
|
id="password"
|
|
|
|
labelCol={{span: 7}}
|
|
|
|
wrapperCol={{span: 12}}
|
|
|
|
validateStatus={this.getValidateStatus('passwd')}
|
|
|
|
hasFeedback
|
|
|
|
help={(getFieldError('passwd') || []).join(', ')}
|
|
|
|
required>
|
|
|
|
<Input {...getFieldProps('passwd', {
|
|
|
|
rules: [
|
|
|
|
{ required: true, whitespace: true, message: '请填写密码' },
|
|
|
|
{ validator: this.checkPass.bind(this) },
|
|
|
|
],
|
|
|
|
})} id="password" type="password" autoComplete="off"
|
|
|
|
onContextMenu={noop} onPaste={noop} onCopy={noop} onCut={noop}/>
|
|
|
|
</FormItem>
|
|
|
|
|
|
|
|
<FormItem
|
|
|
|
label="确认密码:"
|
|
|
|
id="password2"
|
|
|
|
labelCol={{span: 7}}
|
|
|
|
wrapperCol={{span: 12}}
|
|
|
|
validateStatus={this.getValidateStatus('rePasswd')}
|
|
|
|
hasFeedback
|
|
|
|
help={(getFieldError('rePasswd') || []).join(', ')}
|
|
|
|
required>
|
|
|
|
<Input {...getFieldProps('rePasswd', {
|
|
|
|
rules: [{
|
|
|
|
required: true,
|
|
|
|
whitespace: true,
|
|
|
|
message: '请再次输入密码',
|
|
|
|
}, {
|
|
|
|
validator: this.checkPass2.bind(this),
|
|
|
|
}],
|
|
|
|
})} id="password2" type="password" autoComplete="off" placeholder="两次输入密码保持一致"
|
|
|
|
onContextMenu={noop} onPaste={noop} onCopy={noop} onCut={noop} />
|
|
|
|
</FormItem>
|
|
|
|
|
|
|
|
<FormItem
|
|
|
|
label="备注:"
|
|
|
|
id="textarea"
|
|
|
|
labelCol={{span: 7}}
|
|
|
|
wrapperCol={{span: 12}}
|
|
|
|
validateStatus={this.getValidateStatus('textarea')}
|
|
|
|
help={(getFieldError('textarea') || []).join(', ')}
|
|
|
|
required>
|
|
|
|
<Input {...getFieldProps('textarea', {
|
|
|
|
rules: [
|
|
|
|
{required: true, message: '真的不打算写点什么吗?'},
|
|
|
|
],
|
|
|
|
})} type="textarea" placeholder="随便写" id="textarea" name="textarea" />
|
|
|
|
</FormItem>
|
|
|
|
|
|
|
|
<FormItem wrapperCol={{span: 12, offset: 7}} >
|
|
|
|
<Button type="primary" onClick={this.handleSubmit.bind(this)}>确定</Button>
|
|
|
|
|
|
|
|
<Button type="ghost" onClick={this.handleReset.bind(this)}>重置</Button>
|
|
|
|
</FormItem>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BasicDemo = createForm({})(BasicDemo);
|
|
|
|
|
|
|
|
ReactDOM.render(<BasicDemo />, mountNode);
|
|
|
|
````
|