-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform_control_handler.go
61 lines (45 loc) · 1.45 KB
/
form_control_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package dhtmlform
import (
"fmt"
"log"
"strings"
"github.com/mitoteam/dhtml"
)
type FormControlHandler struct {
// [required] renders control
RenderF func(control *FormControlElement) dhtml.HtmlPiece
// [optional] preprocesses raw data from POST
ProcessPostValueF func(controlData *FormControlData)
// [optional] check preprocessed value, return (true, nil) or (false, <error output>)
//TODO: ValidateValueF func(value any) (ok bool, errorOut *dhtml.HtmlPiece)
}
var formControlHandlers map[string]*FormControlHandler
func RegisterFormControlHandler(controlKind string, handler *FormControlHandler) {
controlKind = strings.TrimSpace(controlKind)
if controlKind == "" {
panic("controlKind should not be empty")
}
if handler == nil {
panic("handler should not be nil")
}
if handler.RenderF == nil {
panic("handler.RenderF is not set")
}
if formControlHandlers == nil {
formControlHandlers = make(map[string]*FormControlHandler)
}
if _, ok := formControlHandlers[controlKind]; ok {
panic(fmt.Sprintf("handler for '%s' already registered", controlKind))
}
formControlHandlers[controlKind] = handler
}
func GetFormControlHandler(controlKind string) (*FormControlHandler, bool) {
if formControlHandlers == nil {
formControlHandlers = make(map[string]*FormControlHandler)
}
if handler, ok := formControlHandlers[controlKind]; ok {
return handler, true
}
log.Fatalf("Unknown form control kind: %s\n", controlKind)
return nil, false
}