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

109 lines
2.8 KiB
Markdown
Raw Normal View History

2016-03-31 09:40:55 +08:00
---
order: 12
2016-08-23 21:00:35 +08:00
title:
2016-07-31 09:53:51 +08:00
zh-CN: 校验其他组件
2016-11-02 10:47:50 +08:00
en-US: Other Form Controls
2016-03-31 09:40:55 +08:00
---
2016-01-22 10:25:58 +08:00
2016-07-31 09:53:51 +08:00
## zh-CN
2016-11-02 10:47:50 +08:00
以上演示没有出现的表单控件对应的校验演示。
2016-01-22 10:25:58 +08:00
2016-07-31 09:53:51 +08:00
## en-US
2016-11-02 10:47:50 +08:00
Demostration for validataion configuration for form controls which are not show in the above demos.
2016-07-31 09:53:51 +08:00
2016-01-22 10:25:58 +08:00
````jsx
2016-11-02 10:47:50 +08:00
import { Select, Button, InputNumber, Form } from 'antd';
2016-01-22 10:25:58 +08:00
const Option = Select.Option;
const FormItem = Form.Item;
2016-11-02 10:47:50 +08:00
const Demo = Form.create()(React.createClass({
2016-01-22 10:25:58 +08:00
handleSubmit(e) {
e.preventDefault();
2016-11-02 10:47:50 +08:00
this.props.form.validateFields((err, values) => {
if (err) {
2016-01-22 10:25:58 +08:00
return;
}
2016-11-02 10:47:50 +08:00
console.log('Received values of form: ', values);
});
2016-01-22 10:25:58 +08:00
},
checkPrime(rule, value, callback) {
if (value !== 11) {
callback(new Error('The prime number between 8 to 12 is 11!'));
2016-01-22 10:25:58 +08:00
} else {
callback();
}
},
render() {
const { getFieldDecorator } = this.props.form;
2016-02-25 14:34:31 +08:00
const formItemLayout = {
labelCol: { span: 7 },
wrapperCol: { span: 12 },
};
2016-01-22 10:25:58 +08:00
return (
2016-11-02 10:47:50 +08:00
<Form horizontal onSubmit={this.handleSubmit}>
2016-01-22 10:25:58 +08:00
<FormItem
2016-02-25 14:34:31 +08:00
{...formItemLayout}
2016-07-31 09:53:51 +08:00
label="Country"
>
{getFieldDecorator('select', {
rules: [
{ required: true, message: 'Please select your country' },
],
})(
<Select placeholder="Please select a country" style={{ width: '100%' }}>
<Option value="china">China</Option>
<Option value="use">U.S.A</Option>
<Option value="japan">Japan</Option>
<Option value="korean">Korea</Option>
<Option value="Thailand">Thai</Option>
</Select>
)}
2016-01-22 10:25:58 +08:00
</FormItem>
<FormItem
2016-02-25 14:34:31 +08:00
{...formItemLayout}
2016-07-31 09:53:51 +08:00
label="Favourite colors"
>
{getFieldDecorator('multiSelect', {
rules: [
{ required: true, message: 'Please select your favourite colors', type: 'array' },
],
})(
<Select multiple placeholder="Please select favourite colors" style={{ width: '100%' }}>
<Option value="red">Red</Option>
<Option value="orange">Orange</Option>
<Option value="yellow">Yellow</Option>
<Option value="green">Green</Option>
<Option value="blue">Blue</Option>
</Select>
)}
2016-01-22 10:25:58 +08:00
</FormItem>
<FormItem
2016-02-25 14:34:31 +08:00
{...formItemLayout}
label="Prime num between 8, 12"
>
{getFieldDecorator('primeNumber', {
rules: [{ validator: this.checkPrime }],
})(
<InputNumber min={8} max={12} />
)}
2016-01-25 16:25:20 +08:00
</FormItem>
2016-11-02 10:47:50 +08:00
<FormItem wrapperCol={{ span: 12, offset: 7 }}>
<Button type="primary" htmlType="submit">Submit</Button>
2016-01-22 10:25:58 +08:00
</FormItem>
</Form>
);
},
2016-11-02 10:47:50 +08:00
}));
2016-01-22 10:25:58 +08:00
ReactDOM.render(<Demo />, mountNode);
````