ant-design/components/form/demo/without-form-create.md

73 lines
1.7 KiB
Markdown
Raw Normal View History

---
order: 9
title:
zh-CN: 自行处理表单数据
en-US: Handle Form Data Manually
---
## zh-CN
使用 `Form.create` 处理后的表单具有自动收集数据并校验的功能,但如果您不需要这个功能,或者默认的行为无法满足业务需求,可以选择不使用 `Form.create` 并自行处理数据。
## en-US
`Form.create` will collect and validate form data automatically. But if you don't need this feature or the default behaviour cannot satisfy your business, you can drop `Form.create` and handle form data manually.
2019-05-07 14:57:32 +08:00
```jsx
import { Form, InputNumber } from 'antd';
2018-06-27 15:55:04 +08:00
function validatePrimeNumber(number) {
if (number === 11) {
return {
validateStatus: 'success',
errorMsg: null,
};
}
return {
validateStatus: 'error',
errorMsg: 'The prime between 8 and 12 is 11!',
};
}
class RawForm extends React.Component {
state = {
number: {
value: 11,
},
};
2018-06-27 15:55:04 +08:00
2019-05-07 14:57:32 +08:00
handleNumberChange = value => {
this.setState({
number: {
...validatePrimeNumber(value),
value,
},
});
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
render() {
const formItemLayout = {
labelCol: { span: 7 },
wrapperCol: { span: 12 },
};
2019-06-19 19:09:08 +08:00
const { number } = this.state;
2019-05-07 14:57:32 +08:00
const tips =
'A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself.';
return (
2017-01-05 16:57:41 +08:00
<Form>
2018-12-22 16:48:30 +08:00
<Form.Item
{...formItemLayout}
label="Prime between 8 & 12"
validateStatus={number.validateStatus}
help={number.errorMsg || tips}
>
2019-05-07 14:57:32 +08:00
<InputNumber min={8} max={12} value={number.value} onChange={this.handleNumberChange} />
2018-12-22 16:48:30 +08:00
</Form.Item>
</Form>
);
}
}
ReactDOM.render(<RawForm />, mountNode);
2019-05-07 14:57:32 +08:00
```