validator

package
v0.0.0-...-724bf7d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 17, 2022 License: MIT Imports: 2 Imported by: 0

README

Validator 数据验证模块

用于处理和用户有关的数据模型

Validator 验证器对象

验证器对象是一个用于集成的用户数据验证基础结构

提供了初始化,绑定 Http request,验证并存储用户输出数据错误的接口

//继承对象
type Form stuct{
    Validator.Validator
    httprequest *http.Request
}

//实现InitWithRequest方法
func (model *Form) InitWithRequest(r *http.Request) error {
    model.httprequest=r
    return nil
}

//设置字段名称,可以为Model设置错误提示时每个字段的名称。需要每次在实例化的时候添加
//没有设置过的字段名称会以属性名表示
var FormFieldLabels=map[string]strint{
    "field1":"Field 1",
    "field2:"Field 2"
}

//表单对应的ID
const FormID="formid"

//创建新Form对象
func New() *Form{
    form:=&Form{}
    form.SetComponentI(ui.MapLabels(FormFieldLabels))
    //设置表单ID,便于在需要的时候快速处理和创建表单
    form.SetComponentID(FormID)
}

使用 验证器 对象

//创建新表单
form:=NewForm()

//验证字段。第一个参数为false的话,会为model添加名称为第二参数,值为第三参数的错误
form.ValidateField(form.field1==1,"field1","field1 must be 1")
//验证字段。第一个参数为false的话,会为model添加名称为第二参数,值为第三参数的错误
//传入的字符串有两个特殊的占位符
//{{field}}代表原始的Field名
//{{label}}代表通过SetFieldLabels设置的字段名
form.ValidateFieldf(form.field1==1,"field1","{{label}} must be 1")

//添加不转换的错误信息
form.AddPlainError("field", "error msg")
//添加只转换字段信息的错误信息
form.AddError("field", "error msg")
//添加转换字段信息和错误信息
//传入的字符串有两个特殊的占位符
//{{label}}代表原始的Field名
//{{field}}代表通过SetFieldLabels设置的字段名
form.AddErrorf("field", " {{label}} error msg")

//判断model对象是否有错误
TrueOrFalse:=form.HasError()
//返回model的所有错误
errors:=form.Errors()

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoValidateMethod = errors.New("error no validate method for model")

ErrNoValidateMethod error rasied when model validate method not overrided.

Functions

func MustValidate

func MustValidate(m Fields) bool

MustValidate return model validate result. Panic if any error raised.

Types

type FieldError

type FieldError struct {
	//Field field name.
	Field string
	//Label field label.
	//If field not found in model's labels,field label is same as field name.
	Label string
	//Msg error message
	Msg string
}

FieldError field error info struct

type Fields

type Fields interface {
	ui.TranslationLanguage
	ui.Component
	ui.ComponentLabels
	//HasError return if model has any error.
	HasError() bool
	//Errors return error list of model
	Errors() []*FieldError
	//AddError add error by given field and plain msg.
	AddError(field string, msg string)
	//AddErrorf add error by given field and formatted msg.
	AddErrorf(field string, msg string)
	//Validate method used to validate model.
	//Fail validation will add error to model.
	//Return any error if rasied.
	Validate() error
	//GetFieldLabel get label by given label name.
	GetFieldLabel(field string) string
}

Fields interface for model that can be validated.

type Validator

type Validator struct {
	ui.Language
	ui.LabelsComponent
	// contains filtered or unexported fields
}

Validator model struct.

func (*Validator) AddError

func (v *Validator) AddError(field string, msg string)

AddError add error by given field and plain msg. Msg will be translated.

func (*Validator) AddErrorf

func (v *Validator) AddErrorf(field string, msg string)

AddErrorf add error by given field and formatted msg. Msg will be translated.

func (*Validator) AddPlainError

func (v *Validator) AddPlainError(field string, msg string)

AddPlainError add plain error with given field and msg. Msg will not be translated.

func (*Validator) AddRawError

func (v *Validator) AddRawError(field string, label string, msg string)

func (*Validator) ComponentID

func (v *Validator) ComponentID() string

ComponentID get ui component id

func (*Validator) Errors

func (v *Validator) Errors() []*FieldError

Errors return error list of model

func (*Validator) GetFieldLabel

func (v *Validator) GetFieldLabel(field string) string

GetFieldLabel get label by given label name. Return field name itself if not found in field labels of model.

func (*Validator) HasError

func (v *Validator) HasError() bool

HasError return if model has any error.

func (*Validator) Validate

func (v *Validator) Validate() error

Validate method used to validate model. Fail validation will add error to model. Return any error if rasied. You must override this method for your own model,otherwise ErrNoValidateMethod will be raised.

func (*Validator) ValidateField

func (v *Validator) ValidateField(validated bool, field string, msg string)

ValidateField validated field then add error with given field name and plain msg if not validated.

func (*Validator) ValidateFieldMessagef

func (v *Validator) ValidateFieldMessagef(validated bool, field string, msg ui.Translatable)

ValidateFieldMessagef validated field then add error with given field name and string interfcae msg if not validated.

func (*Validator) ValidateFieldf

func (v *Validator) ValidateFieldf(validated bool, field string, msg string)

ValidateFieldf validated field then add error with given field name and formatted msg if not validated.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL