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:
parent
665cf3504f
commit
8ee564a34c
@ -26,6 +26,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/beego/beego/v2/core/logs"
|
||||||
"github.com/beego/beego/v2/server/web/context"
|
"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>"))
|
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 {
|
func renderFormField(label, name, fType string, value interface{}, id string, class string, required bool) string {
|
||||||
if id != "" {
|
if id != "" {
|
||||||
id = " id=\"" + 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)
|
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)
|
return fmt.Sprintf(`%v<%v%v%v name="%v"%v>%v</%v>`, label, fType, id, class, name, requiredString, value, fType)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -205,13 +205,13 @@ func TestRenderForm(t *testing.T) {
|
|||||||
ID int `form:"-"`
|
ID int `form:"-"`
|
||||||
Name interface{} `form:"username"`
|
Name interface{} `form:"username"`
|
||||||
Age int `form:"age,text,年龄:"`
|
Age int `form:"age,text,年龄:"`
|
||||||
Sex string
|
Sex string `form:"sex,select"`
|
||||||
Email []string
|
Email []string
|
||||||
Intro string `form:",textarea"`
|
Intro string `form:",textarea"`
|
||||||
Ignored string `form:"-"`
|
Ignored string `form:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
u := user{Name: "test", Intro: "Some Text"}
|
u := user{Name: "test", Intro: "Some Text", Sex: "Male,Female"}
|
||||||
output := RenderForm(u)
|
output := RenderForm(u)
|
||||||
if output != template.HTML("") {
|
if output != template.HTML("") {
|
||||||
t.Errorf("output should be empty but got %v", output)
|
t.Errorf("output should be empty but got %v", output)
|
||||||
@ -220,7 +220,10 @@ func TestRenderForm(t *testing.T) {
|
|||||||
result := template.HTML(
|
result := template.HTML(
|
||||||
`Name: <input name="username" type="text" value="test"></br>` +
|
`Name: <input name="username" type="text" value="test"></br>` +
|
||||||
`年龄:<input name="age" type="text" value="0"></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>`)
|
`Intro: <textarea name="Intro">Some Text</textarea>`)
|
||||||
if output != result {
|
if output != result {
|
||||||
t.Errorf("output should equal `%v` but got `%v`", result, output)
|
t.Errorf("output should equal `%v` but got `%v`", result, output)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user