2015-11-03 23:34:51 +08:00
|
|
|
|
# Input 表单域
|
2015-08-03 13:34:40 +08:00
|
|
|
|
|
|
|
|
|
- order: 0
|
|
|
|
|
|
2015-08-05 14:47:47 +08:00
|
|
|
|
基本的表单校验例子。
|
2015-08-03 13:34:40 +08:00
|
|
|
|
|
2015-11-03 23:34:51 +08:00
|
|
|
|
**每个表单域要声明 `name` 属性作为校验的标识**,可通过其 `isValidating`、`errors` 属性判断是否处于校验中、是否校验不通过状态,具体可参见 **用户名** 校验。
|
2015-08-03 13:34:40 +08:00
|
|
|
|
|
|
|
|
|
表单提交的时候,通过 Validation 的 validate 方法判断是否所有表单域校验通过(isValid 会作为回调函数的参数传入)。
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
````jsx
|
2015-11-03 23:34:51 +08:00
|
|
|
|
import {Validation, Button, Form, Input} from 'antd';
|
2015-10-29 18:59:34 +08:00
|
|
|
|
const Validator = Validation.Validator;
|
|
|
|
|
const FormItem = Form.Item;
|
2015-08-03 13:34:40 +08:00
|
|
|
|
|
|
|
|
|
function cx(classNames) {
|
|
|
|
|
if (typeof classNames === 'object') {
|
|
|
|
|
return Object.keys(classNames).filter(function(className) {
|
|
|
|
|
return classNames[className];
|
|
|
|
|
}).join(' ');
|
|
|
|
|
} else {
|
|
|
|
|
return Array.prototype.join.call(arguments, ' ');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-20 18:56:21 +08:00
|
|
|
|
function noop() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-29 18:59:34 +08:00
|
|
|
|
const Demo = React.createClass({
|
2015-08-03 13:34:40 +08:00
|
|
|
|
mixins: [Validation.FieldMixin],
|
|
|
|
|
|
|
|
|
|
getInitialState() {
|
|
|
|
|
return {
|
|
|
|
|
status: {
|
|
|
|
|
email: {},
|
|
|
|
|
name: {},
|
|
|
|
|
passwd: {},
|
|
|
|
|
rePasswd: {},
|
2015-11-03 23:34:51 +08:00
|
|
|
|
textarea: {}
|
2015-08-03 13:34:40 +08:00
|
|
|
|
},
|
|
|
|
|
formData: {
|
|
|
|
|
email: undefined,
|
|
|
|
|
name: undefined,
|
|
|
|
|
passwd: undefined,
|
|
|
|
|
rePasswd: undefined,
|
2015-11-03 23:34:51 +08:00
|
|
|
|
textarea: undefined
|
2015-08-05 14:47:47 +08:00
|
|
|
|
},
|
|
|
|
|
isEmailOver: false, // email 是否输入完毕
|
|
|
|
|
emailValidateMethod: 'onBlur' // 用于改变 email 的验证方法
|
2015-08-03 13:34:40 +08:00
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
2015-10-29 18:59:34 +08:00
|
|
|
|
renderValidateStyle(item) {
|
|
|
|
|
const formData = this.state.formData;
|
|
|
|
|
const status = this.state.status;
|
2015-08-03 13:34:40 +08:00
|
|
|
|
|
2015-10-29 18:59:34 +08:00
|
|
|
|
const classes = cx({
|
|
|
|
|
'error': status[item].errors,
|
|
|
|
|
'validating': status[item].isValidating,
|
|
|
|
|
'success': formData[item] && !status[item].errors && !status[item].isValidating
|
2015-08-03 13:34:40 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return classes;
|
|
|
|
|
},
|
|
|
|
|
|
2015-08-05 14:47:47 +08:00
|
|
|
|
handleEmailInputBlur() {
|
|
|
|
|
this.setState({
|
|
|
|
|
isEmailOver: true
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
handleEmailInputFocus() {
|
|
|
|
|
if (this.state.isEmailOver) {
|
|
|
|
|
this.setState({
|
|
|
|
|
emailValidateMethod: 'onChange'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2015-08-03 13:34:40 +08:00
|
|
|
|
handleReset(e) {
|
|
|
|
|
this.refs.validation.reset();
|
|
|
|
|
this.setState(this.getInitialState());
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
handleSubmit(e) {
|
|
|
|
|
e.preventDefault();
|
2015-08-05 14:47:47 +08:00
|
|
|
|
this.setState({
|
|
|
|
|
isEmailOver: true
|
|
|
|
|
});
|
2015-10-29 18:59:34 +08:00
|
|
|
|
const validation = this.refs.validation;
|
2015-08-03 13:34:40 +08:00
|
|
|
|
validation.validate((valid) => {
|
|
|
|
|
if (!valid) {
|
|
|
|
|
console.log('error in form');
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
console.log('submit');
|
|
|
|
|
}
|
|
|
|
|
console.log(this.state.formData);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
userExists(rule, value, callback) {
|
|
|
|
|
if (!value) {
|
|
|
|
|
callback();
|
|
|
|
|
} else {
|
|
|
|
|
setTimeout(function () {
|
2015-10-25 11:35:44 +08:00
|
|
|
|
if (value === 'JasonWood') {
|
2015-08-03 13:34:40 +08:00
|
|
|
|
callback([new Error('抱歉,该用户名已被占用。')]);
|
|
|
|
|
} else {
|
|
|
|
|
callback();
|
|
|
|
|
}
|
2015-08-05 14:47:47 +08:00
|
|
|
|
}, 800);
|
2015-08-03 13:34:40 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
checkPass(rule, value, callback) {
|
2015-08-20 18:56:21 +08:00
|
|
|
|
if (this.state.formData.passwd) {
|
|
|
|
|
this.refs.validation.forceValidate(['rePasswd']);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-03 13:34:40 +08:00
|
|
|
|
callback();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
checkPass2(rule, value, callback) {
|
2015-08-05 10:12:12 +08:00
|
|
|
|
if (value && value !== this.state.formData.passwd) {
|
2015-08-03 13:34:40 +08:00
|
|
|
|
callback('两次输入密码不一致!');
|
|
|
|
|
} else {
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
render() {
|
2015-10-29 18:59:34 +08:00
|
|
|
|
const formData = this.state.formData;
|
|
|
|
|
const status = this.state.status;
|
2015-08-03 13:34:40 +08:00
|
|
|
|
|
|
|
|
|
return (
|
2015-10-25 11:35:44 +08:00
|
|
|
|
<Form horizontal>
|
2015-08-05 10:12:12 +08:00
|
|
|
|
<Validation ref="validation" onValidate={this.handleValidate}>
|
2015-10-29 18:59:34 +08:00
|
|
|
|
<FormItem
|
2015-10-25 11:35:44 +08:00
|
|
|
|
label="用户名:"
|
|
|
|
|
id="name"
|
2015-10-29 18:59:34 +08:00
|
|
|
|
labelCol={{span: 7}}
|
|
|
|
|
wrapperCol={{span: 12}}
|
|
|
|
|
validateStatus={this.renderValidateStyle('name')}
|
|
|
|
|
hasFeedback
|
2015-11-25 17:35:49 +08:00
|
|
|
|
help={status.name.isValidating ? '正在校验中..' : (status.name.errors && status.name.errors.join(','))}
|
2015-10-25 11:35:44 +08:00
|
|
|
|
required>
|
|
|
|
|
<Validator rules={[{required: true, min: 5, message: '用户名至少为 5 个字符'}, {validator: this.userExists}]}>
|
2015-10-29 18:59:34 +08:00
|
|
|
|
<Input name="name" id="name" value={formData.name} placeholder="实时校验,输入 JasonWood 看看" onChange={this.setField.bind(this, 'name')} />
|
2015-10-25 11:35:44 +08:00
|
|
|
|
</Validator>
|
2015-10-29 18:59:34 +08:00
|
|
|
|
</FormItem>
|
2015-10-25 11:35:44 +08:00
|
|
|
|
|
2015-10-29 18:59:34 +08:00
|
|
|
|
<FormItem
|
2015-10-25 11:35:44 +08:00
|
|
|
|
label="邮箱:"
|
|
|
|
|
id="email"
|
2015-10-29 18:59:34 +08:00
|
|
|
|
labelCol={{span: 7}}
|
|
|
|
|
wrapperCol={{span: 12}}
|
|
|
|
|
validateStatus={this.renderValidateStyle('email')}
|
|
|
|
|
hasFeedback={this.state.isEmailOver}
|
|
|
|
|
help={status.email.errors ? status.email.errors.join(',') : null}
|
2015-10-25 11:35:44 +08:00
|
|
|
|
required>
|
|
|
|
|
<Validator rules={[{required: true, type:'email', message: '请输入正确的邮箱地址'}]} trigger={this.state.emailValidateMethod}>
|
2015-11-25 17:35:49 +08:00
|
|
|
|
<Input type="email" name="email" id="email" value={formData.email} placeholder="onBlur 与 onChange 相结合" onBlur={this.handleEmailInputBlur} onFocus={this.handleEmailInputFocus} />
|
2015-10-25 11:35:44 +08:00
|
|
|
|
</Validator>
|
2015-10-29 18:59:34 +08:00
|
|
|
|
</FormItem>
|
2015-10-25 11:35:44 +08:00
|
|
|
|
|
2015-10-29 18:59:34 +08:00
|
|
|
|
<FormItem
|
2015-10-25 11:35:44 +08:00
|
|
|
|
label="密码:"
|
|
|
|
|
id="password"
|
2015-10-29 18:59:34 +08:00
|
|
|
|
labelCol={{span: 7}}
|
|
|
|
|
wrapperCol={{span: 12}}
|
|
|
|
|
validateStatus={this.renderValidateStyle('passwd')}
|
|
|
|
|
hasFeedback
|
|
|
|
|
help={status.passwd.errors ? status.passwd.errors.join(',') : null}
|
2015-10-25 11:35:44 +08:00
|
|
|
|
required>
|
|
|
|
|
<Validator rules={[{required: true, whitespace: true, message: '请填写密码'}, {validator: this.checkPass}]}>
|
2015-11-03 17:13:05 +08:00
|
|
|
|
<Input name="passwd" id="password" type="password" onContextMenu={noop} onPaste={noop} onCopy={noop} onCut={noop} autoComplete="off" value={formData.passwd}/>
|
2015-10-29 18:59:34 +08:00
|
|
|
|
</Validator>
|
|
|
|
|
</FormItem>
|
2015-10-25 11:35:44 +08:00
|
|
|
|
|
2015-10-29 18:59:34 +08:00
|
|
|
|
<FormItem
|
2015-10-25 11:35:44 +08:00
|
|
|
|
label="确认密码:"
|
|
|
|
|
id="password2"
|
2015-10-29 18:59:34 +08:00
|
|
|
|
labelCol={{span: 7}}
|
|
|
|
|
wrapperCol={{span: 12}}
|
|
|
|
|
validateStatus={this.renderValidateStyle('rePasswd')}
|
|
|
|
|
hasFeedback
|
|
|
|
|
help={status.rePasswd.errors ? status.rePasswd.errors.join(',') : null}
|
2015-10-25 11:35:44 +08:00
|
|
|
|
required>
|
|
|
|
|
<Validator rules={[{
|
|
|
|
|
required: true,
|
|
|
|
|
whitespace: true,
|
|
|
|
|
message: '请再次输入密码'
|
|
|
|
|
}, {validator: this.checkPass2}]}>
|
2015-11-03 17:13:05 +08:00
|
|
|
|
<Input name="rePasswd" id="password2" type="password" onContextMenu={noop} onPaste={noop} onCopy={noop} onCut={noop} autoComplete="off" value={formData.rePasswd} placeholder="两次输入密码保持一致"/>
|
2015-10-25 11:35:44 +08:00
|
|
|
|
</Validator>
|
2015-10-29 18:59:34 +08:00
|
|
|
|
</FormItem>
|
2015-10-25 11:35:44 +08:00
|
|
|
|
|
2015-10-29 18:59:34 +08:00
|
|
|
|
<FormItem
|
2015-10-25 11:35:44 +08:00
|
|
|
|
label="备注:"
|
|
|
|
|
id="textarea"
|
2015-10-29 18:59:34 +08:00
|
|
|
|
labelCol={{span: 7}}
|
|
|
|
|
wrapperCol={{span: 12}}
|
|
|
|
|
validateStatus={this.renderValidateStyle('textarea')}
|
|
|
|
|
help={status.textarea.errors ? status.textarea.errors.join(',') : null}
|
2015-10-25 11:35:44 +08:00
|
|
|
|
required>
|
|
|
|
|
<Validator rules={[{required: true, message: '真的不打算写点什么吗?'}]}>
|
2015-10-29 18:59:34 +08:00
|
|
|
|
<Input type="textarea" placeholder="随便写" id="textarea" name="textarea" value={formData.textarea} />
|
2015-10-25 11:35:44 +08:00
|
|
|
|
</Validator>
|
2015-10-29 18:59:34 +08:00
|
|
|
|
</FormItem>
|
2015-08-03 13:34:40 +08:00
|
|
|
|
|
2015-10-29 18:59:34 +08:00
|
|
|
|
<FormItem
|
|
|
|
|
wrapperCol={{span: 12, offset: 7}} >
|
|
|
|
|
<Button type="primary" onClick={this.handleSubmit}>确定</Button>
|
2015-08-03 13:34:40 +08:00
|
|
|
|
|
2015-10-29 18:59:34 +08:00
|
|
|
|
<Button type="ghost" onClick={this.handleReset}>重置</Button>
|
|
|
|
|
</FormItem>
|
2015-08-03 13:34:40 +08:00
|
|
|
|
</Validation>
|
2015-10-25 11:35:44 +08:00
|
|
|
|
</Form>
|
2015-08-03 13:34:40 +08:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2015-10-25 11:35:44 +08:00
|
|
|
|
ReactDOM.render(<Demo />, document.getElementById('components-validation-demo-basic'));
|
2015-08-03 13:34:40 +08:00
|
|
|
|
````
|