Easy form generation. TODO: parse forms

This commit is contained in:
2019-02-22 00:04:57 +01:00
parent 19067a5616
commit 185cc4dc60
6 changed files with 288 additions and 3 deletions

View File

@@ -0,0 +1,56 @@
package forms
import (
"html/template"
)
// Form is a form which can be rendered in HTML and submitted
type Form struct {
Title string // Shown in a large font above the form
PreFormHTML template.HTML // Content to be rendered above the form
Fields []Field
BackLink string // Empty for no back link
SubmitLabel string // Label for the submit button
SubmitRed bool // If the submit button should be red or green
PostFormHTML template.HTML // Content to be rendered below the form
// Fields to render if the form has been submitted once
Submit bool // If the form has been submitted
SubmitSuccess bool // If the submission was a success
SubmitMessages []string // Messages telling the user the results
}
// Field is a single input field in a form
type Field struct {
// Used for reading the data. Entered data is POSTed back to the same URL with this name
Name string
// Is entered in the input field by default
DefaultValue string
// Text next to the input field
Label string
// Text below the input field
Description string
// Separates fields with a horizontal rule
Separator bool
Type FieldType
// Only used when Type = `captcha`
CaptchaSiteKey string
}
// FieldType defines the type a form field has and how it should be rendered
type FieldType string
// Fields which can be in a form
const (
FieldTypeText FieldType = "text"
FieldTypeUsername FieldType = "username"
FieldTypeEmail FieldType = "email"
FieldTypeOldPassword FieldType = "current-password"
FieldTypeNewPassword FieldType = "new-password"
FieldTypeCaptcha FieldType = "captcha"
)

View File

@@ -7,6 +7,7 @@ import (
"time"
"fornaxian.com/pixeldrain-web/pixelapi"
"fornaxian.com/pixeldrain-web/webcontroller/forms"
"github.com/Fornaxian/log"
)
@@ -19,12 +20,15 @@ type TemplateData struct {
APIEndpoint template.URL
PixelAPI *pixelapi.PixelAPI
Other interface{}
URLQuery url.Values
// Only used on file viewer page
Title string
OGData OGData
Other interface{}
URLQuery url.Values
// Only used for pages containing forms
Form forms.Form
}
func (wc *WebController) newTemplateData(w http.ResponseWriter, r *http.Request) *TemplateData {

View File

@@ -1,9 +1,11 @@
package webcontroller
import (
"html/template"
"net/http"
"fornaxian.com/pixeldrain-web/pixelapi"
"fornaxian.com/pixeldrain-web/webcontroller/forms"
"github.com/Fornaxian/log"
"github.com/julienschmidt/httprouter"
)
@@ -54,3 +56,38 @@ func (wc *WebController) serveLogout(
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func (wc *WebController) formPassword(
w http.ResponseWriter,
r *http.Request,
p httprouter.Params,
) {
td := wc.newTemplateData(w, r)
td.Form = forms.Form{
Title: "Test Form",
PreFormHTML: template.HTML("preform"),
Fields: []forms.Field{
forms.Field{
Name: "field_1",
DefaultValue: "def val 1",
Label: "Field 1",
Description: "Description of field one",
Separator: false,
Type: forms.FieldTypeUsername,
},
},
BackLink: "/",
SubmitLabel: "ayy lmao send",
SubmitRed: false,
PostFormHTML: template.HTML("postform"),
Submit: true,
SubmitSuccess: true,
SubmitMessages: []string{"yay success"},
}
err := wc.templates.Get().ExecuteTemplate(w, "form_page", td)
if err != nil {
log.Error("Error executing template '%s': %s", "register", err)
}
}

View File

@@ -67,6 +67,9 @@ func New(r *httprouter.Router, prefix string, conf *conf.PixelWebConfig) *WebCon
r.GET(p+"/user/files" /* */, wc.serveTemplate("user_files", true))
r.GET(p+"/user/lists" /* */, wc.serveTemplate("user_lists", true))
r.GET(p+"/user/filemanager" /**/, wc.serveTemplate("file_manager", true))
r.GET(p+"/user/password" /* */, wc.serveTemplate("user_password", true))
r.GET(p+"/testform", wc.formPassword)
r.NotFound = http.HandlerFunc(wc.serveNotFound)