SoFunction
Updated on 2025-03-05

Detailed explanation of GoFrame implementation sequence verification example

introduction

In the previous postGoFrame data verification result | Error interface object, There is no sufficient introduction to the order and non-sequentiality verification.

This article fills in the pit left before, and we use map verification as an example:

Basic introduction

We learned from the previous article that the Error interface object method has the orderliness only when using the order verification rules, otherwise the returned result will be random.

Even if we useFirstItemFirstString()The same is true for other methods to obtain the verification results, and the returned verification results are not fixed.

Causes of disorder

Because we pass the verification rulesmaptype, andgolangofmapThe type is not ordered (the underlying data structure is hashmap), so the verification result is random like the rules, and the same verification method for the same verification result may be different from the return of the result value after multiple times.

Sequential verification

Let’s give you a chestnut:

If the verification result is not satisfiedrequiredThen return the corresponding error message, otherwise it will be the subsequent verification error message;

In other words, the returned error message should be in the same order as the rule I set.

The code example is as follows:

package main

import (
	"fmt"
	"/gogf/gf/v2/frame/g"
	"/gogf/gf/v2/os/gctx"
)

func main() {
	var (
		ctx    = ()
		params = map[string]interface{}{
			"passport":  "",
			"password":  "wangzhongyang",
			"password2": "wangyang",
		}
		rules = []string{
			"passport@required|length:6,16#Account cannot be empty | Account length should be between {min} and {max}",			"password@required|length:6,16|same:password2#Password cannot be empty | Password length should be between {min} and {max} | The password inputs are not equal when the two times",			"password2@required|length:6,16#",
		}
	)  
	err := ().Rules(rules).Data(params).Run(ctx)  
	if err != nil {
		(())
		(())
		(())
	}
}

After execution, the terminal outputs:

map[length: The account length should be between 6 and 16 required: The account cannot be empty]
passport map[length: The account length should be between 6 and 16 required: The account cannot be empty]
Account cannot be empty

It can be seen that the above execution results satisfy the order.

Let's summarize: If we want the verification result to satisfy the order, we only need torulesThe parameter type is set to[]string, set according to certain rules, andmsgsParameters can be defined torulesAmong the parameters, you can also pass them separately (using the third parameter).

rulesThis rule that satisfies the return of the sequential verification result is calledgvalid tag

The next article will analyze it for yougvalid tagknowledge points.

Summarize

Through this article, we have obtained the golden key to implement sequential verification: just need torulesThe parameter type is set to[]string, set according to certain rules, andmsgsParameters can be defined torulesParameters can also be passed in separately.

The above is the detailed explanation of the GoFrame implementation sequence verification example. For more information about GoFrame sequence verification, please pay attention to my other related articles!