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

214 lines
6.0 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: 校验其他组件
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
Provide validation for fllowing 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-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.getTime() >= 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) {
2016-07-31 09:53:51 +08:00
callback(new Error('The prime number between 8 to 12 is obiviously 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
}],
}];
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-07-31 09:53:51 +08:00
{ required: true, message: 'Please select your country' },
2016-02-25 14:34:31 +08:00
],
});
const multiSelectProps = getFieldProps('multiSelect', {
rules: [
2016-07-31 09:53:51 +08:00
{ required: true, message: 'Please select your favourite colors', type: 'array' },
2016-05-11 09:32:33 +08:00
],
2016-02-25 14:34:31 +08:00
});
const radioProps = getFieldProps('radio', {
rules: [
2016-07-31 09:53:51 +08:00
{ required: true, message: 'Please select your gender' },
2016-05-11 09:32:33 +08:00
],
2016-02-25 14:34:31 +08:00
});
const birthdayProps = getFieldProps('birthday', {
rules: [
{
required: true,
type: 'date',
2016-07-31 09:53:51 +08:00
message: 'When is your birthday?',
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: [
2016-07-31 09:53:51 +08:00
{ required: true, message: 'Please select the time' },
2016-05-27 14:46:58 +08:00
],
});
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 (
2016-07-29 13:59:37 +08:00
<Form horizontal form={this.props.form}>
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"
>
2016-07-31 09:53:51 +08:00
<Select {...selectProps} 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>
2016-01-22 10:25:58 +08:00
</Select>
</FormItem>
<FormItem
2016-02-25 14:34:31 +08:00
{...formItemLayout}
2016-07-31 09:53:51 +08:00
label="Favourite colors"
>
2016-07-31 09:53:51 +08:00
<Select {...multiSelectProps} 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>
2016-01-22 10:25:58 +08:00
</Select>
</FormItem>
<FormItem
2016-02-25 14:34:31 +08:00
{...formItemLayout}
2016-07-31 09:53:51 +08:00
label="Gender"
>
2016-02-25 14:34:31 +08:00
<RadioGroup {...radioProps}>
2016-07-31 09:53:51 +08:00
<Radio value="male">male</Radio>
<Radio value="female">female</Radio>
2016-01-22 10:25:58 +08:00
</RadioGroup>
2016-07-31 09:53:51 +08:00
<span><Icon type="info-circle-o" /> Temporarily does not support ohter 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"
>
2016-02-24 15:41:53 +08:00
<Checkbox {...getFieldProps('eat', {
valuePropName: 'checked',
2016-07-31 09:53:51 +08:00
})}>eat</Checkbox>
2016-02-24 15:41:53 +08:00
<Checkbox {...getFieldProps('sleep', {
valuePropName: 'checked',
2016-07-31 09:53:51 +08:00
})}>sleeping</Checkbox>
2016-02-24 15:41:53 +08:00
<Checkbox {...getFieldProps('beat', {
valuePropName: 'checked',
2016-07-31 09:53:51 +08:00
})}>dozen doug</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}
2016-07-31 09:53:51 +08:00
label="Birthday"
>
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}
2016-07-31 09:53:51 +08:00
label="Select the time"
>
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}
2016-07-31 09:53:51 +08:00
label="A prime number between 8 to 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}
2016-07-31 09:53:51 +08:00
label="Please select address"
>
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-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);
````