]> saetta.ns0.it Git - zakegg_go/commitdiff
Form: gestione campi data.
authorAndrea Zagli <azagli@libero.it>
Wed, 10 Aug 2022 10:55:13 +0000 (12:55 +0200)
committerAndrea Zagli <azagli@libero.it>
Wed, 10 Aug 2022 10:55:13 +0000 (12:55 +0200)
form.go

diff --git a/form.go b/form.go
index e61a48599e887b0a34844ae06416e9d7b4744dfa..1e55fb9e0153c3d826da356a601d2ec9a4a24d4e 100644 (file)
--- 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
                        }