When implementing form parameter validators, you must define the CheckParams method on a value receiver, not a pointer receiver.
Why? The validator is automatically registered in the global container during program startup. If you use a pointer receiver (e.g., func (r *Register) CheckParams(...)), the first successful request will bind the request's data to the instance stored in the container. Subsequent requests will then access the data from the previous request, leading to data pollution and security issues.
Correct Pattern:
type Register struct {
Base
Pass string `form:"pass" json:"pass" binding:"required,min=3,max=20"`
Phone string `form:"phone" json:"phone" binding:"required,len=11"`
}
// Use a value receiver (r Register)
func (r Register) CheckParams(context *gin.Context) {
// ...
}
type Register struct {
Base
Pass string `form:"pass" json:"pass" binding:"required,min=3,max=20"`
Phone string `form:"phone" json:"phone" binding:"required,len=11"`
}
// Correct: Value receiver
func (r Register) CheckParams(context *gin.Context) {
// ...
}
// Incorrect: Pointer receiver (causes data pollution)
func (r *Register) CheckParams(context *gin.Context) {
// ...
}