lishwist/http/props/register.go
2026-02-14 12:29:47 +09:00

86 lines
1.9 KiB
Go

package props
import (
"html/template"
"github.com/Teajey/hmc"
"lishwist/http/templates"
)
type RegisterForm struct {
Username hmc.Input
Password hmc.Input
ConfirmPassword hmc.Input
}
type Register struct {
hmc.Namespace
Nav templates.NavCollapse
Error string `json:",omitempty" xml:",omitempty"`
Warning template.HTML `json:",omitempty" xml:",omitempty"`
Form hmc.Form[RegisterForm]
}
func (p *RegisterForm) Validate() (valid bool) {
valid = true
if p.Password.Value != p.ConfirmPassword.Value {
p.ConfirmPassword.Error = "Passwords didn't match"
valid = false
}
_ = p.Username.Validate()
_ = p.Password.Validate()
_ = p.ConfirmPassword.Validate()
if valid {
valid = p.Username.Error == "" && p.Password.Error == "" && p.ConfirmPassword.Error == ""
}
return
}
func NewRegister(usernameVal, passwordVal, confirmPassVal string) Register {
return Register{
Namespace: hmc.NS(),
Nav: templates.NavCollapse{
Links: []hmc.Link{{
Label: "Login",
Href: "/",
}},
},
Error: "",
Warning: template.HTML(`<p>Your password will be stored in a safe, responsible manner; but don't trust my programming skills!</p><p class="mb-0">Maybe use a password here that you don't use for important things...</p>`),
Form: hmc.Form[RegisterForm]{
Method: "POST",
Elements: RegisterForm{
Username: hmc.Input{
Label: "Username",
Name: "username",
Required: true,
MinLength: 4,
Value: usernameVal,
},
Password: hmc.Input{
Label: "Password",
Type: "password",
Name: "newPassword",
Required: true,
MinLength: 5,
Value: passwordVal,
},
ConfirmPassword: hmc.Input{
Label: "Confirm password",
Type: "password",
Name: "confirmPassword",
Required: true,
Value: confirmPassVal,
},
},
},
}
}