Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

表单统一校验 #25

Open
mominger opened this issue Jun 13, 2020 · 0 comments
Open

表单统一校验 #25

mominger opened this issue Jun 13, 2020 · 0 comments

Comments

@mominger
Copy link
Owner

表单校验

  • 采用表单校验配置文件,进行校验
  • 配置文件 formRule.config.js, 校验文件 graceChecker.js

文件简解

1. graceChecker.js

graceChecker_chat

原理是,通过正则表达式去验证,不通过抛出配置文件对应的错误信息。
它有内置验证,比如phoneno,代表验证手机号;支持自己传入正则表达式,类型reg;支持传入部分验证规则,比如类型string,传入长度验证。

2. formRule.config.js

formrule_chat

原理是,传输待校验的表单json数据对象,通过name字段匹配到formRule.config.js里的验证规则,graceChecker根据校验类型checkType,执行内置或传入的检验规则checkRule

3.代码示例

    import graceChecker from '@/common/graceChecker.js'
    import formRuleConfig from '@/config/formRule.config.js'

    export default {
		onLoad(){
            this.checkForm = {phoneNo:"18120119568",code:1223}
            let checkRes = graceChecker.check(this.checkForm, formRuleConfig.sendCodeRule);
            if (!checkRes) {
				uni.showToast({
					title: graceChecker.error,
					icon: "none"
				});

            }
		}
    }

反馈与建议

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant