--- order: 12 title: 校验其他组件 --- 提供以下组件表单域的校验:`Select` `Radio` `DatePicker` `InputNumber` `Cascader`。在 submit 时使用 `validateFieldsAndScroll`,进行校验,可以自动把不在可见范围内的校验不通过的菜单域滚动进可见范围。 ````jsx import { Select, Radio, Checkbox, Button, DatePicker, TimePicker, InputNumber, Form, Cascader, Icon } from 'antd'; const Option = Select.Option; const RadioGroup = Radio.Group; const createForm = Form.create; const FormItem = Form.Item; let Demo = React.createClass({ componentDidMount() { this.props.form.setFieldsValue({ eat: true, sleep: true, beat: true, }); }, handleReset(e) { e.preventDefault(); this.props.form.resetFields(); }, handleSubmit(e) { e.preventDefault(); this.props.form.validateFieldsAndScroll((errors, values) => { 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() { const address = [{ value: 'zhejiang', label: '浙江', children: [{ value: 'hangzhou', label: '杭州', }], }]; const { getFieldProps } = this.props.form; const selectProps = getFieldProps('select', { rules: [ { required: true, message: '请选择您的国籍' }, ], }); const multiSelectProps = getFieldProps('multiSelect', { rules: [ { required: true, message: '请选择您喜欢的颜色', type: 'array' }, ], }); const radioProps = getFieldProps('radio', { rules: [ { required: true, message: '请选择您的性别' }, ], }); const birthdayProps = getFieldProps('birthday', { rules: [ { required: true, type: 'date', message: '你的生日是什么呢?', }, { validator: this.checkBirthday, }, ], }); const timeProps = getFieldProps('time', { getValueFromEvent: (value, timeString) => timeString, rules: [ { required: true, message: '请选择一个时间' }, ], }); 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 }, }; return (
); }, }); Demo = createForm()(Demo); ReactDOM.render(