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

209 lines
5.6 KiB
Markdown
Raw Normal View History

2016-03-31 09:40:55 +08:00
---
order: 12
title: 校验其他组件
---
2016-01-22 10:25:58 +08:00
提供以下组件表单域的校验:`Select` `Radio` `DatePicker` `InputNumber` `Cascader`。在 submit 时使用 `validateFieldsAndScroll`,进行校验,可以自动把不在可见范围内的校验不通过的菜单域滚动进可见范围。
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-01-22 10:25:58 +08:00
if (!!errors) {
console.log('Errors in form!!!');
return;
}
console.log('Submit!!!');
console.log(values);
});
},
checkBirthday(rule, value, callback) {
if (value && value.getTime() >= Date.now()) {
callback(new Error('你不可能在未来出生吧!'));
} else {
callback();
}
},
checkPrime(rule, value, callback) {
if (value !== 11) {
callback(new Error('8~12之间的质数明明是11啊!'));
} else {
callback();
}
},
render() {
2016-01-25 16:25:20 +08:00
const address = [{
value: 'zhejiang',
label: '浙江',
children: [{
value: 'hangzhou',
label: '杭州',
}],
}];
2016-01-30 19:00:56 +08:00
const { getFieldProps } = this.props.form;
2016-02-25 14:34:31 +08:00
const selectProps = getFieldProps('select', {
rules: [
2016-05-11 09:32:33 +08:00
{ required: true, message: '请选择您的国籍' },
2016-02-25 14:34:31 +08:00
],
});
const multiSelectProps = getFieldProps('multiSelect', {
rules: [
{ required: true, message: '请选择您喜欢的颜色', type: 'array' },
2016-05-11 09:32:33 +08:00
],
2016-02-25 14:34:31 +08:00
});
const radioProps = getFieldProps('radio', {
rules: [
2016-05-11 09:32:33 +08:00
{ required: true, message: '请选择您的性别' },
],
2016-02-25 14:34:31 +08:00
});
const birthdayProps = getFieldProps('birthday', {
rules: [
{
required: true,
type: 'date',
2016-05-27 14:46:58 +08:00
message: '你的生日是什么呢?',
2016-02-25 14:34:31 +08:00
}, {
validator: this.checkBirthday,
2016-05-11 09:32:33 +08:00
},
],
2016-02-25 14:34:31 +08:00
});
2016-05-27 14:46:58 +08:00
const timeProps = getFieldProps('time', {
getValueFromEvent: (value, timeString) => timeString,
rules: [
{ required: true, message: '请选择一个时间' },
],
});
2016-02-25 14:34:31 +08:00
const primeNumberProps = getFieldProps('primeNumber', {
rules: [{ validator: this.checkPrime }],
});
const addressProps = getFieldProps('address', {
rules: [{ required: true, type: 'array' }],
});
const formItemLayout = {
labelCol: { span: 7 },
wrapperCol: { span: 12 },
};
2016-01-22 10:25:58 +08:00
return (
<Form horizontal form={this.props.form}>
<FormItem
2016-02-25 14:34:31 +08:00
{...formItemLayout}
label="国籍"
>
2016-02-25 14:34:31 +08:00
<Select {...selectProps} placeholder="请选择国家" style={{ width: '100%' }}>
2016-01-22 10:25:58 +08:00
<Option value="china">中国</Option>
<Option value="use">美国</Option>
<Option value="japan">日本</Option>
<Option value="korean">韩国</Option>
<Option value="Thailand">泰国</Option>
</Select>
</FormItem>
<FormItem
2016-02-25 14:34:31 +08:00
{...formItemLayout}
label="喜欢的颜色"
>
2016-02-25 14:34:31 +08:00
<Select {...multiSelectProps} multiple placeholder="请选择颜色" style={{ width: '100%' }}>
2016-01-22 10:25:58 +08:00
<Option value="red">红色</Option>
<Option value="orange">橙色</Option>
<Option value="yellow">黄色</Option>
<Option value="green">绿色</Option>
<Option value="blue">蓝色</Option>
</Select>
</FormItem>
<FormItem
2016-02-25 14:34:31 +08:00
{...formItemLayout}
label="性别"
>
2016-02-25 14:34:31 +08:00
<RadioGroup {...radioProps}>
2016-01-22 10:25:58 +08:00
<Radio value="male"></Radio>
<Radio value="female"></Radio>
</RadioGroup>
<span><Icon type="info-circle-o" /> 暂不支持其它性别</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}
label="兴趣爱好"
>
2016-02-24 15:41:53 +08:00
<Checkbox {...getFieldProps('eat', {
valuePropName: 'checked',
})}
>吃饭饭</Checkbox>
2016-02-24 15:41:53 +08:00
<Checkbox {...getFieldProps('sleep', {
valuePropName: 'checked',
})}
>睡觉觉</Checkbox>
2016-02-24 15:41:53 +08:00
<Checkbox {...getFieldProps('beat', {
valuePropName: 'checked',
})}
>打豆豆</Checkbox>
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}
label="生日"
>
2016-02-25 14:34:31 +08:00
<DatePicker {...birthdayProps} />
2016-01-22 10:25:58 +08:00
</FormItem>
2016-05-27 14:46:58 +08:00
<FormItem
{...formItemLayout}
label="选一个时间"
>
2016-05-27 14:46:58 +08:00
<TimePicker {...timeProps} />
</FormItem>
2016-01-22 10:25:58 +08:00
<FormItem
2016-02-25 14:34:31 +08:00
{...formItemLayout}
label="8~12间的质数"
>
2016-02-25 14:34:31 +08:00
<InputNumber {...primeNumberProps} min={8} max={12} />
2016-01-25 16:25:20 +08:00
</FormItem>
<FormItem
2016-02-25 14:34:31 +08:00
{...formItemLayout}
label="选择地址"
>
2016-02-25 14:34:31 +08:00
<Cascader {...addressProps} options={address} />
2016-01-22 10:25:58 +08:00
</FormItem>
<FormItem
wrapperCol={{ span: 12, offset: 7 }}
>
2016-01-22 10:25:58 +08:00
<Button type="primary" onClick={this.handleSubmit}>确定</Button>
&nbsp;&nbsp;&nbsp;
<Button type="ghost" onClick={this.handleReset}>重置</Button>
</FormItem>
</Form>
);
},
});
Demo = createForm()(Demo);
ReactDOM.render(<Demo />, mountNode);
````