ant-design/components/validation/demo/customize.md

266 lines
7.5 KiB
Markdown
Raw Normal View History

2015-10-20 16:47:55 +08:00
# 自定义校验规则
2015-08-03 13:34:40 +08:00
2015-11-03 23:34:51 +08:00
- order: 2
2015-08-03 13:34:40 +08:00
密码校验实例。
2015-08-05 10:12:12 +08:00
这里使用了 validation 的 `forceValidate(fields, callback)` 方法,在对第一次输入的密码进行校验时会触发二次密码的校验。
2015-08-03 13:34:40 +08:00
---
````jsx
2015-10-29 18:59:34 +08:00
import {Validation, Button, Form, Input, Row, Col} from 'antd';
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, ' ');
}
}
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: {
pass: {},
rePass: {}
},
formData: {
pass: undefined,
rePass: undefined
},
passBarShow: false, // 是否显示密码强度提示条
2015-10-20 16:47:55 +08:00
rePassBarShow: false,
2015-08-03 13:34:40 +08:00
passStrength: 'L', // 密码强度
rePassStrength: 'L'
};
},
handleSubmit(e) {
e.preventDefault();
var validation = this.refs.validation;
validation.validate((valid) => {
if (!valid) {
console.log('error in form');
return;
} else {
console.log('submit');
}
console.log(this.state.formData);
});
},
handleReset(e) {
this.refs.validation.reset();
this.setState(this.getInitialState());
e.preventDefault();
},
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;
},
getPassStrenth(value, type) {
if (value) {
2015-10-29 18:59:34 +08:00
let strength;
2015-08-03 13:34:40 +08:00
// 密码强度的校验规则自定义,这里只是做个简单的示例
if (value.length < 6) {
strength = 'L';
} else if (value.length <= 9) {
strength = 'M';
} else {
strength = 'H';
}
type === 'pass' ? this.setState({ passBarShow: true, passStrength: strength }) : this.setState({ rePassBarShow: true, rePassStrength: strength });
} else {
type === 'pass' ? this.setState({ passBarShow: false }) : this.setState({ rePassBarShow: false });
}
},
checkPass(rule, value, callback) {
this.getPassStrenth(value, 'pass');
if (this.state.formData.pass) {
this.refs.validation.forceValidate(['rePass']);
}
callback();
},
checkPass2(rule, value, callback) {
this.getPassStrenth(value, 'rePass');
if (value && value !== this.state.formData.pass) {
callback('两次输入密码不一致!');
} else {
callback();
}
},
renderPassStrengthBar(type) {
2015-10-29 18:59:34 +08:00
const strength = type === 'pass' ? this.state.passStrength : this.state.rePassStrength;
const classSet = cx({
2015-08-03 13:34:40 +08:00
'ant-pwd-strength': true,
'ant-pwd-strength-low': strength === 'L',
'ant-pwd-strength-medium': strength === 'M',
'ant-pwd-strength-high': strength === 'H'
});
2015-10-29 18:59:34 +08:00
const level = {
2015-08-03 13:34:40 +08:00
L: '低',
M: '中',
H: '高'
};
return (
<div>
<ul className={classSet}>
<li className="ant-pwd-strength-item ant-pwd-strength-item-1"></li>
<li className="ant-pwd-strength-item ant-pwd-strength-item-2"></li>
<li className="ant-pwd-strength-item ant-pwd-strength-item-3"></li>
<span className="ant-form-text">
{level[strength]}
</span>
</ul>
</div>
);
},
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
<Row>
<Col span="18">
<FormItem
2015-10-25 11:35:44 +08:00
label="密码:"
id="confirmPass"
2015-10-29 18:59:34 +08:00
labelCol={{span: 6}}
wrapperCol={{span: 18}}
validateStatus={this.renderValidateStyle('pass')}
help={status.pass.errors ? status.pass.errors.join(',') : null}
2015-10-25 11:35:44 +08:00
required>
<Validator rules={[{required: true, whitespace: true, message: '请填写密码'}, {validator: this.checkPass}]} trigger="onChange">
2015-11-03 17:13:05 +08:00
<Input name="pass" id="confirmPass" type="password" onContextMenu={noop} onPaste={noop} onCopy={noop} onCut={noop} autoComplete="off" value={formData.pass}/>
2015-10-25 11:35:44 +08:00
</Validator>
2015-10-29 18:59:34 +08:00
</FormItem>
</Col>
<Col span="6">
2015-08-03 13:34:40 +08:00
{this.state.passBarShow ? this.renderPassStrengthBar('pass') : null}
2015-10-29 18:59:34 +08:00
</Col>
</Row>
2015-08-03 13:34:40 +08:00
2015-10-29 18:59:34 +08:00
<Row>
<Col span="18">
<FormItem
2015-10-25 11:35:44 +08:00
label="确认密码:"
id="confirmPass2"
2015-10-29 18:59:34 +08:00
labelCol={{span: 6}}
wrapperCol={{span: 18}}
validateStatus={this.renderValidateStyle('rePass')}
help={status.rePass.errors ? status.rePass.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="rePass" id="confirmPass2" type="password" onContextMenu={noop} onPaste={noop} onCopy={noop} onCut={noop} autoComplete="off" value={formData.rePass}/>
2015-10-25 11:35:44 +08:00
</Validator>
2015-10-29 18:59:34 +08:00
</FormItem>
</Col>
<Col span="6">
2015-08-03 13:34:40 +08:00
{this.state.rePassBarShow ? this.renderPassStrengthBar('rePass') : null}
2015-10-29 18:59:34 +08:00
</Col>
</Row>
2015-08-03 13:34:40 +08:00
2015-10-29 18:59:34 +08:00
<FormItem
wrapperCol={{span: 12, offset: 6}}
2015-10-25 11:35:44 +08:00
required>
2015-10-29 18:59:34 +08:00
<Button type="primary" onClick={this.handleSubmit}>确定</Button>
2015-10-25 11:35:44 +08:00
&nbsp;&nbsp;&nbsp;
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-customize'));
2015-08-03 13:34:40 +08:00
````
<style>
.ant-pwd-strength {
display: inline-block;
margin-left: 8px;
line-height: 32px;
height: 32px;
vertical-align: middle;
}
.ant-pwd-strength-item {
float: left;
margin-right: 1px;
margin-top: 12px;
width: 19px;
height: 8px;
line-height: 8px;
list-style: none;
background-color: #f3f3f3;
transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
-webkit-transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
-moz-transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
}
.ant-pwd-strength-item-1 {
border-top-left-radius: 6px;
border-bottom-left-radius: 6px;
}
.ant-pwd-strength-item-2 {
width: 20px;
}
.ant-pwd-strength-item-3 {
border-top-right-radius: 6px;
border-bottom-right-radius: 6px;
margin-right: 8px;
}
.ant-pwd-strength-low .ant-pwd-strength-item-1, .ant-pwd-strength-medium .ant-pwd-strength-item-1, .ant-pwd-strength-high .ant-pwd-strength-item-1 {
background-color: #FAC450;
}
.ant-pwd-strength-medium .ant-pwd-strength-item-2, .ant-pwd-strength-high .ant-pwd-strength-item-2 {
background-color: rgba(135, 208, 104, .6);
2015-08-18 17:48:26 +08:00
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#9987D068,endColorstr=#9987D068);
2015-08-03 13:34:40 +08:00
}
.ant-pwd-strength-high .ant-pwd-strength-item-3 {
background-color: #87D068;
}
</style>