]> saetta.ns0.it Git - zakegg_go/commitdiff
Form: aggiunto validator PIVA.
authorAndrea Zagli <azagli@libero.it>
Wed, 17 Aug 2022 10:21:15 +0000 (12:21 +0200)
committerAndrea Zagli <azagli@libero.it>
Wed, 17 Aug 2022 10:21:15 +0000 (12:21 +0200)
form.go
go.mod

diff --git a/form.go b/form.go
index 4b915062cb2e38c552ed8cdffd4b5f5484eaea82..ba47984a7fa0dd69eb85e5d2428b0885e859b399 100644 (file)
--- a/form.go
+++ b/form.go
@@ -13,6 +13,7 @@ import (
        ut "github.com/go-playground/universal-translator"
 
        "github.com/squeeze69/codicefiscale"
+       "github.com/squeeze69/partitaiva"
 )
 
 type FormElement struct {
@@ -26,6 +27,8 @@ type FormElement struct {
        Options map[string]string
        OptionsSort []string
        Rows string
+       FileSize string
+       FileMimeType string
        Disabled bool
        Invisible bool
        Validation string
@@ -43,6 +46,12 @@ func ValidatorCodFisc(fl validator.FieldLevel) bool {
        return ok
 }
 
+func ValidatorPIva(fl validator.FieldLevel) bool {
+       ok, _ := partitaiva.ItPartitaIva(fmt.Sprintf("%s", fl.Field()))
+
+       return ok
+}
+
 func ValidatorNumeric(fl validator.FieldLevel) bool {
        regThou := regexp.MustCompile("^[-+]?\\d{1,3}(.\\d{3})*(\\,\\d{1,2})?$")
        regNoThou := regexp.MustCompile("^[-+]?\\d+(\\,\\d{1,2})?$")
@@ -100,6 +109,13 @@ func ValidatorFormErrorsTranslations(validate *validator.Validate, trans ut.Tran
 
                return t
        })
+       validate.RegisterTranslation("piva", trans, func(ut ut.Translator) error {
+               return ut.Add("piva", "Partita IVA non valida.", true)
+       }, func(ut ut.Translator, fe validator.FieldError) string {
+               t, _ := ut.T("piva", fe.Field())
+
+               return t
+       })
        validate.RegisterTranslation("numeric", trans, func(ut ut.Translator) error {
                return ut.Add("numeric", "Il campo deve contenere un numero.", true)
        }, func(ut ut.Translator, fe validator.FieldError) string {
diff --git a/go.mod b/go.mod
index 4d462651e90e7f57c7abb54e8c2133fab8a12d46..fbe1100e22ca2fdfe021f42c96d5b3aaf9ea40e2 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -7,6 +7,7 @@ require (
        github.com/go-playground/universal-translator v0.18.0
        github.com/go-playground/validator/v10 v10.11.0
        github.com/squeeze69/codicefiscale v1.0.2
+       github.com/squeeze69/partitaiva v1.0.1
 )
 
 require (