7a376c32 by astaxie

delete forms

1 parent 3d74a1a4
1 package elements
2
3 type Button struct {
4 Element
5 }
6
7 func NewButton() *Button {
8 b := &Button{}
9 b.options = make(map[string]interface{})
10 b.attributes = make(map[string]interface{})
11 b.labelAttributes = make(map[string]interface{})
12 b.labelOptions = make(map[string]interface{})
13 b.SetAttribute("type", "button")
14 return b
15 }
1 package elements
...\ No newline at end of file ...\ No newline at end of file
1 package elements
...\ No newline at end of file ...\ No newline at end of file
1 package elements
...\ No newline at end of file ...\ No newline at end of file
1 package elements
...\ No newline at end of file ...\ No newline at end of file
1 package elements
2
3 type LableInterface interface {
4 SetLable(label string)
5 GetLable() (label string)
6 SetLabelAttributes(labelattrs map[string]interface{})
7 GetLabelAttributes() (labelattrs map[string]interface{})
8 SetLabelOptions(labelOptions map[string]interface{})
9 GetLabelOptions() (labelOptions map[string]interface{})
10 ClearLabelOptions()
11 RemoveLabelOptions(keys []string)
12 SetLabelOption(key string, val interface{})
13 GetLabelOption(key string) (val interface{})
14 RemoveLabelOption(key string)
15 HasLabelOption(key string) bool
16 }
17
18 type ElementInterface interface {
19 SetName(name string)
20 GetName() (name string)
21 SetOptions(options map[string]interface{})
22 SetOption(key string, val interface{})
23 GetOptions() (options map[string]interface{})
24 GetOption(key string) (val interface{})
25 SetAttribute(key string, val interface{})
26 GetAttribute(key string) (val interface{})
27 RemoveAttribute(key string)
28 HasAttribute(key string) bool
29 SetAttributes(attributes map[string]interface{})
30 GetAttributes() (attributes map[string]interface{})
31 RemoveAttributes(keys []string)
32 ClearAttributes()
33 SetValue(val interface{})
34 GetValue() (val interface{})
35 SetMessages(msg string)
36 GetMessages() (msg string)
37 SetValidator(v ValidatorInterface)
38 Valid(val interface{}) bool
39 GetMessage() string
40 LableInterface
41 }
42
43 type ValidatorInterface interface {
44 IsValid(val interface{}) bool
45 GetMessages() (msg string)
46 }
47
48 type Element struct {
49 options map[string]interface{}
50 attributes map[string]interface{}
51 labelAttributes map[string]interface{}
52 labelOptions map[string]interface{}
53 value interface{}
54 lable string
55 messages string
56 validator ValidatorInterface
57 }
58
59 func (e *Element) SetName(name string) {
60 e.SetAttribute("name", name)
61 }
62
63 func (e *Element) GetName() (name string) {
64 return e.GetAttribute("name").(string)
65 }
66
67 func (e *Element) SetOptions(options map[string]interface{}) {
68 if val, ok := options["label"]; ok {
69 e.SetLable(val.(string))
70 }
71
72 if val, ok := options["label_attributes"]; ok {
73 e.SetLabelAttributes(val.(map[string]interface{}))
74 }
75
76 if val, ok := options["label_options"]; ok {
77 e.SetLabelOptions(val.(map[string]interface{}))
78 }
79 e.options = options
80 }
81
82 func (e *Element) SetOption(key string, val interface{}) {
83 e.options[key] = val
84 }
85
86 func (e *Element) GetOptions() (options map[string]interface{}) {
87 return e.options
88 }
89
90 func (e *Element) GetOption(key string) (val interface{}) {
91 if val, ok := e.options[key]; ok {
92 return val
93 }
94 return nil
95 }
96
97 func (e *Element) SetAttribute(key string, val interface{}) {
98 e.attributes[key] = val
99 }
100
101 func (e *Element) GetAttribute(key string) (val interface{}) {
102 if val, ok := e.attributes[key]; ok {
103 return val
104 }
105 return nil
106 }
107
108 func (e *Element) RemoveAttribute(key string) {
109 delete(e.attributes, key)
110 }
111
112 func (e *Element) HasAttribute(key string) bool {
113 if _, ok := e.attributes[key]; ok {
114 return true
115 }
116 return false
117 }
118
119 func (e *Element) SetAttributes(attributes map[string]interface{}) {
120 for key, val := range attributes {
121 e.SetAttribute(key, val)
122 }
123 }
124
125 func (e *Element) GetAttributes() (attributes map[string]interface{}) {
126 return e.attributes
127 }
128
129 func (e *Element) RemoveAttributes(keys []string) {
130 for _, key := range keys {
131 e.RemoveAttribute(key)
132 }
133 }
134
135 func (e *Element) ClearAttributes() {
136 e.attributes = make(map[string]interface{})
137 }
138
139 func (e *Element) SetValue(val interface{}) {
140 e.value = val
141 }
142
143 func (e *Element) GetValue() (val interface{}) {
144 return e.value
145 }
146
147 func (e *Element) SetLable(label string) {
148 e.lable = label
149 }
150
151 func (e *Element) GetLable() (label string) {
152 return e.lable
153 }
154
155 func (e *Element) SetLabelAttributes(labelattrs map[string]interface{}) {
156 e.labelAttributes = labelattrs
157 }
158
159 func (e *Element) GetLabelAttributes() (labelattrs map[string]interface{}) {
160 return e.labelAttributes
161 }
162
163 func (e *Element) SetLabelOptions(labelOptions map[string]interface{}) {
164 for key, val := range labelOptions {
165 e.SetLabelOption(key, val)
166 }
167 }
168
169 func (e *Element) GetLabelOptions() (labelOptions map[string]interface{}) {
170 return e.labelOptions
171 }
172
173 func (e *Element) ClearLabelOptions() {
174 e.labelOptions = make(map[string]interface{})
175 }
176
177 func (e *Element) RemoveLabelOptions(keys []string) {
178 for _, key := range keys {
179 e.RemoveLabelOption(key)
180 }
181 }
182
183 func (e *Element) SetLabelOption(key string, val interface{}) {
184 e.labelOptions[key] = val
185 }
186
187 func (e *Element) GetLabelOption(key string) (val interface{}) {
188 if v, ok := e.labelOptions[key]; ok {
189 return v
190 }
191 return nil
192 }
193
194 func (e *Element) RemoveLabelOption(key string) {
195 delete(e.labelOptions, key)
196 }
197
198 func (e *Element) HasLabelOption(key string) bool {
199 if _, ok := e.labelOptions[key]; ok {
200 return true
201 }
202 return false
203 }
204
205 func (e *Element) SetMessages(msg string) {
206 e.messages = msg
207 }
208
209 func (e *Element) GetMessages() (msg string) {
210 return e.messages
211 }
212
213 func (e *Element) SetValidator(v ValidatorInterface) {
214 e.validator = v
215 }
216
217 func (e *Element) Valid(val interface{}) bool {
218 if e.validator == nil {
219 return true
220 }
221 if e.validator.IsValid(val) {
222 return true
223 }
224 return false
225 }
226
227 func (e *Element) GetMessage() string {
228 if e.validator == nil {
229 return ""
230 }
231 return e.validator.GetMessages()
232 }
1 package elements
...\ No newline at end of file ...\ No newline at end of file
1 package elements
...\ No newline at end of file ...\ No newline at end of file
1 package elements
...\ No newline at end of file ...\ No newline at end of file
1 package elements
...\ No newline at end of file ...\ No newline at end of file
1 package elements
...\ No newline at end of file ...\ No newline at end of file
1 package elements
...\ No newline at end of file ...\ No newline at end of file
1 package elements
...\ No newline at end of file ...\ No newline at end of file
1 package elements
...\ No newline at end of file ...\ No newline at end of file
1 package elements
...\ No newline at end of file ...\ No newline at end of file
1 package elements
...\ No newline at end of file ...\ No newline at end of file
1 package elements
2
3 type Submit struct {
4 Element
5 }
6
7 func NewSubmit() *Submit {
8 b := &Submit{}
9 b.options = make(map[string]interface{})
10 b.attributes = make(map[string]interface{})
11 b.labelAttributes = make(map[string]interface{})
12 b.labelOptions = make(map[string]interface{})
13 b.SetAttribute("type", "submit")
14 return b
15 }
1 package elements
2
3 type Text struct {
4 Element
5 }
6
7 func NewText() *Text {
8 b := &Text{}
9 b.options = make(map[string]interface{})
10 b.attributes = make(map[string]interface{})
11 b.labelAttributes = make(map[string]interface{})
12 b.labelOptions = make(map[string]interface{})
13 b.SetAttribute("type", "text")
14 return b
15 }
1 package elements
2
3 type Textarea struct {
4 Element
5 }
6
7 func NewTextarea() *Textarea {
8 b := &Textarea{}
9 b.options = make(map[string]interface{})
10 b.attributes = make(map[string]interface{})
11 b.labelAttributes = make(map[string]interface{})
12 b.labelOptions = make(map[string]interface{})
13 b.SetAttribute("type", "textarea")
14 return b
15 }
1 package elements
...\ No newline at end of file ...\ No newline at end of file
1 package elements
...\ No newline at end of file ...\ No newline at end of file
1 // Beego (http://beego.me/)
2 // @description beego is an open-source, high-performance web framework for the Go programming language.
3 // @link http://github.com/astaxie/beego for the canonical source repository
4 // @license http://github.com/astaxie/beego/blob/master/LICENSE
5 // @authors astaxie
6
7 package forms
8
9 import (
10 "github.com/astaxie/beego/forms/elements"
11 )
12
13 type Form struct {
14 elements map[string]elements.ElementInterface
15 }
16
17 func NewForm() *Form {
18 f := &Form{}
19 f.elements = make(map[string]elements.ElementInterface)
20 return f
21 }
22
23 func (f *Form) Init() *Form {
24 f.elements = make(map[string]elements.ElementInterface)
25 }
26
27 func (f *Form) Valid(data map[string]interface{}) bool {
28 for k, val := range data {
29 if e, ok := f.elements[k]; ok {
30 if !e.Valid(val) {
31 return false
32 }
33 }
34 }
35 return true
36 }
37
38 func (f *Form) SetData() {
39
40 }
41
42 func (f *Form) Bind() {
43
44 }
45
46 func (f *Form) SaveData() {
47
48 }
49
50 func (f *Form) AddElement(e elements.ElementInterface) {
51 name := e.GetName()
52 f.elements[name] = e
53 }
54
55 func (f *Form) Render() string {
56
57 }
1 package forms
2
3 import (
4 "fmt"
5 "testing"
6
7 "./elements"
8 )
9
10 func formTest(t *testing.T) {
11 rf := NewForm()
12 username := elements.NewText()
13 username.SetAttribute("name", "username")
14 username.SetAttribute("id", "username")
15 rf.AddElement(username)
16
17 button := elements.NewSubmit()
18 rf.AddElement(rf)
19
20 fmt.Println(rf.Render())
21 }
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!