Validation

Variables

var DefaultValidationKeys map[string]map[int]string

Register default validation keys for all calls to Controller.Validation.Func(). Map from (package).func => (line => name of first arg to Validation func) E.g. "myapp/controllers.helper" or "myapp/controllers.(*Application).Action" This is set on initialization in the generated main.go file.

func ValidationFilter

func ValidationFilter(c *Controller, fc []Filter)

Revel Filter function to be hooked into the filter chain.

type Validation

type Validation struct {
    Errors []*ValidationError
    // contains filtered or unexported fields
}

A Validation context manages data validation and error messages.

func (*Validation) Check

func (v *Validation) Check(obj interface{}, checks ...Validator) *ValidationResult

Apply a group of validators to a field, in order, and return the ValidationResult from the first one that fails, or the last one that succeeds.

func (*Validation) Clear

func (v *Validation) Clear()

Clear *all* ValidationErrors

func (*Validation) Email

func (v *Validation) Email(str string) *ValidationResult

func (*Validation) Error

func (v *Validation) Error(message string, args ...interface{}) *ValidationResult

Error adds an error to the validation context.

func (*Validation) ErrorMap

func (v *Validation) ErrorMap() map[string]*ValidationError

ErrorMap returns the errors mapped by key. If there are multiple validation errors associated with a single key, the first one "wins". (Typically the first validation will be the more basic).

func (*Validation) HasErrors

func (v *Validation) HasErrors() bool

HasErrors returns true if there are any (ie > 0) errors. False otherwise.

func (*Validation) Keep

func (v *Validation) Keep()

Keep tells revel to set a flash cookie on the client to make the validation errors available for the next request. This is helpful when redirecting the client after the validation failed. It is good practice to always redirect upon a HTTP POST request. Thus one should use this method when HTTP POST validation failed and redirect the user back to the form.

func (*Validation) Length

func (v *Validation) Length(obj interface{}, n int) *ValidationResult

func (*Validation) Match

func (v *Validation) Match(str string, regex *regexp.Regexp) *ValidationResult

func (*Validation) Max

func (v *Validation) Max(n int, max int) *ValidationResult

func (*Validation) MaxSize

func (v *Validation) MaxSize(obj interface{}, max int) *ValidationResult

func (*Validation) Min

func (v *Validation) Min(n int, min int) *ValidationResult

func (*Validation) MinSize

func (v *Validation) MinSize(obj interface{}, min int) *ValidationResult

func (*Validation) Range

func (v *Validation) Range(n, min, max int) *ValidationResult

func (*Validation) Required

func (v *Validation) Required(obj interface{}) *ValidationResult

Required tests that the argument is non-nil and non-empty (if string or list)

type ValidationError

type ValidationError struct {
    Message, Key string
}

Simple struct to store the Message & Key of a validation error

func (*ValidationError) String

func (e *ValidationError) String() string

String returns the Message field of the ValidationError struct.

type ValidationResult

type ValidationResult struct {
    Error *ValidationError
    Ok    bool
}

A ValidationResult is returned from every validation method. It provides an indication of success, and a pointer to the Error (if any).

func (*ValidationResult) Key

func (r *ValidationResult) Key(key string) *ValidationResult

Key sets the ValidationResult's Error "key" and returns itself for chaining

func (*ValidationResult) Message

func (r *ValidationResult) Message(message string, args ...interface{}) *ValidationResult

Message sets the error message for a ValidationResult. Returns itself to allow chaining. Allows Sprintf() type calling with multiple parameters