application

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: 7 Imported by: 0

Documentation

Overview

This package belongs to the form module.

The Types defined in this application packages are meant to be used in your controller to process forms

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetUnsubmittedForm

func GetUnsubmittedForm(ctx context.Context, r *web.Request, service domain.FormService) (domain.Form, error)

GetUnsubmittedForm: Use this if you need an unsubmitted form

func ProcessFormRequest

func ProcessFormRequest(ctx context.Context, r *web.Request, formService domain.FormService) (domain.Form, error)

ProcessFormRequest: Parses and Validates a Request to a Form - with the Help of the passed FormService

It calls the ParseFormData() method of the passed formService. Also, in case the form was submitted, it calls ValidateFormData() method of the formService (passing the parsed data)

Validation is only called if request was send via "POST". Also you can skip validation by passing a "novalidate" parameter. (In this cases form.IsSubmitted stays false)

func SimpleProcessFormRequest

func SimpleProcessFormRequest(ctx context.Context, r *web.Request) (domain.Form, error)

SimpleProcessFormRequest: Parses Post Values and returns a Form object with ALL the submitted data as simple map (string of strings) can be used if you dont need or want advanced form processing and validation. This method don't need a "domain.FormService"

Example
package main

import (
	"fmt"

	"context"
	"net/http"

	"strings"

	"flamingo.me/flamingo/core/form/application"
	"flamingo.me/flamingo/framework/web"
)

func main() {

	httpRequest, _ := http.NewRequest("POST", "?test=demo", strings.NewReader(""))

	flamingoWebRequest := web.RequestFromRequest(httpRequest, nil)
	form, _ := application.SimpleProcessFormRequest(context.Background(), flamingoWebRequest)

	fmt.Printf("%v\n", form.IsSubmitted)
	fmt.Print(form.Data.(map[string]string)["test"])

}
Output:

true
demo

func ValidationErrorsToValidationInfo

func ValidationErrorsToValidationInfo(err error) domain.ValidationInfo

ValidationErrorsToValidationInfo

Use this if you want to convert a error object to the domain.ValidationInfo

Its main purpose is to be used with the package @see gopkg.in/go-playground/validator.v9 (InvalidValidationError / ValidationErrors )

Example
type (
	CustomerEditFormData struct {
		FirstName string `validate:"required"`
		LastName  string `validate:"required"`
		Title     string ``
	}
)
formData := CustomerEditFormData{}
//validate - result from package validator "gopkg.in/go-playground/validator.v9"
validate := validator.New()
result := application.ValidationErrorsToValidationInfo(validate.Struct(formData))

fmt.Printf("%v\n", result.IsValid)
fmt.Printf(result.FieldErrors["firstName"][0].MessageKey)
Output:

false
formerror_firstName_required

Types

This section is empty.

Jump to

Keyboard shortcuts

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