Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
0d3a806c
Commit
0d3a806c
authored
Jun 16, 2016
by
Joshua Santos
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add meta fields with required flag
parent
88c5dfa6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
3 deletions
+24
-3
templatefunc.go
templatefunc.go
+24
-3
No files found.
templatefunc.go
View file @
0d3a806c
...
...
@@ -422,17 +422,18 @@ func RenderForm(obj interface{}) template.HTML {
fieldT
:=
objT
.
Field
(
i
)
label
,
name
,
fType
,
id
,
class
,
ignored
:=
parseFormTag
(
fieldT
)
required
:=
parseMetaTag
(
fieldT
)
if
ignored
{
continue
}
raw
=
append
(
raw
,
renderFormField
(
label
,
name
,
fType
,
fieldV
.
Interface
(),
id
,
class
))
raw
=
append
(
raw
,
renderFormField
(
label
,
name
,
fType
,
fieldV
.
Interface
(),
id
,
class
,
required
))
}
return
template
.
HTML
(
strings
.
Join
(
raw
,
"</br>"
))
}
// renderFormField returns a string containing HTML of a single form field.
func
renderFormField
(
label
,
name
,
fType
string
,
value
interface
{},
id
string
,
class
string
)
string
{
func
renderFormField
(
label
,
name
,
fType
string
,
value
interface
{},
id
string
,
class
string
,
required
bool
)
string
{
if
id
!=
""
{
id
=
" id=
\"
"
+
id
+
"
\"
"
}
...
...
@@ -441,8 +442,13 @@ func renderFormField(label, name, fType string, value interface{}, id string, cl
class
=
" class=
\"
"
+
class
+
"
\"
"
}
requiredString
:=
""
if
required
{
requiredString
=
" required"
}
if
isValidForInput
(
fType
)
{
return
fmt
.
Sprintf
(
`%v<input%v%v name="%v" type="%v" value="%v"
>`
,
label
,
id
,
class
,
name
,
fType
,
value
)
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<%v%v%v name="%v">%v</%v>`
,
label
,
fType
,
id
,
class
,
name
,
value
,
fType
)
...
...
@@ -496,6 +502,21 @@ func parseFormTag(fieldT reflect.StructField) (label, name, fType string, id str
label
=
tags
[
2
]
}
}
return
}
// parseMetaTag takes the stuct-tag of a StructField and parses the `meta` value.
// returned is the boolean of whether the field is required
func
parseMetaTag
(
fieldT
reflect
.
StructField
)
(
required
bool
)
{
meta
:=
strings
.
Split
(
fieldT
.
Tag
.
Get
(
"meta"
),
","
)
required
=
false
switch
len
(
meta
)
{
case
1
:
if
len
(
meta
[
0
])
>
0
&&
meta
[
0
]
!=
"-"
{
required
=
true
}
}
return
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment