


What is the most difficult public business to implement in WeChat mini programs? It should be form verification. This article mainly introduces how the WeChat applet implements the form verification function. It has certain reference value. Interested friends can refer to
Mini Program SDK version 1.4
Difficulties in form verification
If you want to ask what is the most difficult public business to implement in WeChat mini programs? It should be form verification, nothing else. The reasons are as follows:
The number of form components reaches 11, ranking first among all types of components. Fortunately, not all of them need to be verified.
The operation methods of these components are diverse, which can be divided into sliding, (multi-line) input, clicking, and clicking + sliding.
Even if it is the same component, there will be different verification rules due to different business scenarios.
What’s even more troublesome is that these components are often linked or associated with each other for verification.
…
However, as a non-simple static page, there is a small program with a lot of user interaction, and form verification is a very common function: login, registration, new addition, editing…
In short: Diversity of form components
Try componentization
If you pay attention to the front-end development trend in recent years, you will definitely think of "componentization" to achieve:
The views, styles, and validation logic of each form component are encapsulated into separate business components and then called directly.
But things don’t seem to be that simple.
If we consider abstracting n native components, adding n verification rules, and then multiplying by n (full arrangement) of the relationship between components, the complexity will reach at least n³.
And each component’s verification failure or success must notify the parent component so that the error message can be displayed or the next step can be performed.
Not only does this not solve the problem, but it also makes these public form components too complex and confusingly coupled.
Try non-componentization
Since the original idea doesn’t work, let’s go back to the starting point and see what our core needs to be abstracted .
It’s nothing more than two things: the element style of the view layer and the verification rules of the logic layer.
As mentioned above, encapsulating native form components will greatly increase the complexity. Simply abandon it, and the complexity can be reduced to n² in an instant.
But at the same time, we must maintain a unified style, which is what we often call consistent style.
For example, how high should the input box be, how to display error prompts, font size and color...etc.
This is easy to do. We write the style class into a public style file form.wxss, and then import it when needed, or even import it globally.
// form.wxss .form { display: block; font-size: 28rpx; position: relative; } .form-line { background-color: #fff; border-bottom: 1px solid #e5e5e5; font-size: 34rpx; height: 96rpx; line-height: 96rpx; display: flex; padding: 0 31rpx; } .form-title { box-sizing: border-box; background-color: #efefef; color: #838383; font-size: 28rpx; padding: 31rpx; min-height: 90rpx; } ...
When we use it, we only need to add the corresponding style to the corresponding element. For example:
// xxx.wxml <form class="form"> <view class="form-title">请输入手机号</view> <view class="form-line"> <label class="label">手机</label> <view class="form-control"> <input class="f-1 va-m input" /> </view> </view> ... </form>
Then we only have two problems left between verification rules and component associations.
The ideal state of verification rules is extensible and configurable.
Scalable. As your business grows, you can add new verification rules without modifying existing rules.
Configurable. Different single or multiple validation rules can be configured individually for each form component.
How to make it definable? Just use a unified form. For example:
/* 统一的格式: [规则名]: { rule: [校验方式] msg: [错误信息] } */ const validators = { // 简单的校验用正则 required: { rule: /.+/, msg: '必填项不能为空' }, // 复杂的校验用函数 same: { rule (val='', sVal='') { return val===this.data[sVal] }, msg: '密码不一致' } ... }
How to make it configurable? The configuration supports an array-like form, and then uses a unified function to read these verification rules in sequence and verify them one by one.
The configured rules must be on the native form component, and the value of the component can only be obtained through the event object.
If you directly bind the event for verification, it will prevent the parent page from obtaining the value, so it is best to pass the value by the parent page binding event, and pass in the event object and execution environment for processing:
/* 校验函数部分代码 e 事件对象 context 页面对象函数执行的上下文环境 */ let validate = (e, context) => { // 从事件对象中获取组件的值 let value = (e.detail.value || '').trim() // 从事件中获取校验规则名称 let validator = e.currentTarget.dataset.validator ? e.currentTarget.dataset.validator .split(',') : [] // 遍历规则进行校验 for (let i = 0; i < validator.length; i++) { let ruleName = validator[i].split('=')[0] let ruleValue = validator[i].split('=')[1] let rule = validators[ruleName].rule || /.*/ if ('function' === typeof rule) { rule.call(context, value, ruleValue) ? '' : validators[ruleName].msg } else { rule.test(value) ? '' : validators[ruleName].msg } } ... }
It is also very simple to call. Add the corresponding style according to the fixed format, configure the verification rules, and then call the verification function.
// 部分代码示例 // page.wxml <form> <!-- 一个表单组件 --> <view class="form-line"> <label class="label">授权手机</label> <view class="form-control"> <!-- 校验规则:必须填写,且为电话号码 --> <input maxlength="11" class="f-1 va-m input" bindblur="validate" type="number" data-name="phone" data-validator="required,phone" confirm-type="next" value="{{phone}}" /> <!-- 错误图标 --> <icon wx:if="{{form.phone!==undefined}}" type="{{form.phone?'warn':'success'}}" size="16" /> </view> </view> ... </form> // page.js valid(e) { this.setData({ [e.currentTarget.dataset.name]: e.detail.value }) validate(e, this) }
Summary
The above is the detailed content of Example of implementation of form verification function in WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

随着移动互联网技术和智能手机的普及,微信成为了人们生活中不可或缺的一个应用。而微信小程序则让人们可以在不需要下载安装应用的情况下,直接使用小程序来解决一些简单的需求。本文将介绍如何使用Python来开发微信小程序。一、准备工作在使用Python开发微信小程序之前,需要安装相关的Python库。这里推荐使用wxpy和itchat这两个库。wxpy是一个微信机器

小程序能用react,其使用方法:1、基于“react-reconciler”实现一个渲染器,生成一个DSL;2、创建一个小程序组件,去解析和渲染DSL;3、安装npm,并执行开发者工具中的构建npm;4、在自己的页面中引入包,再利用api即可完成开发。

实现思路x01服务端的建立首先,在服务端,使用socket进行消息的接受,每接受一个socket的请求,就开启一个新的线程来管理消息的分发与接受,同时,又存在一个handler来管理所有的线程,从而实现对聊天室的各种功能的处理x02客户端的建立客户端的建立就要比服务端简单多了,客户端的作用只是对消息的发送以及接受,以及按照特定的规则去输入特定的字符从而实现不同的功能的使用,因此,在客户端这里,只需要去使用两个线程,一个是专门用于接受消息,一个是专门用于发送消息的至于为什么不用一个呢,那是因为,只

微信小程序是一种轻量级的应用程序,可以在微信平台上运行,不需要下载安装,方便快捷。Java语言作为一种广泛应用于企业级应用开发的语言,也可以用于微信小程序的开发。在Java语言中,可以使用SpringBoot框架和第三方工具包来开发微信小程序。下面是一个简单的微信小程序开发过程。创建微信小程序首先,需要在微信公众平台上注册一个小程序。注册成功后,可以获取到

本篇文章给大家带来了关于微信小程序的相关问题,其中主要介绍了如何在小程序中用公众号模板消息,下面一起来看一下,希望对大家有帮助。

PHP与小程序的地理位置定位与地图显示地理位置定位与地图显示在现代科技中已经成为了必备的功能之一。随着移动设备的普及,人们对于定位和地图显示的需求也越来越高。在开发过程中,PHP和小程序是常见的两种技术选择。本文将为大家介绍PHP与小程序中的地理位置定位与地图显示的实现方法,并附上相应的代码示例。一、PHP中的地理位置定位在PHP中,我们可以使用第三方地理位

随着小程序的广泛应用,越来越多的开发者需要将其与后台服务器进行数据交互,其中最常见的业务场景之一就是上传文件。本文将介绍在小程序中实现文件上传的PHP后台实现方法。一、小程序中的文件上传在小程序中实现文件上传,主要依赖于小程序APIwx.uploadFile()。该API接受一个options对象作为参数,其中包含了要上传的文件路径、需要传递的其他数据以及

PHP与小程序的第三方登录与绑定功能实现随着互联网的发展和智能手机的普及,小程序成为了移动应用程序开发的热门选择。小程序不仅提供了优秀的用户体验,还具备各种强大的功能。其中,第三方登录与绑定是小程序中常见的功能之一。本文将介绍如何使用PHP与小程序实现第三方登录与绑定的功能,并为读者提供代码示例。第三方登录是指用户可以使用其他平台的账号信息登录到目标平台,而


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools