import React,{ Component } from 'react';
import { connect } from 'dva';
import { WhiteSpace,NavBar ,List, InputItem ,Button,WingBlank } from 'antd-mobile';
import { createForm } from 'rc-form';
import * as UtilAPI from '../../utils/utils.js';
import * as FormAPI from '../../utils/form.js';
import styles from './Login.less'; const rules = {
name: [{ required:true, message:"请输入手机号码" },{ pattern: /^1[3|5|7|8|9][0-9]\d{8}$/, message: "手机号码格式不正确"}],
code:[{required:true,message:"条形码不能为空"}],
msg:[{required:true,message:'短信验证码不能为空'}]
}; class Login extends Component {
constructor(props){
super(props);
this.getFieldProps = this.props.form.getFieldProps;
this.saveBaseInfo = this.saveBaseInfo.bind(this);
this.handleScanInput =this.handleScanInput.bind(this); } handleScanInput(){
//调微信sdk接口
} saveBaseInfo(){
const ruleFields = FormAPI.getRuleFields(rules);
console.log(ruleFields);
const form = this.props.form;
this.props.form.validateFields(ruleFields,(error,value)=>{
let flag = false;
for(var key in error){
if(error[key].errors.length > 0 ){
UtilAPI.Toasting({type:"info", msg: error[key].errors[0].message});
flag = true;
return;
}
}
if(!flag){
let formdata = FormAPI.getFormData(form.getFieldsValue());
this.props.dispatch({
type:'login/saveItem',
payload:{
itemType:"baseinfo",
itemData:{
baseinfo:{
...formdata
}
}
}
})
}
}) }
render(){
return(
<div className={styles.main}>
<NavBar className="top-nav-bar">登录{this.props.token}</NavBar>
<WhiteSpace />
<WingBlank>
<List>
{<InputItem clear placeholder="手机号码" {...this.getFieldProps('name', { initialValue: this.props.baseinfo.name,rules: rules["name"]})} className="item-required">手机号码</InputItem>}
</List>
<WhiteSpace />
<List>
{<InputItem clear placeholder="扫描条形码" {...this.getFieldProps('code',{initialValue:this.props.baseinfo.code,rules:rules["code"]})} className="item-required" onClick={this.handleScanInput}>扫描条形码</InputItem>}
</List>
<WhiteSpace />
<List>
{<InputItem clear placeholder="短信验证码" {...this.getFieldProps('msg',{initialValue:this.props.baseinfo.msg,rules:rules["msg"]})} className="item-required" >短信验证码</InputItem>}
</List>
<WhiteSpace />
<Button type="primary">获取短信验证码</Button> <WhiteSpace /> <Button type="primary" onClick={ this.saveBaseInfo }>登录</Button>
</WingBlank> </div>
);
}
} function mapStateToProps(state){
const { baseinfo,token } = state.login || {}; return {
baseinfo : baseinfo || {},
token : token
};
} export default connect(mapStateToProps)(createForm()(Login));

  

react dva 表单校验的更多相关文章

  1. React-Antd4的Form表单校验

    之前很少用react做项目,最近入职新公司,用的react,在自己的摸索过程中,慢慢会记录一些使用方法.今天简单记录一下使用antd 4.0版本的Form表单校验,直接代码. 需要购买阿里云产品和服务 ...

  2. 利用jquery.validate以及bootstrap的tooltip开发气泡式的表单校验组件

    表单校验是页面开发中非常常见的一类需求,相信每个前端开发人员都有这方面的经验.网上有很多成熟的表单校验框架,虽然按照它们默认的设计,用起来没有多大的问题,但是在实际工作中,表单校验有可能有比较复杂的个 ...

  3. AngularJS 1.2.x 学习笔记(表单校验篇)

    https://my.oschina.net/cokolin/blog/526911 摘要: 本文首发于 blog.csdn.net/vipshop_ebs/article/details/39472 ...

  4. angularJs表单校验(超级详细!!!)

    html代码 <!DOCTYPE html> <html ng-app="angularFormCheckModule"> <head> < ...

  5. bootstrap+jQuery.validate表单校验

    谈谈表单校验 这大概是一种惯例,学习前台后台最开始接触的业务都是用户注册和登录.现在社会坚持以人为本的理念,在网站开发过程同样如此.User是我们面对较多的对象,也是较核心的对象.最开始的用户注册和登 ...

  6. 关于jQuery表单校验的应用

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  7. php 表单校验函数库(判断email格式是否正确、http地址是否合法有效、手机号码是否合法)

    /** * 表单校验函数库 */ /** * 判断email格式是否正确 * @param $email */ function is_email($email) { return strlen($e ...

  8. 关于jQuery表单校验

    <style> .red{border: 1px solid red;} .wrong-tip{color: red;} </style> <form action=&q ...

  9. jQuery.validate表单校验+bootstrap

    谈谈表单校验 这大概是一种惯例,学习前台后台最开始接触的业务都是用户注册和登录.现在社会坚持以人为本的理念,在网站开发过程同样如此.User是我们面对较多的对象,也是较核心的对象.最开始的用户注册和登 ...

随机推荐

  1. 怎样查看web软件例如apache的连接数

    查看连接总数和当前的连接数 netstat -ant | grep $ip:80 | wc -l netstat -ant | grep $ip:80 | grep EST | wc -l 查看IP访 ...

  2. 第15课 栏目的排序处理(组件化) Thinkphp5商城第四季

    目录 要实现的功能 思路: 视图层 控制器里: 扩展函数里 要实现的功能 用表单里的提交过来的sort数据,批量修改表里的排序值 界面效果: 思路: 视图层表单提交数据主键=>sort值 控制器 ...

  3. 模块之 logging模块 time 模块 random模块 sys模块 pickle模块

    1.如果执行文件不在项目根目录下,需要添加项目根目录到sys.path中2.调用业务逻辑 2.logging模块 程序日志是 什么时间发生了什么事情,以及当时的情况 不是logging的话 记录日志的 ...

  4. Pychram基本操作

    1. 更改pychram页面为黑色背景主题.更改主题: File ->Settings -> Editor -> Color Scheme -> Scheme -> Mo ...

  5. 动态拼接SQL语句

    1.参考官方文档 ? if:字符判断 ? choose (when, otherwise):分支选择 ? trim (where, set):字符串截取:其中where标签封装查询条件,set标签封装 ...

  6. libevent 信号事件实现方式

    学会使用libevent,才能真正的掌握其是实现原理,我们先从一个简短的测试用例开始: #include <sys/types.h> #include <sys/stat.h> ...

  7. 笔记-python-__new__()

    笔记-python-__new__() 1.       __new__() __new__() 是在新式类中新出现的方法,它作用在构造方法建造实例之前. 验证代码: class Person(obj ...

  8. 【01】《html5权威指南》(扫描版)(全)

    [01]<html5权威指南>(扫描版)(全) []魔芋:无高清电子书.   只看第五部分,高级功能. 作者:(美)弗里曼 著,谢延晟,牛化成,刘美英 译 [美]adam freeman ...

  9. 【palindrome partitioning II】cpp

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  10. leetcode 【 Find Minimum in Rotated Sorted Array II 】python 实现

    题目: Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? W ...