Added support for select with options tag for templatefun.RenderForm (#5691)

* Added support for `select` with `options` tag for templatefun.RenderForm

* removing unwanted spaces

* added test for select in RenderForm
This commit is contained in:
Fahad 2024-08-17 03:45:24 -04:00 committed by GitHub
parent 665cf3504f
commit 8ee564a34c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 4 deletions

View File

@ -26,6 +26,7 @@ import (
"strings"
"time"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/server/web/context"
)
@ -310,7 +311,8 @@ func RenderForm(obj interface{}) template.HTML {
return template.HTML(strings.Join(raw, "</br>"))
}
// renderFormField returns a string containing HTML of a single form field.
// renderFormField returns a string containing HTML of a single form field. In case of select fType, it will retrun
// select tag with options. Value for select fType must be comma separated string which are use are
func renderFormField(label, name, fType string, value interface{}, id string, class string, required bool) string {
if id != "" {
id = " id=\"" + id + "\""
@ -329,6 +331,25 @@ func renderFormField(label, name, fType string, value interface{}, id string, cl
return fmt.Sprintf(`%v<input%v%v name="%v" type="%v" value="%v"%v>`, label, id, class, name, fType, value, requiredString)
}
if fType == "select" {
valueStr, ok := value.(string)
if !ok {
logs.Error("for select value must comma separated string that are the options for select")
return ""
}
var selectBuilder strings.Builder
selectBuilder.WriteString(fmt.Sprintf(`%v<select%v%v name="%v"></br>`, label, id, class, name))
for _, option := range strings.Split(valueStr, ",") {
selectBuilder.WriteString(fmt.Sprintf(` <option value="%v"> %v </option></br>`, option, option))
}
selectBuilder.WriteString(`</select>`)
return selectBuilder.String()
}
return fmt.Sprintf(`%v<%v%v%v name="%v"%v>%v</%v>`, label, fType, id, class, name, requiredString, value, fType)
}

View File

@ -205,13 +205,13 @@ func TestRenderForm(t *testing.T) {
ID int `form:"-"`
Name interface{} `form:"username"`
Age int `form:"age,text,年龄:"`
Sex string
Sex string `form:"sex,select"`
Email []string
Intro string `form:",textarea"`
Ignored string `form:"-"`
}
u := user{Name: "test", Intro: "Some Text"}
u := user{Name: "test", Intro: "Some Text", Sex: "Male,Female"}
output := RenderForm(u)
if output != template.HTML("") {
t.Errorf("output should be empty but got %v", output)
@ -220,7 +220,10 @@ func TestRenderForm(t *testing.T) {
result := template.HTML(
`Name: <input name="username" type="text" value="test"></br>` +
`年龄:<input name="age" type="text" value="0"></br>` +
`Sex: <input name="Sex" type="text" value=""></br>` +
`Sex: <select name="sex"></br>` +
` <option value="Male"> Male </option></br>` +
` <option value="Female"> Female </option></br>` +
`</select></br>` +
`Intro: <textarea name="Intro">Some Text</textarea>`)
if output != result {
t.Errorf("output should equal `%v` but got `%v`", result, output)