From 69f718499eeeed5d519bc1428eea9246b0a5284c Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Wed, 17 Aug 2022 12:21:15 +0200 Subject: [PATCH] Form: aggiunto validator PIVA. --- form.go | 16 ++++++++++++++++ go.mod | 1 + 2 files changed, 17 insertions(+) diff --git a/form.go b/form.go index 4b91506..ba47984 100644 --- 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 4d46265..fbe1100 100644 --- 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 ( -- 2.49.0