Skip to content

Instantly share code, notes, and snippets.

@joncalhoun joncalhoun/signup_form.go Secret

Last active Jun 16, 2020
Embed
What would you like to do?
type UserForm struct {
Name string `schema:"name"`
Email string `schema:"email"`
Password string `schema:"password"`
Errors map[string]error
}
func (uf UserForm) Form() Form {
return Form{
Path: "/signup",
Method: "POST",
Fields: []FormField{
FormField{
AutoFocus: true,
Name: "name",
Icon: "id-card-o",
PlaceHolder: "Your full name please",
Value: uf.Name,
Error: uf.Errors["name"],
}.Normalized(),
FormField{
Name: "email",
Label: "Email address",
Type: "email",
Icon: "envelope",
Value: uf.Email,
Error: uf.Errors["email"],
}.Normalized(),
FormField{
Name: "password",
Type: "password",
Icon: "lock",
PlaceHolder: "Something super secret",
Error: uf.Errors["password"],
}.Normalized(),
},
Buttons: []FormButton{SubmitButton("Sign Up")},
Footer: Footer{
Right: template.HTML(`<a href="/login" class="text-muted">Already have an account?</a>`),
},
}
}
type SignupForm struct {
UserForm
License string `schema:"license"`
Errors map[string]error
}
func (sf SignupForm) Form() Form {
sf.UserForm.Errors = sf.Errors
form := sf.UserForm.Form()
form.Path = "/signup"
form.Buttons = []FormButton{SubmitButton("Sign Up")}
form.Footer = Footer{
Right: template.HTML(`<a href="/login" class="text-muted">Already have an account?</a>`),
}
form.Fields = append(form.Fields, FormField{
Name: "license",
Label: "License key",
Icon: "key",
HelperText: "You can find this in an email sent over by Gumroad after purchasing the course.",
Value: sf.License,
Error: sf.Errors["license"],
}.Normalized())
return form
}
type Form struct {
Path string
Method string
Fields []FormField
Buttons []FormButton
Footer Footer
}
type FormField struct {
Name string
ID string
Type string
Label string
PlaceHolder string
HelperText string
AutoFocus bool
Icon string
Value interface{}
Error error
}
func (f FormField) Normalized() FormField {
if f.ID == "" {
f.ID = f.Name
}
if f.Type == "" {
f.Type = "text"
}
if f.Label == "" {
f.Label = strings.Title(f.Name)
}
if f.PlaceHolder == "" {
f.PlaceHolder = f.Label
}
return f
}
func SubmitButton(text string) FormButton {
return FormButton{
Type: "submit",
Class: "primary",
Text: text,
}
}
type FormButton struct {
Type string
Class string
Text string
}
type Footer struct {
Left template.HTML
Right template.HTML
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.