initial commit
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"templating/ui"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/template/html/v2"
|
||||
)
|
||||
|
||||
func main() {
|
||||
engine := html.New("./templates", ".html")
|
||||
engine.Reload(true)
|
||||
|
||||
app := fiber.New(
|
||||
fiber.Config{
|
||||
Views: engine,
|
||||
})
|
||||
|
||||
app.Get("/", func(c *fiber.Ctx) error {
|
||||
return c.Render("pages/index", fiber.Map{
|
||||
"Message": "Hello World",
|
||||
"UsernameField": ui.UsernameField().ToMap(),
|
||||
"EmailField": ui.EmailField().ToMap(),
|
||||
"PasswordField": ui.PasswordField().ToMap(),
|
||||
}, "layouts/base")
|
||||
})
|
||||
|
||||
type SomeForm struct {
|
||||
Username string `json:"username" validate:"required,min=3,max=30,username"`
|
||||
Email string `json:"email" validate:"required,email"`
|
||||
Password string `json:"password" validate:"required,min=8,max=20"`
|
||||
}
|
||||
|
||||
app.Post("/validate/username", func(c *fiber.Ctx) error {
|
||||
slog.Info(`validate username triggered`)
|
||||
|
||||
username := c.FormValue("username")
|
||||
input := SomeForm{
|
||||
Username: username,
|
||||
}
|
||||
|
||||
validate := ui.GetValidator()
|
||||
err := validate.StructPartial(input, "Username")
|
||||
if err != nil {
|
||||
field := ui.UsernameField().
|
||||
WithValue(username).
|
||||
WithError(ui.GetFirstError(err))
|
||||
return c.Render("components/input", field.ToMap())
|
||||
}
|
||||
|
||||
// Emular que el nombre de usuario existe
|
||||
if username == "juan" {
|
||||
slog.Info("username already exists")
|
||||
field := ui.UsernameField().
|
||||
WithValue(username).
|
||||
WithError("El nombre de usuario ya existe")
|
||||
return c.Render("components/input", field.ToMap())
|
||||
}
|
||||
|
||||
field := ui.UsernameField().WithValue(username)
|
||||
return c.Render("components/input", field.ToMap())
|
||||
})
|
||||
|
||||
app.Post("/validate/email", func(c *fiber.Ctx) error {
|
||||
slog.Info(`validate email triggered`)
|
||||
|
||||
email := c.FormValue("email")
|
||||
input := SomeForm{
|
||||
Email: email,
|
||||
}
|
||||
|
||||
validate := ui.GetValidator()
|
||||
err := validate.StructPartial(input, "Email")
|
||||
if err != nil {
|
||||
field := ui.EmailField().
|
||||
WithValue(email).
|
||||
WithError(ui.GetFirstError(err))
|
||||
return c.Render("components/input", field.ToMap())
|
||||
|
||||
}
|
||||
|
||||
field := ui.EmailField().WithValue(email)
|
||||
return c.Render("components/input", field.ToMap())
|
||||
})
|
||||
|
||||
app.Post("/validate/password", func(c *fiber.Ctx) error {
|
||||
slog.Info(`validate password triggered`)
|
||||
|
||||
password := c.FormValue("password")
|
||||
input := SomeForm{
|
||||
Password: password,
|
||||
}
|
||||
|
||||
validate := ui.GetValidator()
|
||||
err := validate.StructPartial(input, "Password")
|
||||
if err != nil {
|
||||
field := ui.PasswordField().
|
||||
WithValue(password).
|
||||
WithError(ui.GetFirstError(err))
|
||||
return c.Render("components/input", field.ToMap())
|
||||
}
|
||||
|
||||
field := ui.PasswordField().WithValue(password)
|
||||
return c.Render("components/input", field.ToMap())
|
||||
})
|
||||
|
||||
app.Listen(":3000")
|
||||
}
|
||||
Reference in New Issue
Block a user