formValidation
5 分钟阅读
Form validation 表单验证
Form validation 表单验证
The Form validation module is used for data validation and error collection.
表单验证模块用于数据验证和错误收集。
Installing and testing 安装和测试
Installing:
安装:
go get github.com/beego/beego/v2/core/validation
Testing:
测试:
go test github.com/beego/beego/v2/core/validation
Localization 本地化
In order to localize validation error messages, one might use SetDefaultMessage function of the validation package.
为了本地化验证错误消息,可以使用 SetDefaultMessage 包的 validation 函数。
Note that format markers (%d, %s) must be preserved in translated text to provide resulting messages with validation context values.
请注意,格式标记( %d 、 %s )必须保留在翻译后的文本中,以便为结果消息提供验证上下文值。
Default template messages are present in validation.MessageTmpls variable.
默认模板消息存在于 validation.MessageTmpls 变量中。
Simple message localization for Russian language:
俄语的简单消息本地化:
| |
Examples: 示例:
Direct use:
直接使用:
| |
Use through StructTag
通过 StructTag 使用
| |
Available validation functions in StrucTag:
StrucTag 中可用的验证函数:
Requirednot empty. :TODO 不为空,即各个类型要求不为其零值Min(min int)minimum value. Valid type isint, all other types are invalid.Min(min int)最小值。有效类型为int,所有其他类型无效。Max(max int)maximum value. Valid type isint, all other types are invalid.Max(max int)最大值。有效类型为int,所有其他类型无效。Range(min, max int)Value range. Valid type isint, all other types are invalid.Range(min, max int)值范围。有效类型为int,所有其他类型无效。MinSize(min int)minimum length. Valid type isstring slice, all other types are invalid.MinSize(min int)最小长度。有效类型为string slice,所有其他类型无效。MaxSize(max int)maximum length. Valid type isstring slice, all other types are invalid.MaxSize(max int)最大长度。有效类型为string slice,所有其他类型无效。Length(length int)fixed length. Valid type isstring slice, all other types are invalid.Length(length int)固定长度。有效类型为string slice,所有其他类型无效。Alphaalpha characters. Valid type isstring, all other types are invalid.Alpha字母字符。有效类型为string,所有其他类型无效。Numericnumerics. Valid type isstring, all other types are invalid.Numeric数字。有效类型为string,所有其他类型无效。AlphaNumericalpha characters or numerics. Valid type isstring, all other types are invalid.AlphaNumeric字母字符或数字。有效类型为string,所有其他类型无效。Match(pattern string)regex matching. Valid type isstring, all other types will be cast to string then match. (fmt.Sprintf("%v", obj).Match)Match(pattern string)正则表达式匹配。有效类型为string,所有其他类型将转换为字符串然后匹配。(fmt.Sprintf("%v", obj).Match)AlphaDashalpha characters or numerics or-_. Valid type isstring, all other types are invalid.AlphaDash字母字符或数字或-_。有效类型为string,所有其他类型无效。EmailEmail address. Valid type isstring, all other types are invalid.Email电子邮件地址。有效类型为string,所有其他类型无效。IPIP address,Only support IPv4 address. Valid type isstring, all other types are invalid.IPIP 地址,仅支持 IPv4 地址。有效类型为string,所有其他类型无效。Base64base64 encoding. Valid type isstring, all other types are invalid.Base64base64 编码。有效类型为string,所有其他类型无效。Mobilemobile number. Valid type isstring, all other types are invalid.Mobile手机号码。有效类型为string,所有其他类型无效。Teltelephone number. Valid type isstring, all other types are invalid.Tel电话号码。有效类型为string,所有其他类型无效。Phonemobile number or telephone number. Valid type isstring, all other types are invalid.Phone手机号码或电话号码。有效类型为string,所有其他类型无效。ZipCodezip code. Valid type isstring, all other types are invalid.ZipCode邮政编码。有效类型为string,所有其他类型无效。
API doc API 文档
Please see Go Walker
请参阅 Go Walker