domain

package
v0.0.0-...-fd23dd1 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2019 License: OSL-3.0 Imports: 9 Imported by: 4

Documentation

Overview

Domain package of the "form" module.

The overall purpose (bounded context) of the form module is to provide models and functionality around processing web formulars. So this "domain" package contains core models and types for handling forms - as well as common validation funcs.

Added date and regex validators:

FormData struct {
	...
	DateOfBirth string `form:"dateOfBirth" validate:"required,dateformat,minimumage=18,maximumage=150"`
	Password 	string `form:"password" validate:"required,password"`
	...
}

Additional date validators: dateformat (default 2006-01-02), minimumage (by specifying age value) and maximumage (by specifying age value).

Regex validators can be specified as part of "customRegex" map. Each name defines validator tag, each value defines actual regex.

To setup up specific config, use: form:

validator:
	dateFormat: 02.01.2006
	customRegex:
		password: ^[a-z]*$

To initiate validator use:

type (

validatorProvider func() *validator.Validate
....

)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateAge

func ValidateAge(date string, age int) bool

ValidateAge - validates a date and checks if the date is older than given age - used for birthday validations for example Deprecated

func ValidateDate

func ValidateDate(date string) bool

ValidateDate validates a string representing a date from a input field type date (see https://www.w3.org/TR/2011/WD-html-markup-20110405/input.date.html) Deprecated

func ValidatorProvider

func ValidatorProvider(fieldValidators []FieldValidator, config *struct {
	DateFormat  string     `inject:"config:form.validator.dateFormat"`
	CustomRegex config.Map `inject:"config:form.validator.customRegex"`
}) *validator.Validate

ValidatorProvider as dingo provider It creates instance of validator.Validate and inject all custom validators into it.

Types

type Error

type Error struct {
	//Tag - contains the validation tag that failed. if the
	Tag string
	//MessageKey - a key of the error message. Often used to pass to translation func in the template
	MessageKey string
	//DefaultLabel - a speaking error label. OFten used to show to end user - in case no translation exists
	DefaultLabel string
}

Error - representation of an Error Message - intented usage is to display errors in the view to the end user

type FieldValidator

type FieldValidator interface {
	ValidatorName() string
}

FieldValidator as primary interface for defining custom field validation

type FieldValidatorWithParam

type FieldValidatorWithParam interface {
	ValidateWithParam(string, interface{}) bool
}

FieldValidatorWithParam as additional interface for defining field validation without additional parameter

type FieldValidatorWithoutParam

type FieldValidatorWithoutParam interface {
	ValidateWithoutParam(interface{}) bool
}

FieldValidatorWithoutParam as additional interface for defining field validation without additional parameter

type Form

type Form struct {
	//Data  the form Data Struct (Forms DTO)
	Data interface{}
	//ValidationInfo for the form
	ValidationInfo ValidationInfo
	//IsSubmitted  flag if form was submitted and this is the result page
	IsSubmitted bool
	//IsSuccess  - if IsValid && IsSubmitted && The planned Action was sucessfull (e.g. register user)
	IsSuccess bool
	//OriginalPostValues contain the original posted values
	OriginalPostValues url.Values
	//ValidationRules contains map with validation rules for all validatable fields
	ValidationRules map[string][]ValidationRule
}

Form represents a Form - its intended usage is to pass to your view

func (Form) GetErrorsForField

func (f Form) GetErrorsForField(name string) []Error

func (Form) GetOriginalPostValue1

func (f Form) GetOriginalPostValue1(key string) string

func (Form) GetValidationRulesForField

func (f Form) GetValidationRulesForField(name string) []ValidationRule

GetValidationRulesForField adds option to extract validation rules for desired field in templates

func (Form) HasAnyFieldErrors

func (f Form) HasAnyFieldErrors() bool

func (Form) HasErrorForField

func (f Form) HasErrorForField(name string) bool

func (Form) HasGeneralErrors

func (f Form) HasGeneralErrors() bool

func (Form) IsValidAndSubmitted

func (f Form) IsValidAndSubmitted() bool

type FormService

type FormService interface {
	//ParseFormData is responsible of mapping the passed formValues to your FormData Struct (Forms DTO)
	ParseFormData(ctx context.Context, r *web.Request, formValues url.Values) (interface{}, error)
}

FormService interface that need to be implemented, in case you want to use "application.ProcessFormRequest"

type GetDefaultFormData

type GetDefaultFormData interface {
	//GetDefaultFormData
	GetDefaultFormData(parsedData interface{}) interface{}
}

GetDefaultFormData interface

type GetDefaultFormDataWithContext

type GetDefaultFormDataWithContext interface {
	//GetDefaultFormDataWithContext
	GetDefaultFormDataWithContext(ctx context.Context, parsedData interface{}) interface{}
}

GetDefaultFormDataWithContext interface

type ValidateFormData

type ValidateFormData interface {
	//ValidateFormData is responsible to run validations on the Data, the returned error type can be a slice of errors. each error is converted to a validation Error
	ValidateFormData(data interface{}) (ValidationInfo, error)
}

ValidateFormData interface that need to be implemented, in case you want to use "application.ProcessFormRequest" with validation that doesn't include context

type ValidateFormDataWithContext

type ValidateFormDataWithContext interface {
	//ValidateFormDataWithContext is responsible to run validations on the Data, with provided context, the returned error type can be a slice of errors. each error is converted to a validation Error
	ValidateFormDataWithContext(ctx context.Context, data interface{}) (ValidationInfo, error)
}

ValidateFormDataWithContext interface that need to be implemented, in case you want to use "application.ProcessFormRequest" with validation that include context

type ValidationInfo

type ValidationInfo struct {
	//FieldErrors list of errors per form field.
	FieldErrors map[string][]Error
	//GeneralErrors list of general form errors, that are not related to any field
	GeneralErrors []Error
	//IsValid  flag if data was valid
	IsValid bool
}

ValidationInfo - represents the complete Validation Informations of your form. It can contain GeneralErrors and form field related errors.

func (*ValidationInfo) AddError

func (vi *ValidationInfo) AddError(messageKey string, defaultLabel string)

AddError adds a general error with the passed MessageKey and DefaultLabel

func (*ValidationInfo) AddFieldError

func (vi *ValidationInfo) AddFieldError(fieldName string, messageKey string, defaultLabel string)

AddFieldError -adds a Error with the passed messageKey and defaultLabel - for a (form)field with the given name

func (*ValidationInfo) AddGeneralUnknownError

func (vi *ValidationInfo) AddGeneralUnknownError(err error)

AddGeneralUnknownError - adds a general unknown error to the validation infos

type ValidationRule

type ValidationRule struct {
	Name  string
	Value string
}

ValidationRule - contains single validation rule for field. Name is mandatory (required|email|max|len|...), Value is optional and adds additional info (like "128" for "max=128" rule)

Jump to

Keyboard shortcuts

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