-
Notifications
You must be signed in to change notification settings - Fork 16
/
chap-forms.lyx
2546 lines (1790 loc) · 43 KB
/
chap-forms.lyx
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#LyX 1.6.5 created this file. For more info see http://www.lyx.org/
\lyxformat 345
\begin_document
\begin_header
\textclass book
\use_default_options true
\language english
\inputencoding auto
\font_roman default
\font_sans default
\font_typewriter default
\font_default_family default
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100
\graphics default
\paperfontsize default
\spacing single
\use_hyperref false
\papersize default
\use_geometry false
\use_amsmath 1
\use_esint 1
\cite_engine basic
\use_bibtopic false
\paperorientation portrait
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\defskip medskip
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\author ""
\author ""
\end_header
\begin_body
\begin_layout Chapter
Forms in Lift
\begin_inset CommandInset label
LatexCommand label
name "cha:Forms-in-Lift"
\end_inset
\end_layout
\begin_layout Standard
In this chapter we're going to discuss the specifics of how you generate
and process forms with Lift.
Besides standard GET/POST form processing, Lift provides AJAX forms (Chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "cha:AJAX-and-COMET"
\end_inset
) as well as JSON form processing (Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:JSON-forms"
\end_inset
), but we're going to focus on the standard stuff here.
We're going to assume that you have a general knowledge of basic HTML form
tags as well as how CGI
\begin_inset Index
status open
\begin_layout Plain Layout
CGI
\end_layout
\end_inset
form processing works.
\end_layout
\begin_layout Section
Form Fundamentals
\begin_inset CommandInset label
LatexCommand label
name "sec:Form-Fundamentals"
\end_inset
\end_layout
\begin_layout Standard
Let's start with the basics of Lift form processing.
A form in Lift is usually produced via a snippet that contains the additional
\family typewriter
form
\family default
attribute.
As we mentioned in Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:snippet-tag"
\end_inset
, this attribute takes the value GET or POST, and when present makes the
snippet code embed the proper form tags around the snippet HTML.
Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:An-example-form-template"
\end_inset
shows an example of a form that we will be discussing throughout this section.
\begin_inset Note Note
status open
\begin_layout Plain Layout
Make more pokcetchangey
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
An Example Form Template
\begin_inset CommandInset label
LatexCommand label
name "lst:An-example-form-template"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
<lift:Ledger.add form="POST">
\end_layout
\begin_layout Plain Layout
<entry:description /> <entry:amount /><br />
\end_layout
\begin_layout Plain Layout
<entry:submit />
\end_layout
\begin_layout Plain Layout
</lift:Ledger.add>
\end_layout
\end_inset
\end_layout
\begin_layout Standard
The first thing to understand about Lift's form support is that you generally
don't use the HTML tags for form elements directly, but rather you use
generator functions on
\begin_inset Newline linebreak
\end_inset
\family typewriter
net.liftweb.http.SHtml
\family default
.
The main reason for this is that it allows Lift to set up all of the internal
plumbing so that you keep your code simple.
Additionally, we use Lift's binding mechanism (Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:Binding-Values-in-snippets"
\end_inset
) to
\begin_inset Quotes eld
\end_inset
attach
\begin_inset Quotes erd
\end_inset
the form elements in the proper location.
In our example in Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:An-example-form-template"
\end_inset
, we have bindings for a description field, an amount, and a submit button.
\end_layout
\begin_layout Standard
Our next step is to define the form snippet itself.
Corresponding to our example template is Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:An-example-form-snippet"
\end_inset
.
This shows our add method with a few vars to hold the form data and a binding
to the proper form elements.
We'll cover the
\family typewriter
processEntryAdd
\family default
method in a moment; for now let's look at what we have inside the add method.
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
An Example Form Snippet
\begin_inset CommandInset label
LatexCommand label
name "lst:An-example-form-snippet"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
def add (xhtml : NodeSeq) : NodeSeq = {
\end_layout
\begin_layout Plain Layout
var desc = ""
\end_layout
\begin_layout Plain Layout
var amount = "0"
\end_layout
\begin_layout Plain Layout
\end_layout
\begin_layout Plain Layout
def processEntryAdd () { ...
}
\end_layout
\begin_layout Plain Layout
\end_layout
\begin_layout Plain Layout
bind("entry", xhtml,
\end_layout
\begin_layout Plain Layout
"description" -> SHtml.text(desc, desc = _),
\end_layout
\begin_layout Plain Layout
"amount" -> SHtml.text(amount, amount = _),
\end_layout
\begin_layout Plain Layout
"submit" -> SHtml.submit("Add", processEntryAdd))
\end_layout
\begin_layout Plain Layout
}
\end_layout
\end_inset
\end_layout
\begin_layout Standard
First, you may be wondering why we use vars defined inside the method.
Normally, these vars would be locally scoped (stack-based) and would be
discarded as soon as the method returns.
The beauty of Scala and Lift is that the right hand argument of each of
the SHtml functions is actually a function itself.
Because these functions, also known as anonymous closures
\begin_inset Index
status open
\begin_layout Plain Layout
closure
\end_layout
\end_inset
, reference variables in local scope, Scala magically transforms them to
heap variables behind the scenes.
Lift, in turn, adds the function callbacks for each form element into its
session state so that when the form is submitted, the appropriate closure
is called and the state is updated.
This is also why we define the
\family typewriter
processEntryAdd
\family default
function inside of the
\family typewriter
add
\family default
method: by doing so, the
\family typewriter
processEntryAdd
\family default
function
\emph on
also
\emph default
has access to the closure variables.
In our example, we're using Scala's placeholder
\begin_inset Quotes eld
\end_inset
_
\begin_inset Quotes erd
\end_inset
shorthand
\begin_inset Foot
status open
\begin_layout Plain Layout
For more details on placeholders, see the
\emph on
Scala Language Specification
\emph default
, section 6.23
\end_layout
\end_inset
to define our functions.
Your description processing function could also be defined as:
\end_layout
\begin_layout LyX-Code
newDesc => description = newDesc
\end_layout
\begin_layout Standard
One important thing to remember, however, is that each new invocation of
the add method (for each page view) will get its own unique instance of
the variables that we've defined.
That means that if you want to retain values between submission and re-renderin
g of the form, you'll want to use
\family typewriter
RequestVar
\family default
s (Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:Session-and-Request"
\end_inset
) or a
\family typewriter
StatefulSnippet
\family default
(Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:Stateless-versus-Stateful"
\end_inset
) instead .
Generally you will only use vars defined within the snippet method when
your form doesn't require validation and you don't need any of the submitted
data between snippet executions.
An example of using
\family typewriter
RequestVar
\family default
s for your form data would be if you want to do form validation and retain
submitted values if validation fails, as shown in Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Using-RequestVars-with-forms"
\end_inset
.
In this instance, we set an error message (more in Chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "cha:Message-Handling"
\end_inset
).
Since we don't explicitly redirect, the same page is loaded (the default
\begin_inset Quotes eld
\end_inset
action
\begin_inset Quotes erd
\end_inset
for a page in Lift is the page itself) and the current RequestVar value
of description is used as the default value of the text box.
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Using RequestVars with Forms
\begin_inset CommandInset label
LatexCommand label
name "lst:Using-RequestVars-with-forms"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
object description extends RequestVar("")
\end_layout
\begin_layout Plain Layout
object amount extends RequestVar("0")
\end_layout
\begin_layout Plain Layout
\end_layout
\begin_layout Plain Layout
def add (xhtml : NodeSeq) : NodeSeq = {
\end_layout
\begin_layout Plain Layout
def processEntryAdd () =
\end_layout
\begin_layout Plain Layout
if (amount.toDouble <= 0) {
\end_layout
\begin_layout Plain Layout
S.error("Invalid amount")
\end_layout
\begin_layout Plain Layout
} else {
\end_layout
\begin_layout Plain Layout
// ...
process Add ...
\end_layout
\begin_layout Plain Layout
redirectTo(...)
\end_layout
\begin_layout Plain Layout
}
\end_layout
\begin_layout Plain Layout
\end_layout
\begin_layout Plain Layout
bind("entry", xhtml,
\end_layout
\begin_layout Plain Layout
"description" -> SHtml.text(description.is, description(_)),
\end_layout
\begin_layout Plain Layout
...
\end_layout
\begin_layout Plain Layout
}
\end_layout
\end_inset
\end_layout
\begin_layout Standard
The next thing to look at is how the form elements are generated.
We use the SHtml helper object to generate a form element of the appropriate
type for each variable.
In our case, we just want text fields for the description and amount, but
SHtml provides a number of other form element types that we'll be covering
later in this section.
Generally, an element generator takes an argument for the initial value
as well as a function to process the submitted value.
Usually both of these arguments will use a variable, but there's nothing
stopping you from doing something such as
\end_layout
\begin_layout LyX-Code
\begin_inset Quotes eld
\end_inset
description
\begin_inset Quotes erd
\end_inset
-> SHtml.text(
\begin_inset Quotes eld
\end_inset
\begin_inset Quotes erd
\end_inset
, println(
\begin_inset Quotes eld
\end_inset
Description =
\begin_inset Quotes eld
\end_inset
+ _))
\end_layout
\begin_layout Standard
Finally, our submit function executes the partially applied
\family typewriter
processEntryAdd
\family default
function, which, having access to the variables we've defined, can do whatever
it needs to do when the submit button is pressed.
\end_layout
\begin_layout Section
\begin_inset CommandInset label
LatexCommand label
name "sec:Attributes-for-Form-Elems"
\end_inset
Attributes for Form Elements
\begin_inset Index
status open
\begin_layout Plain Layout
Attributes ! in form elements
\end_layout
\end_inset
\end_layout
\begin_layout Standard
In addition to the approaches shown in Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:Handling-XHTML-Attributes"
\end_inset
, the SHtml generator functions allow you to apply attributes by passing
the attribute name/value pairs as final arguments.
This is usually simpler, and in some cases is much simpler than using the
\begin_inset Quotes eld
\end_inset
%
\begin_inset Quotes erd
\end_inset
operator directly.
For example, checkbox and radio form elements are acutally returned as
ChoiceHolder instances, which do not directly support the
\begin_inset Quotes eld
\end_inset
%
\begin_inset Quotes erd
\end_inset
operator.
Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Applying-Attributes-vararg"
\end_inset
shows how to apply the same attributes as Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Applying-Attributes-percent"
\end_inset
using the varargs approach.
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
val myInput = SHtml.text("", processText(_), "id" -> "inputField",
\end_layout
\begin_layout Plain Layout
"class" -> "highlighted")
\end_layout
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
\begin_inset CommandInset label
LatexCommand label
name "lst:Applying-Attributes-vararg"
\end_inset
Applying Attributes as Varargs
\end_layout
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Section
An Overview of Form Elements
\end_layout
\begin_layout Standard
Now that we've covered the basics of forms, we're going to go into a little
more detail for each form element generator method on SHtml.
The
\family typewriter
a
\family default
method (all 3 variants) as well as the
\family typewriter
ajax
\family default
* methods are specific to AJAX forms, which are covered in detail in Chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "cha:AJAX-and-COMET"
\end_inset
.
The
\family typewriter
json*
\family default
methods are covered in Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:JSON-forms"
\end_inset
.
We'll be covering the fileUpload method in detail in Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:File-Uploads"
\end_inset
.
One final note before we dive in is that most generator methods have an
overload with a trailing asterisk (i.e.
\family typewriter
hidden_*
\family default
); these are generally equivalent to the overloads without an asterisk but
are intended for Lift's internal use.
\end_layout
\begin_layout Subsection
checkbox
\end_layout
\begin_layout Standard
The
\family typewriter
checkbox
\family default
method generates a checkbox form element, taking an initial Boolean value
as well as a function
\begin_inset Formula $(Boolean)\Rightarrow Any$
\end_inset
that is called when the checkbox is submitted.
If you've done a lot of HTML form processing you might wonder how this
actually occurs, since
\emph on
an unchecked checkbox is not actually submitted as part of a form
\emph default
.
Lift works around this by adding a hidden form element for each checkbox
with the same element name, but with a false value, to ensure that the
callback function is always called.
Because more than one XML node is returned by the generator, you can’t
use the % metadata mechanism to set attributes on the check box element.
Instead, use attribute pairs as arguments to the generator function as
outlined in Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:Direct-attr-manip"
\end_inset
.
\end_layout
\begin_layout Standard
For example, Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:A-checkbox-example"
\end_inset
shows a checkbox with an id of
\begin_inset Quotes eld
\end_inset
snazzy
\begin_inset Quotes erd
\end_inset
and a class attribute set to
\begin_inset Quotes eld
\end_inset
woohoo.
\begin_inset Quotes erd
\end_inset
\begin_inset Note Note
status open
\begin_layout Plain Layout
Discuss toForm here
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
A Checkbox Example
\begin_inset CommandInset label
LatexCommand label
name "lst:A-checkbox-example"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
SHtml.checkbox_id(false, if (_) frobnicate(),
\end_layout
\begin_layout Plain Layout
Full("snazzy"), "class" -> "woohoo")
\end_layout
\end_inset
\end_layout
\begin_layout Subsection
hidden
\end_layout
\begin_layout Standard
The
\family typewriter
hidden
\family default
method generates a hidden form field.
Unlike the HTML hidden field, the hidden tag is not intended to hold a
plain value; rather, in Lift it takes a function
\begin_inset Formula $()\Rightarrow Any$
\end_inset
argument that is called when the form is submitted.
As with most of the other generators, it also takes a final varargs sequence
of
\family typewriter
Pair[String,String]
\family default
attributes to be added to the XML node.
Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:A-hidden-example"
\end_inset
shows an example of using a hidden field to
\begin_inset Quotes eld
\end_inset
log
\begin_inset Quotes erd
\end_inset
information.
(When the form is submitted,
\begin_inset Quotes eld
\end_inset
Form was submitted
\begin_inset Quotes erd
\end_inset
will be printed to stdout.
This can be a useful trick for debugging if you're not using a full-blown
IDE.)
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
A Hidden Example
\begin_inset CommandInset label
LatexCommand label
name "lst:A-hidden-example"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
SHtml.hidden(() => println("Form was submitted"))
\end_layout
\end_inset
\end_layout
\begin_layout Subsection
link
\end_layout
\begin_layout Standard
The link method generates a standard HTML link to a page (an
\family typewriter
<a>
\family default
tag, or anchor
\begin_inset Index
status open
\begin_layout Plain Layout
anchor
\end_layout
\end_inset