• antd4 Form表单验证的错误信息用Tooltip展示


        1、Tooltip属性

        autoAdjustOverflow={false}   // 气泡被遮挡时是否自动调整位置

        open={!!errorInfo1}  // 用于手动控制浮层显隐

       2、Form.Item属性

        help=''   // 设置这个不显示错误信息

        hidden   // 隐藏当前项不显示

      3、Form属性

       validateTrigger={['onChange']}  // 设置字段校验的时机

    1. import React, { Component } from 'react'
    2. import { Form, Input, Row, Col, Select, Button, Tooltip } from 'antd'
    3. const { Option } = Select;
    4. class FormAntd extends Component {
    5. constructor(props) {
    6. super(props)
    7. this.state = {
    8. errorInfo: '',
    9. errorInfo1: '',
    10. errorInfo2: '',
    11. }
    12. }
    13. formRef = React.createRef()
    14. onValuesChange = (changedValues, allValues) => {
    15. console.log('onValuesChange', changedValues, allValues);
    16. }
    17. onFinish = (value) => {  // 点击提交时,做最后的校验
    18. console.log('onFinish', value);
    19. // const form = this.formRef.current
    20. // form.validateFields().then((values) => {  // 如果全部字段通过校验,会走then方法,里面可以打印出表单所有字段(一个object)
    21. // console.log('成功')
    22. // console.log(values)
    23. // }).catch((errInfo) => {  // 如果有字段没听过校验,会走catch,里面可以打印所有校验失败的信息
    24. // console.log('失败');
    25. // console.log(errInfo);
    26. // })
    27. }
    28. onFinishFailed = ({ values, errorFields, outOfDate }) => {  // 点击提交时,做最后的校验
    29. console.log('onFinishFailed', values, errorFields, outOfDate);
    30. }
    31. render() {
    32. const layout = {
    33. labelCol: { span: 8 },
    34. wrapperCol: { span: 16 },
    35. }
    36. const { errorInfo, errorInfo1, errorInfo2 } = this.state;
    37. return (
    38. <div>
    39. <h3>表单校验-demoh3>
    40. <Form ref={this.formRef}
    41. {...layout}
    42. onFinish={this.onFinish}
    43. onFinishFailed={this.onFinishFailed}
    44. onValuesChange={this.onValuesChange}
    45. validateTrigger={['onChange']}
    46. style={{ width: '600px', margin: '0 auto' }}
    47. >
    48. <Row gutter={24}>
    49. <Col span={24} key="select1">
    50. <Tooltip placement="right" title={errorInfo1}
    51. autoAdjustOverflow={false}
    52. open={!!errorInfo1}>
    53. <Form.Item label="下拉框1" name="select1"
    54. rules={[{ required: true, message: '请输入用户名!' },
    55. ]}
    56. >
    57. <Select
    58. mode="multiple"
    59. showSearch
    60. placeholder="下拉框1"
    61. // onChange={this.handleChangeSchool}
    62. style={{ width: '100%' }}
    63. >
    64. <Option key='1' value='11'>aaOption>
    65. <Option key='2' value='22'>bbOption>
    66. <Option key='3' value='33'>ccOption>
    67. Select>
    68. Form.Item>
    69. Tooltip>
    70. Col>
    71. <Col span={24} key="text1">
    72. <Tooltip placement="right" title={errorInfo2}
    73. autoAdjustOverflow={false}
    74. open={!!errorInfo2}>
    75. <Form.Item label="密码" name="text1"
    76. // validateTrigger={['onBlur']}
    77. // validateTrigger={['onChange']}
    78. help='' // 设置这个不显示错误信息
    79. // hidden // 隐藏当前项不显示
    80. rules={[
    81. // { required: true, message: '请输入密码!' },
    82. // { min: 6, message: '密码至少6位!' },
    83. // { max: 10, message: '密码最长10位!' },
    84. {
    85. validator: (_, value) => {
    86. if (!value) {
    87. this.setState({
    88. errorInfo2: '请输入密码!'
    89. });
    90. return Promise.reject('请输入密码!')
    91. } else if (value && (value.length < 6 || value.length > 10)) {
    92. this.setState({
    93. errorInfo2: 'text1必须是6~10位'
    94. });
    95. return Promise.reject('text1必须是6~10位')
    96. } else {
    97. this.setState({
    98. errorInfo2: ''
    99. });
    100. return Promise.resolve()
    101. }
    102. }
    103. }
    104. ]}
    105. >
    106. <Input placeholder="text1" />
    107. Form.Item>
    108. Tooltip>
    109. Col>
    110. <Col span={24} key="text2">
    111. <Tooltip placement="right" title={errorInfo}
    112. autoAdjustOverflow={false}
    113. open={!!errorInfo}>
    114. <Form.Item label="确认密码" name="text2"
    115. // validateTrigger="onfocus"
    116. // validateTrigger={['onChange']}
    117. // hidden
    118. help=''
    119. rules={[
    120. {
    121. validator: (_, value) => {
    122. const form = this.formRef.current
    123. let text1 = form.getFieldValue('text1')
    124. if (value && (value.length < 6 || value.length > 10)) {
    125. this.setState({
    126. errorInfo: 'text1必须是6~10位'
    127. });
    128. return Promise.reject('text1必须是6~10位')
    129. } else if (text1 !== value) {
    130. this.setState({
    131. errorInfo: '两次密码不一致'
    132. });
    133. return Promise.reject(new Error('两次密码不一致'))
    134. } else {
    135. this.setState({
    136. errorInfo: ''
    137. });
    138. return Promise.resolve()
    139. }
    140. }
    141. }
    142. ]}
    143. >
    144. <Input placeholder="text2" />
    145. Form.Item>
    146. Tooltip>
    147. Col>
    148. <Col span={24} key="text3">
    149. <Form.Item label="文本框3" name="text3"
    150. tooltip={{
    151. title: (
    152. '我是浮动提示内容'
    153. ),
    154. color: 'red',
    155. placement: 'rightTop',
    156. }}
    157. >
    158. <Input type="number" placeholder="text3" />
    159. Form.Item>
    160. Col>
    161. Row>
    162. <Row>
    163. <Col span={24} style={{ textAlign: 'right' }}>
    164. <Button type="primary" htmlType='submit' style={{ marginRight: '8px' }}>提交Button>
    165. Col>
    166. Row>
    167. Form>
    168. div>
    169. )
    170. }
    171. }
    172. export default FormAntd;

  • 相关阅读:
    1362:家庭问题(family)
    SMART PLC 和S7-1200PLC MODBUSTCP通信速度测试
    企业电子招标采购系统源码Spring Boot + Mybatis + Redis + Layui + 前后端分离 构建企业电子招采平台之立项流程图
    Spring Boot 整合 Redis,使用 RedisTemplate 客户端
    序列化和反序列化
    MAX插件CGMAGIC一键解决效果图在软包硬包上费时费力操作!
    tcpdump抓包详解
    前端动效讲解与实战
    SOAP接口对接
    线程安全问题
  • 原文地址:https://blog.csdn.net/web_cgh/article/details/127717406