fix
This commit is contained in:
@@ -27,21 +27,21 @@ func InitGlobalValidator() {
|
||||
once.Do(func() {
|
||||
// 1. 创建新的校验器实例
|
||||
globalValidator = validator.New()
|
||||
|
||||
|
||||
// 2. 创建中文翻译器
|
||||
zhLocale := zh.New()
|
||||
uni := ut.New(zhLocale, zhLocale)
|
||||
globalTranslator, _ = uni.GetTranslator("zh")
|
||||
|
||||
|
||||
// 3. 注册官方中文翻译
|
||||
zh_translations.RegisterDefaultTranslations(globalValidator, globalTranslator)
|
||||
|
||||
|
||||
// 4. 注册自定义校验规则
|
||||
RegisterCustomValidators(globalValidator)
|
||||
|
||||
|
||||
// 5. 注册自定义中文翻译
|
||||
RegisterCustomTranslations(globalValidator, globalTranslator)
|
||||
|
||||
|
||||
// 6. 设置到Gin全局校验器(确保Gin使用我们的校验器)
|
||||
if binding.Validator.Engine() != nil {
|
||||
// 如果Gin已经初始化,则替换其校验器
|
||||
@@ -78,11 +78,11 @@ type RequestValidator struct {
|
||||
func NewRequestValidator(response interfaces.ResponseBuilder) interfaces.RequestValidator {
|
||||
// 确保全局校验器已初始化
|
||||
InitGlobalValidator()
|
||||
|
||||
|
||||
return &RequestValidator{
|
||||
response: response,
|
||||
translator: globalTranslator, // 使用全局翻译器
|
||||
validator: globalValidator, // 使用全局校验器
|
||||
translator: globalTranslator, // 使用全局翻译器
|
||||
validator: globalValidator, // 使用全局校验器
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user