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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247#lang racket/base
(require (for-syntax racket/base
syntax/parse/pre)
racket/contract/base
racket/match
racket/string
web-server/http
(only-in xml xexpr/c)
"contract.rkt")
(provide
(contract-out
[widget-namespace (-> string? widget-renderer/c widget-renderer/c)]
[widget-errors (->* [] [#:class string?] widget/c)]
[widget-input (widget-> [] [#:type string? #:omit-value? boolean?])]
[widget-checkbox (widget->)]
[widget-email (widget->)]
[widget-file (widget->)]
[widget-hidden (widget->)]
[widget-list (-> (-> render-element/c xexpr/c) widget/c)]
[widget-number (widget->)]
[widget-password (widget->)]
[widget-radio-group (widget-> [radio-options/c])]
[widget-select (widget-> [(or/c (hash/c string? radio-options/c) select-options/c)])]
[widget-text (widget->)]
[widget-textarea (widget-> [] [#:omit-value? boolean?])]))
(define-syntax (widget-> stx)
(syntax-parse stx
[(_) #'(widget-> [] [])]
[(_ [required-arg-ctc ...]) #'(widget-> [required-arg-ctc ...] [])]
[(_ [required-arg-ctc ...] [optional-arg-ctc ...])
#'(->* [required-arg-ctc ...]
[optional-arg-ctc ... #:attributes attributes/c]
widget/c)]))
(define render-element/c
(->* [widget/c]
[(or/c #f exact-nonnegative-integer?)]
(or/c xexpr/c (listof xexpr/c))))
(define (lookup-errors errors full-name)
(let loop ([path (map string->symbol (string-split full-name "."))]
[errors errors])
(define current (car path))
(define remaining (cdr path))
(define maybe-error
(and (pair? errors)
(assq current errors)))
(cond
[maybe-error
(if (null? remaining)
(cdr maybe-error)
(loop remaining (cdr maybe-error)))]
[else #f])))
(define ((widget-namespace namespace widget-renderer) name widget)
(widget-renderer (string-append namespace "." name) widget))
(define ((widget-errors #:class [class "errors"]) name _value errors)
(define (render . error-messages)
`((ul ([class ,class])
,@(for/list ([message (in-list error-messages)])
`(li ,message)))))
(match (lookup-errors errors name)
[(? string? error-message)
(render error-message)]
[(? list? error-pairs)
(apply render (for/list ([pair (in-list error-pairs)])
(string-append (car pair) ": " (cdr pair))))]
[_ null]))
(define (xexpr/optional attribute value)
(cond
[value (list (list attribute value))]
[else null]))
(define ((widget-input #:type [type "text"]
#:omit-value? [omit-value? #f]
#:attributes [attributes null]) name binding _errors)
(define value (and (not omit-value?) binding (bytes->string/utf-8 (binding:form-value binding))))
`(input
([type ,type]
[name ,name]
,@attributes
,@(xexpr/optional 'value value))))
(define ((widget-checkbox #:attributes [attributes null]) name binding _errors)
(define value (and binding (bytes->string/utf-8 (binding:form-value binding))))
(define checked (and value "checked"))
`(input
([type "checkbox"]
[name ,name]
,@attributes
,@(xexpr/optional 'value value)
,@(xexpr/optional 'checked checked))))
(define (widget-email #:attributes [attributes null])
(widget-input #:type "email"
#:attributes attributes))
(define (widget-file #:attributes [attributes null])
(widget-input #:type "file"
#:omit-value? #t
#:attributes attributes))
(define (widget-hidden #:attributes [attributes null])
(widget-input #:type "hidden"
#:attributes attributes))
(define (widget-number #:attributes [attributes null])
(widget-input #:type "number"
#:attributes attributes))
(define (widget-password #:attributes [attributes null])
(widget-input #:type "password"
#:omit-value? #t
#:attributes attributes))
(define ((widget-radio-group options #:attributes [attributes null]) name binding _errors)
(define value (and binding (bytes->string/utf-8 (binding:form-value binding))))
(define (make-radio option)
(define radio-value (car option))
(define radio-label (cdr option))
(define checked (and value (string=? value radio-value) "checked"))
`(label
(input
((type "radio")
(name ,name)
,@attributes
,@(xexpr/optional 'value radio-value)
,@(xexpr/optional 'checked checked)))
,radio-label))
`(div ,@(map make-radio options)))
(define ((widget-select options #:attributes [attributes null]) name binding _errors)
(define value (and binding (bytes->string/utf-8 (binding:form-value binding))))
(define (make-option opt-value opt-label)
(define selected?
(and value (string=? value opt-value) "selected"))
`(option
([value ,opt-value]
,@(xexpr/optional 'selected selected?))
,opt-label))
(define (make-option-group group-label group-opts)
`(optgroup
([label ,group-label])
,@(for*/list ([group-option (in-list group-opts)]
[opt-value (in-value (car group-option))]
[opt-label (in-value (cdr group-option))])
(make-option opt-value opt-label))))
(define options-elements
(cond
;; Supported for backwards-compatibility, but discouraged.
[(hash? options)
(for/list ([(group-label group-opts) options])
(make-option-group group-label group-opts))]
[else
(for/list ([opt (in-list options)])
(match opt
[(cons opt-value (? string? opt-label))
(make-option opt-value opt-label)]
[(list (? string? group-label) group-opts)
(make-option-group group-label group-opts)]))]))
`(select
([name ,name]
,@attributes)
,@options-elements))
(define (widget-text #:attributes [attributes null])
(widget-input #:attributes attributes))
(define ((widget-textarea #:omit-value? [omit-value? #f]
#:attributes [attributes null]) name binding _errors)
`(textarea
([name ,name]
,@attributes)
,@(cond
[omit-value? null]
[(not binding) null]
[else (list (bytes->string/utf-8 (binding:form-value binding)))])))
(define ((widget-list proc) name value errors)
(define sym (string->symbol name))
(define seq 0)
(define (next-id)
(begin0 seq
(set! seq (add1 seq))))
(define the-values
(and value
(if (list? value)
(list->vector value)
(vector value))))
(define the-errors
(let ([errors (assq sym errors)])
(define the-error-list (and errors (cdr errors)))
(when (and the-error-list (not (pair? the-error-list)))
(error 'widget-list
(string-join
'("invalid error list"
" in field: ~a"
" expected: a list of errors"
" got: ~e"
" hint: maybe use binding/list instead of some other binding")
"\n")
name the-error-list))
(and errors (list->vector the-error-list))))
(define (render-element widget [idx (next-id)])
(define element-errors
(cond
[(and the-errors (idx . < . (vector-length the-errors)))
(define the-error (vector-ref the-errors idx))
(if (equal? the-error "") null `((,sym . ,the-error)))]
[else null]))
(widget
#;name name
#;value (cond
[idx
(and the-values
(idx . < . (vector-length the-values))
(vector-ref the-values idx))]
[else
;; A #f index means that the widget would like to see all the
;; values that were sent. This is helpful when implementing lists
;; of checkboxes because we care about the set of values sent in
;; the form, but want to ignore their order.
(if the-values (vector->list the-values) null)])
#;errors element-errors))
(proc render-element))