From: Andrea Zagli Date: Wed, 10 Aug 2022 10:55:13 +0000 (+0200) Subject: Form: gestione campi data. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=8f6179109104881abe2f38d1a1d5a59b3703d232;p=zakegg_go Form: gestione campi data. --- diff --git a/form.go b/form.go index e61a485..1e55fb9 100644 --- a/form.go +++ b/form.go @@ -111,6 +111,13 @@ func ValidatorFormErrorsTranslations(validate *validator.Validate, trans ut.Tran }, func(ut ut.Translator, fe validator.FieldError) string { t, _ := ut.T("numeric_it", fe.Field()) + return t + }) + validate.RegisterTranslation("mydate", trans, func(ut ut.Translator) error { + return ut.Add("mydate", "Data non valida.", true) + }, func(ut ut.Translator, fe validator.FieldError) string { + t, _ := ut.T("mydate", fe.Field()) + return t }) } @@ -146,6 +153,9 @@ func Map2FormElements(s map[string]interface{}, f map[string]*FormElement) { } else if refv.Kind() == reflect.Float32 || refv.Kind() == reflect.Float64 { vg.Value = fmt.Sprintf("%v", v) vg.Value = strings.ReplaceAll(vg.Value, ".", ",") + /* TODO */ + } else if strings.Contains(vg.Validation, "mydate") { + vg.Value = v.(time.Time).Format("02/01/2006") } else { vg.Value = fmt.Sprintf("%v", v) } @@ -193,6 +203,14 @@ func CreaMapDb (elems map[string]*FormElement) (map[string]interface{}) { /* tolgo la formattazione */ campi[elem.DBName] = strings.ReplaceAll(elem.Value, ".", "") campi[elem.DBName] = strings.ReplaceAll(campi[elem.DBName].(string), ",", ".") + } else if strings.Contains(elem.Validation, "mydate") && elem.Value != "" { + /* metto la data in formato sql */ + t, err := time.Parse("02/01/2006", elem.Value) + if err == nil { + campi[elem.DBName] = t.Format("2006-01-02") + } else { + fmt.Println(err) + } } else { campi[elem.DBName] = elem.Value }