initial commit

This commit is contained in:
2025-09-29 23:53:04 +02:00
commit 085b645265
13 changed files with 476 additions and 0 deletions
+67
View File
@@ -0,0 +1,67 @@
package ui
import "github.com/gofiber/fiber/v2"
type InputField struct {
ID string
Legend string
Type string
Placeholder string
Name string
Value string
Endpoint string
Trigger string
Hint string
InputClass string
}
func NewInputField(id, name, legend, placeholder, endpoint string) InputField {
return InputField{
ID: id,
Legend: legend,
Type: "text",
Placeholder: placeholder,
Name: name,
Value: "",
Endpoint: endpoint,
Trigger: "blur",
Hint: "",
InputClass: "",
}
}
func (f InputField) ToMap() fiber.Map {
return fiber.Map{
"ID": f.ID,
"Legend": f.Legend,
"Type": f.Type,
"Placeholder": f.Placeholder,
"Name": f.Name,
"Value": f.Value,
"Endpoint": f.Endpoint,
"Trigger": f.Trigger,
"Hint": f.Hint,
"InputClass": f.InputClass,
}
}
func (f InputField) WithValue(value string) InputField {
f.Value = value
return f
}
func (f InputField) WithError(hint string) InputField {
f.Hint = hint
f.InputClass = "input-error text-error"
return f
}
func (f InputField) WithType(inputType string) InputField {
f.Type = inputType
return f
}
func (f InputField) WithTrigger(trigger string) InputField {
f.Trigger = trigger
return f
}
+35
View File
@@ -0,0 +1,35 @@
package ui
// Aquí puedes añadir helpers predefinidos para campos comunes
// UsernameField crea un campo de username preconfigurado
func UsernameField() InputField {
return NewInputField(
"username-container",
"username",
"Nombre de usuario",
"juan.01",
"/validate/username",
)
}
// EmailField crea un campo de email preconfigurado
func EmailField() InputField {
return NewInputField(
"email-container",
"email",
"Correo electrónico",
"juan.01@email.com",
"/validate/email",
).WithType("email")
}
func PasswordField() InputField {
return NewInputField(
"password-container",
"password",
"Contraseña",
"••••••••",
"/validate/password",
).WithType("password")
}
+75
View File
@@ -0,0 +1,75 @@
package ui
import (
"errors"
"fmt"
"github.com/go-playground/validator/v10"
"regexp"
)
// Instancia global del validador
var validate *validator.Validate
func init() {
validate = validator.New()
_ = validate.RegisterValidation("username", validateUsername)
}
// GetValidator devuelve la instancia del validador
func GetValidator() *validator.Validate {
return validate
}
// Validación personalizada de username (ejemplo)
func validateUsername(fl validator.FieldLevel) bool {
value := fl.Field().String()
re := regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._-]*[a-zA-Z0-9_]$`)
if !re.MatchString(value) {
return false
}
if regexp.MustCompile(`[.-]{2}`).MatchString(value) {
return false
}
return true
}
var ErrorMessages = map[string]string{
"required": "Este campo es obligatorio",
"min": "Debe tener al menos %s caracteres",
"max": "No puede tener más de %s caracteres",
"email": "Correo electrónico no válido",
"alphanum": "Solo se permiten letras y números",
"alpha": "Solo se permiten letras",
"numeric": "Solo se permiten números",
"len": "Debe tener exactamente %s caracteres",
"url": "Formato de URL no válido",
"containsany": "Debe contener al menos uno de estos caracteres: %s",
"username": "Formato de usuario no válido",
}
// translateError convierte un error de validación a mensaje legible
func translateError(err validator.FieldError) string {
template, exists := ErrorMessages[err.Tag()]
if !exists {
return "Valor inválido"
}
// Si el mensaje tiene placeholder, reemplazar con el parámetro
if err.Param() != "" {
return fmt.Sprintf(template, err.Param())
}
return template
}
// GetFirstError extrae el primer error de validación
func GetFirstError(err error) string {
var validationErrors validator.ValidationErrors
if errors.As(err, &validationErrors) {
if len(validationErrors) > 0 {
return translateError(validationErrors[0])
}
}
return "Error de validación"
}