mirror of
https://github.com/astaxie/beego.git
synced 2024-11-01 03:30:54 +00:00
Change meta to required, and refactor a bit to cover edge cases
This commit is contained in:
parent
84b6bef7d0
commit
e0393b721c
@ -475,13 +475,10 @@ func parseFormTag(fieldT reflect.StructField) (label, name, fType string, id str
|
|||||||
id = fieldT.Tag.Get("id")
|
id = fieldT.Tag.Get("id")
|
||||||
class = fieldT.Tag.Get("class")
|
class = fieldT.Tag.Get("class")
|
||||||
|
|
||||||
meta := strings.Split(fieldT.Tag.Get("meta"), ",")
|
|
||||||
required = false
|
required = false
|
||||||
switch len(meta) {
|
required_field := fieldT.Tag.Get("required")
|
||||||
case 1:
|
if required_field != "-" && required_field != "" {
|
||||||
if len(meta[0]) > 0 && meta[0] != "-" {
|
required, _ = strconv.ParseBool(required_field)
|
||||||
required = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch len(tags) {
|
switch len(tags) {
|
||||||
|
@ -219,7 +219,9 @@ func TestParseFormTag(t *testing.T) {
|
|||||||
OnlyLabel int `form:",,年龄:"`
|
OnlyLabel int `form:",,年龄:"`
|
||||||
OnlyName int `form:"name" id:"name" class:"form-name"`
|
OnlyName int `form:"name" id:"name" class:"form-name"`
|
||||||
Ignored int `form:"-"`
|
Ignored int `form:"-"`
|
||||||
Required int `form:"name" meta:"true"`
|
Required int `form:"name" required:"true"`
|
||||||
|
IgnoreRequired int `form:"name"`
|
||||||
|
NotRequired int `form:"name" required:"false"`
|
||||||
}
|
}
|
||||||
|
|
||||||
objT := reflect.TypeOf(&user{}).Elem()
|
objT := reflect.TypeOf(&user{}).Elem()
|
||||||
@ -255,6 +257,16 @@ func TestParseFormTag(t *testing.T) {
|
|||||||
t.Errorf("Form Tag containing only name and required was not correctly parsed.")
|
t.Errorf("Form Tag containing only name and required was not correctly parsed.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
label, name, fType, id, class, ignored, required = parseFormTag(objT.Field(6))
|
||||||
|
if !(name == "name" && required == false) {
|
||||||
|
t.Errorf("Form Tag containing only name and ignore required was not correctly parsed.")
|
||||||
|
}
|
||||||
|
|
||||||
|
label, name, fType, id, class, ignored, required = parseFormTag(objT.Field(7))
|
||||||
|
if !(name == "name" && required == false) {
|
||||||
|
t.Errorf("Form Tag containing only name and not required was not correctly parsed.")
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMapGet(t *testing.T) {
|
func TestMapGet(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user