import React, { Component } from 'react'
import { Form, Input, Button } from 'antd'
class formDataexplae extends Component {
formRef = React.createRef()
onFinish = values => {
console.log(values)
}
getValues= () => {
const form = this.formRef.current
const values = form.getFieldsValue(['name','age'])
const values = form.getFieldsValue()
console.log(values)
}
getValidateValues= async () => {
const form = this.formRef.current
try {
const values = await form.validateFields(['name','age'])
console.log(values)
} catch (err) {
console.log(err)
}
}
render() {
const { Item } = Form
return (
<Form ref={this.formRef} onFinish={this.onFinish}>
<Item name='name' label='姓名' rules={[{required:true,message:'请输入姓名'}]}>
<Input />
</Item>
<Item name='age' label='年龄'>
<Input />
</Item>
<Item>
<Button type='primary' htmlType='submit'>提交</Button>
<Button type='link' onClick={this.getValues}>非 Submit 方式(不验证)</Button>
<Button type='link' onClick={this.getValidateValues}>非 Submit 方式(验证)</Button>
</Item>
</Form>
)
}
}
export default formDataexplae
|