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

218 lines
5.8 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: Others components related to validation
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
提供以下组件表单域的校验:`Select` `Radio` `DatePicker` `InputNumber` `Cascader`。在 submit 时使用 `validateFieldsAndScroll`,进行校验,可以自动把不在可见范围内的校验不通过的菜单域滚动进可见范围。
2016-01-22 10:25:58 +08:00
2016-07-31 09:53:51 +08:00
## en-US
2016-09-01 18:12:12 +08:00
Provide validation for following input filed: `Select` `Radio` `DatePicker` `InputNumber` `Cascader`. To use `validateFieldsAndScroll` with form validation, it will scroll the form to the failed input field which is not in visible area.
2016-07-31 09:53:51 +08:00
2016-01-22 10:25:58 +08:00
````jsx
2016-05-27 14:46:58 +08:00
import { Select, Radio, Checkbox, Button, DatePicker, TimePicker, InputNumber, Form, Cascader, Icon } from 'antd';
2016-01-22 10:25:58 +08:00
const Option = Select.Option;
const RadioGroup = Radio.Group;
2016-01-22 14:31:08 +08:00
const createForm = Form.create;
2016-01-22 10:25:58 +08:00
const FormItem = Form.Item;
let Demo = React.createClass({
2016-02-24 15:41:53 +08:00
componentDidMount() {
this.props.form.setFieldsValue({
eat: true,
sleep: true,
beat: true,
});
},
2016-01-22 10:25:58 +08:00
handleReset(e) {
e.preventDefault();
this.props.form.resetFields();
},
handleSubmit(e) {
e.preventDefault();
this.props.form.validateFieldsAndScroll((errors, values) => {
2016-08-23 21:00:35 +08:00
if (errors) {
2016-01-22 10:25:58 +08:00
console.log('Errors in form!!!');
return;
}
console.log('Submit!!!');
console.log(values);
});
},
checkBirthday(rule, value, callback) {
if (value && value.valueOf() >= Date.now()) {
2016-07-31 09:53:51 +08:00
callback(new Error("You can't be born in the future!"));
2016-01-22 10:25:58 +08:00
} else {
callback();
}
},
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() {
2016-01-25 16:25:20 +08:00
const address = [{
value: 'zhejiang',
2016-07-31 09:53:51 +08:00
label: 'Zhe Jiang',
2016-01-25 16:25:20 +08:00
children: [{
value: 'hangzhou',
2016-07-31 09:53:51 +08:00
label: 'Hang Zhou',
2016-01-25 16:25:20 +08:00
}],
}];
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 (
<Form horizontal>
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}
2016-07-31 09:53:51 +08:00
label="Gender"
>
{getFieldDecorator('radio', {
rules: [
{ required: true, message: 'Please select your gender' },
],
})(
<RadioGroup>
<Radio value="male">male</Radio>
<Radio value="female">female</Radio>
</RadioGroup>
)}
2016-09-22 10:09:22 +08:00
<span><Icon type="info-circle-o" /> No other gender</span>
2016-01-22 10:25:58 +08:00
</FormItem>
2016-02-24 15:41:53 +08:00
<FormItem
2016-02-25 14:34:31 +08:00
{...formItemLayout}
2016-07-31 09:53:51 +08:00
label="Hobby"
>
{getFieldDecorator('hobby')(
<Checkbox.Group options={['eat', 'sleeping', 'dozen doug']} />
)}
2016-02-24 15:41:53 +08:00
</FormItem>
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="Birthday"
>
{getFieldDecorator('birthday', {
rules: [
{
required: true,
type: 'object',
message: 'When is your birthday?',
}, {
validator: this.checkBirthday,
},
],
})(
<DatePicker />
)}
2016-01-22 10:25:58 +08:00
</FormItem>
2016-05-27 14:46:58 +08:00
<FormItem
{...formItemLayout}
2016-07-31 09:53:51 +08:00
label="Select the time"
>
{getFieldDecorator('time', {
rules: [
{
required: true,
type: 'object',
message: 'Please select the time',
},
],
})(
<TimePicker />
)}
2016-05-27 14:46:58 +08:00
</FormItem>
2016-01-22 10:25:58 +08:00
<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>
<FormItem
2016-02-25 14:34:31 +08:00
{...formItemLayout}
2016-07-31 09:53:51 +08:00
label="Please select address"
>
{getFieldDecorator('address', {
rules: [{ required: true, type: 'array' }],
})(
<Cascader options={address} />
)}
2016-01-22 10:25:58 +08:00
</FormItem>
<FormItem
wrapperCol={{ span: 12, offset: 7 }}
>
2016-07-31 09:53:51 +08:00
<Button type="primary" onClick={this.handleSubmit}>OK</Button>
2016-01-22 10:25:58 +08:00
&nbsp;&nbsp;&nbsp;
2016-07-31 09:53:51 +08:00
<Button type="ghost" onClick={this.handleReset}>Reset</Button>
2016-01-22 10:25:58 +08:00
</FormItem>
</Form>
);
},
});
Demo = createForm()(Demo);
ReactDOM.render(<Demo />, mountNode);
````