React Antd 4 使用类方法,提交表单时触发验证

注意

如果你还没升级Antd 4,此文不适用。

个人比较习惯用Class component,此文不适用于Form.useForm

步骤

1、在 class component 下,可以通过 ref 获取数据域

2、在提交时,使用validateFields() 或 getFieldsValue()来获取表单值

建立Form的ref

formRef =  React.createRef();

<Form  name="normal_login"
       className="login-form"
       ref={this.formRef}
       >

绑定提交按钮

<Button size="large" className="btn-red" block onClick={()=>this.checkAuth()}>提 交</Button>

提交时触发验证

checkAuth=async()=>{
          
  if (this.formRef && this.formRef.current){ //很重要,在render后,formRef才实例化
       try {
           // 使用 validateFields 获取多个字段值,若验证通过,则返回表单值数组
           const values = await this.formRef.current.validateFields();
           console.log('Validate Success:', values);

           //执行提交数据

         } catch (errorInfo) {
           //若验证失败,返回数组{values:{表单值数组},errorFields:{验证未通过的表单值数组:{errors,name}}}
           console.log('Failed:', errorInfo);
         }

   }
}
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
灵活好用的jq表单验证,自己封装可按照验证需求添加验证方法,不需要修改验证整体结构,内含使用说明。 var arr = new Array(); /* */ //用户验证 arr[0] = new XQValidation(); arr[0].XChecksetcont("3-12长度,字母,数字,下划线", "", "表单不能为空", "验证失败", "此用户已存在", "此用户可以注册"); arr[0].posturl("/FormValidation/ajax/ajax-validation.asmx/user_isNull", "name"); arr[0].check("tname", "td1", "checkusername", true, true); //密码验证 arr[1] = new XQValidation(); arr[1].XChecksetcont("6-20长度,字母,数字,和其他符号", "ok", "表单不能为空", "wrong", "表单不能为空"); arr[1].check("tpass", "td2", "checkpassword", true, false); //邮箱验证 arr[2] = new XQValidation(); arr[2].XChecksetcont("正确邮箱", "ok", "表单不能为空", "wrong"); arr[2].check("tmail", "td3", "checkemail", true, false); //联系方式验证 arr[3] = new XQValidation(); arr[3].XChecksetcont("正确手机,非必须", "ok", "表单不能为空", "wrong"); arr[3].check("tmobile", "td4", "checkcontactway", true, false); //QQ验证 arr[4] = new XQValidation(); arr[4].XChecksetcont("qq,非必须", "ok", "表单不能为空", "wrong"); arr[4].check("tqq", "td5", "checkqq", false, false); //验证验证 arr[5] = new XQValidation(); arr[5].XChecksetcont("", "ok", "表单不能为空", "wrong"); arr[5].check("tcode", "td6", "checknull", true, false); //重复密码验证 arr[6] = new XQValidation(); arr[6].XChecksetcont("", "", "表单不能为空", "密码不匹配"); arr[6].check("tapass", "td7", "checkpassword_", true, false); /* *summary:提交表单验证 */ function formok() { for (var i = 0; i < arr.length; i++) {//循环遍历验证对象 if (!arr[i].getstate()) { arr[i].alertwrong(); return false } } //判断两次输入的密码是否一样 if (!chkapass($("tapass").value)) { arr[6].alertwrong(); return false; } return true; }
React.js和Ant Design 4.x中,表单提交可以通过使用Form组件的onFinish属性来实现。当表单中的所有表单项都通过验证,onFinish事件会被触发,并且会将表单的值作为参数传递给事件处理函数。下面是一个简单的例子,展示了如何在React.js和Ant Design 4.x中提交表单。 首先,在你的代码中导入所需的模块: ```js import React, { useState } from 'react'; import { Form, Input, Button } from 'antd'; ``` 接下来,创建一个表单组件: ```js const MyForm = () => { // 定义表单项的值 const [form] = Form.useForm(); // 表单提交事件 const handleSubmit = (values) => { console.log('Received values of form: ', values); // 在这里可以提交表单数据到服务器 }; return ( <Form form={form} onFinish={handleSubmit}> <Form.Item label="Username" name="username" rules={[{ required: true, message: 'Please input your username!' }]}> <Input /> </Form.Item> <Form.Item label="Password" name="password" rules={[{ required: true, message: 'Please input your password!' }]}> <Input.Password /> </Form.Item> <Form.Item> <Button type="primary" htmlType="submit"> Submit </Button> </Form.Item> </Form> ); }; ``` 在这个例子中,我们使用了Ant Design的Form、Input和Button组件来创建表单。在表单中,我们定义了一个onFinish事件处理函数handleSubmit,当表单中的所有表单项都通过验证,该事件会被触发。在事件处理函数中,我们可以获取到表单的值,并将其提交到服务器。 最后,我们将表单组件渲染到页面中: ```js ReactDOM.render(<MyForm />, document.getElementById('root')); ``` 这是一个简单的例子,你可以根据自己的需求来添加更多的表单项和验证规则。希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值