/config/validators.js
123456789101112131415161718192021222324252627282930313233
module.exports = { validators : { form : { path: '/validatePost', // 需要验证的url,包括浏览器端的form提交前的验证,以及服务器端的中间件的验证 fields: [ { // 允许空的字段 name: 'allowEmpty', allowEmpty: true }, { // 不允许为空的字段,并且提供了验证的正则表达式 name: 'account', validate: /[0-9a-zA-Z]{6,}/ig, allowEmpty: false, msg: '不能为空' }, { // 不允许为空的字段,并且提供了验证的函数 name: 'number', validate: value => { return /\d+/.test(value) }, allowEmpty: false, msg: '必须为数字' } ], onError: msg => { alert(msg) } } }}
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
import * as React from 'react';import {validateForm} from "../../../client"import {validators} from '../../config/validators'import axios from 'axios'// hoc组件,通过配置的validation生成ValidateFormvar ValidateForm = validateForm(validators.form);class Page extends React.Component<any, any> { constructor(props: any) { super(props); this.state = {body : {}, result : {}}; } validateForm = null render() { return <div> <ValidateForm ref={instance => { this.validateForm = instance; }}> allow empty: <input type="text" name="allowEmpty"/><br/> account: <input type="text" name="account"/><br/> number: <input type="text" name="number"/><br/> <input type="submit" value="submit by posting form"/> <input type="button" value="get post data by validate" onClick={() => { var body = this.validateForm.getBody(); this.setState({body}) }} /> {/* 以下按钮绕过浏览器端验证直接提交到服务器,结果返回400 */} <input type="button" value="post data by ajax" onClick={async () => { try { var result = await axios({ url : validators.form.path, method : 'post', data : this.state.body }) this.setState({result : result.data}) } catch (error) { console.log(error) } }} /> </ValidateForm> <div> post body:{JSON.stringify(this.state.body)} </div> <div> result:{JSON.stringify(this.state.result)} </div> </div> }};export default Page