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
1b3e7de4
Commit
1b3e7de4
authored
Oct 20, 2014
by
WithGJR
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add new feature to 'renderform' function, user could add HTML id and class now
parent
efc14a1e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
6 deletions
+8
-6
templatefunc.go
templatefunc.go
+8
-6
No files found.
templatefunc.go
View file @
1b3e7de4
...
...
@@ -368,23 +368,23 @@ func RenderForm(obj interface{}) template.HTML {
fieldT
:=
objT
.
Field
(
i
)
label
,
name
,
fType
,
ignored
:=
parseFormTag
(
fieldT
)
label
,
name
,
fType
,
i
d
,
class
,
i
gnored
:=
parseFormTag
(
fieldT
)
if
ignored
{
continue
}
raw
=
append
(
raw
,
renderFormField
(
label
,
name
,
fType
,
fieldV
.
Interface
()))
raw
=
append
(
raw
,
renderFormField
(
label
,
name
,
fType
,
fieldV
.
Interface
()
,
id
,
class
))
}
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
{})
string
{
func
renderFormField
(
label
,
name
,
fType
string
,
value
interface
{}
,
id
string
,
class
string
)
string
{
if
isValidForInput
(
fType
)
{
return
fmt
.
Sprintf
(
`%v<input
name="%v" type="%v" value="%v">`
,
label
,
name
,
fType
,
value
)
return
fmt
.
Sprintf
(
`%v<input
id="%v" class="%v" name="%v" type="%v" value="%v">`
,
label
,
id
,
class
,
name
,
fType
,
value
)
}
return
fmt
.
Sprintf
(
`%v<%v
name="%v">%v</%v>`
,
label
,
fType
,
name
,
value
,
fType
)
return
fmt
.
Sprintf
(
`%v<%v
id="%v" class="%v" name="%v">%v</%v>`
,
label
,
fType
,
id
,
class
,
name
,
value
,
fType
)
}
// isValidForInput checks if fType is a valid value for the `type` property of an HTML input element.
...
...
@@ -400,12 +400,14 @@ func isValidForInput(fType string) bool {
// parseFormTag takes the stuct-tag of a StructField and parses the `form` value.
// returned are the form label, name-property, type and wether the field should be ignored.
func
parseFormTag
(
fieldT
reflect
.
StructField
)
(
label
,
name
,
fType
string
,
ignored
bool
)
{
func
parseFormTag
(
fieldT
reflect
.
StructField
)
(
label
,
name
,
fType
string
,
i
d
string
,
class
string
,
i
gnored
bool
)
{
tags
:=
strings
.
Split
(
fieldT
.
Tag
.
Get
(
"form"
),
","
)
label
=
fieldT
.
Name
+
": "
name
=
fieldT
.
Name
fType
=
"text"
ignored
=
false
id
=
fieldT
.
Tag
.
Get
(
"id"
)
class
=
fieldT
.
Tag
.
Get
(
"class"
)
switch
len
(
tags
)
{
case
1
:
...
...
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