-
Notifications
You must be signed in to change notification settings - Fork 8
/
form-controls.xsl
1258 lines (1125 loc) · 44.3 KB
/
form-controls.xsl
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
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
xmlns:form="http://nick-dunn.co.uk/xslt/form-controls"
extension-element-prefixes="exsl form">
<!--
Name: Form Controls
Description: An XSLT utility to create powerful HTML forms with Symphony
Version: 1.6
Author: Nick Dunn <http://github.com/nickdunn>
URL: http://github.com/nickdunn/form-controls/tree/master
-->
<!-- Class valid added to invalid form controls -->
<xsl:variable name="form:invalid-class" select="'invalid'"/>
<!--
Name: validation-summary
Description: Renders a success/error message and list of invalid fields
Returns: HTML
Parameters:
* `error-message` (optional, string/XPath): Error notification message. Defaults to Symphony Event message
* `success-message` (optional, string/XPath): Success notification message. Defaults to Symphony Event message
* `errors` (optional, XML): Custom error messages for individual fields as <error> nodes. Defaults to Symphony Event defaults
* `section` (optional, string): Use with EventEx to show errors for a specific section handle only
* `event` (optional, XPath): XPath expression to the specific event within the page <events> node
-->
<xsl:template name="form:validation-summary">
<xsl:param name="event" select="$form:event"/>
<xsl:param name="error-message" select="$event/message"/>
<xsl:param name="success-message" select="$event/message"/>
<xsl:param name="errors"/>
<xsl:param name="section" select="'fields'"/>
<xsl:variable name="index-key">
<xsl:call-template name="form:section-index-key">
<xsl:with-param name="section" select="$section"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="section-handle">
<xsl:call-template name="form:section-handle">
<xsl:with-param name="section" select="$section"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="event-result">
<xsl:choose>
<xsl:when test="$section!='fields' and $index-key!=''">
<xsl:copy-of select="$event//entry[@section-handle=$section-handle and @index-key=$index-key]"/>
</xsl:when>
<xsl:when test="$section!='fields'">
<xsl:copy-of select="$event//entry[@section-handle=$section-handle]"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="$event"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:choose>
<xsl:when test="exsl:node-set($event-result)//*[@result='error']">
<div class="validation-summary error">
<xsl:choose>
<xsl:when test="exsl:node-set($error-message)/*">
<xsl:copy-of select="$error-message"/>
</xsl:when>
<xsl:otherwise>
<p><xsl:value-of select="$error-message"/></p>
</xsl:otherwise>
</xsl:choose>
<ul>
<xsl:for-each select="exsl:node-set($event-result)//*[not(name()='entry') and @type]">
<li>
<label>
<xsl:attribute name="for">
<xsl:choose>
<xsl:when test="parent::entry/@index-key">
<xsl:value-of select="concat(parent::entry/@section-handle,'-',parent::entry/@index-key,'-',name())"/>
</xsl:when>
<xsl:when test="parent::entry">
<xsl:value-of select="concat(parent::entry/@section-handle,'-',name())"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('fields-',name())"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:call-template name="form:validation-message">
<xsl:with-param name="errors" select="$errors" />
</xsl:call-template>
</label>
</li>
</xsl:for-each>
</ul>
</div>
</xsl:when>
<xsl:when test="exsl:node-set($event-result)//*[@result='success']">
<div class="validation-summary success">
<xsl:choose>
<xsl:when test="exsl:node-set($success-message)/*">
<xsl:copy-of select="$success-message"/>
</xsl:when>
<xsl:otherwise>
<p><xsl:value-of select="$success-message"/></p>
</xsl:otherwise>
</xsl:choose>
</div>
</xsl:when>
</xsl:choose>
</xsl:template>
<!--
Name: validation
Description: Renders a validation message for a given field defined by `$handle`
Returns: HTML
Parameters:
* `handle` (mandatory, string): Handle of the field name
* `errors` (optional, XML): Custom error messages for individual fields as <error> nodes. Defaults to Symphony Event defaults
* `section` (optional, string): Use with EventEx to show errors for a specific section handle only
* `event` (optional, XPath): XPath expression to the specific event within the page <events> node
-->
<xsl:template name="form:validation">
<xsl:param name="handle"/>
<xsl:param name="errors"/>
<xsl:param name="section" select="'fields'"/>
<xsl:param name="event" select="$form:event"/>
<xsl:variable name="index-key">
<xsl:call-template name="form:section-index-key">
<xsl:with-param name="section" select="$section"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="section-handle">
<xsl:call-template name="form:section-handle">
<xsl:with-param name="section" select="$section"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="event-result">
<xsl:choose>
<xsl:when test="$section!='fields' and $index-key!=''">
<xsl:copy-of select="$event//entry[@section-handle=$section-handle and @index-key=$index-key]"/>
</xsl:when>
<xsl:when test="$section!='fields'">
<xsl:copy-of select="$event//entry[@section-handle=$section-handle]"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="$event"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:if test="exsl:node-set($event-result)//*[@result='error']">
<xsl:for-each select="exsl:node-set($event-result)//*[not(name()='entry') and name() = $handle and @type]">
<div class='validation error'>
<xsl:call-template name="form:validation-message">
<xsl:with-param name="errors" select="$errors" />
</xsl:call-template>
</div>
</xsl:for-each>
</xsl:if>
</xsl:template>
<xsl:template name="form:validation-label">
<xsl:param name="label"/>
<xsl:choose>
<xsl:when test="$label/*">
<xsl:copy-of select="$label/text() | $label/*"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$label"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="form:validation-message">
<xsl:param name="errors"/>
<xsl:choose>
<!-- @message and a section specified -->
<xsl:when test="@message and exsl:node-set($errors)/error[@handle=name(current()) and @message=current()/@message and @section=current()/parent::entry/@section-handle]">
<xsl:call-template name="form:validation-label">
<xsl:with-param name="label" select="exsl:node-set($errors)/error[@handle=name(current()) and @message=current()/@message and @section=current()/parent::entry/@section-handle]"/>
</xsl:call-template>
</xsl:when>
<!-- missing -->
<xsl:when test="@message and exsl:node-set($errors)/error[@handle=name(current()) and string(@message)=string(current()/@message)]">
<xsl:call-template name="form:validation-label">
<xsl:with-param name="label" select="exsl:node-set($errors)/error[@handle=name(current()) and @message=current()/@message]"/>
</xsl:call-template>
</xsl:when>
<!-- missing and a section specified -->
<xsl:when test="@type='missing' and exsl:node-set($errors)/error[@handle=name(current()) and contains(@type,'missing') and @section=current()/parent::entry/@section-handle]">
<xsl:call-template name="form:validation-label">
<xsl:with-param name="label" select="exsl:node-set($errors)/error[@handle=name(current()) and contains(@type,'missing') and @section=current()/parent::entry/@section-handle]"/>
</xsl:call-template>
</xsl:when>
<!-- missing -->
<xsl:when test="@type='missing' and exsl:node-set($errors)/error[@handle=name(current()) and contains(@type,'missing')]">
<xsl:call-template name="form:validation-label">
<xsl:with-param name="label" select="exsl:node-set($errors)/error[@handle=name(current()) and contains(@type,'missing')]"/>
</xsl:call-template>
</xsl:when>
<!-- invalid and a section specified-->
<xsl:when test="@type='invalid' and exsl:node-set($errors)/error[@handle=name(current()) and contains(@type,'invalid') and @section=current()/parent::entry/@section-handle]">
<xsl:call-template name="form:validation-label">
<xsl:with-param name="label" select="exsl:node-set($errors)/error[@handle=name(current()) and contains(@type,'invalid') and @section=current()/parent::entry/@section-handle]"/>
</xsl:call-template>
</xsl:when>
<!-- invalid -->
<xsl:when test="@type='invalid' and exsl:node-set($errors)/error[@handle=name(current()) and contains(@type,'invalid')]">
<xsl:call-template name="form:validation-label">
<xsl:with-param name="label" select="exsl:node-set($errors)/error[@handle=name(current()) and contains(@type,'invalid')]"/>
</xsl:call-template>
</xsl:when>
<!-- no specific type match, section specified -->
<xsl:when test="exsl:node-set($errors)/error[@handle=name(current()) and not(@type) and @section=current()/parent::entry/@section-handle]">
<xsl:call-template name="form:validation-label">
<xsl:with-param name="label" select="exsl:node-set($errors)/error[@handle=name(current()) and @section=current()/parent::entry/@section-handle]"/>
</xsl:call-template>
</xsl:when>
<!-- no specific type match -->
<xsl:when test="exsl:node-set($errors)/error[@handle=name(current()) and not(@type) and not(@message)]">
<xsl:call-template name="form:validation-label">
<xsl:with-param name="label" select="exsl:node-set($errors)/error[@handle=name(current())]"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="@message">
<xsl:value-of select="@message"/>
</xsl:when>
<xsl:otherwise>
<span class="field-name">
<xsl:value-of select="translate(name(),'-',' ')"/>
</span>
<xsl:text> is </xsl:text>
<xsl:value-of select="@type"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!--
Name: form:label
Description: Renders an HTML `label` element that can be explicitly assigned to another form element. Can be wrapped around other controls.
Returns: HTML <label> element
Parameters:
* `for` (optional, string): Handle of a Symphony field name that this label is associated with
* `text` (optional, string): Text value of the label. Defaults to field name ($for value)
* `child` (optional, XML): Places this XML inside the label, for wrapping elements with the label
* `child-position` (optional, string): Place the child before or after the label text. Defaults to "after"
* `class` (optional, string): Value of the HTML @class attribute
* `section` (optional, string): Use with EventEx to change "fields[...]" to a section handle
* `event` (optional, XPath): XPath expression to the specific event within the page <events> node
-->
<xsl:template name="form:label">
<xsl:param name="for"/>
<xsl:param name="text"/>
<xsl:param name="child"/>
<xsl:param name="child-position" select="'after'"/>
<xsl:param name="class"/>
<xsl:param name="template"/>
<xsl:param name="section" select="'fields'"/>
<xsl:param name="event" select="$form:event"/>
<xsl:param name="handle" select="$for"/>
<xsl:element name="label" use-attribute-sets="form:attribute-class">
<xsl:if test="$for">
<xsl:attribute name="for">
<xsl:call-template name="form:control-id">
<xsl:with-param name="name">
<xsl:call-template name="form:control-name">
<xsl:with-param name="handle" select="$for"/>
<xsl:with-param name="section" select="$section"/>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:attribute>
</xsl:if>
<xsl:if test="$child and $child-position='before'">
<xsl:copy-of select="$child"/>
</xsl:if>
<xsl:variable name="text">
<xsl:choose>
<xsl:when test="$text and $child">
<xsl:value-of select="concat($text,' ')"/>
</xsl:when>
<xsl:when test="$text">
<xsl:value-of select="$text"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat($for,' ')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:choose>
<xsl:when test="$template">
<xsl:apply-templates select="exsl:node-set($template)" mode="form:replace-template">
<xsl:with-param name="replacement" select="$text"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="$child and $child-position='after'">
<xsl:copy-of select="$child"/>
</xsl:if>
</xsl:element>
</xsl:template>
<!--
Name: form:checkbox
Description: Renders an HTML checkbox `input` element
Returns: HTML <input> element
Parameters:
* `handle` (mandatory, string): Handle of the field name
* `checked` (optional, string): Initial checked state ("yes", "no"). Defaults to "no"
* `checked-by-default` (optional, string): When there is no initial $checked value (a fresh form), check by default ("yes", "no"). Defaults to "no"
* `class` (optional, string): Class attribute value
* `title` (optional, string): Title attribute value
* `section` (optional, string): Use with EventEx to change "fields[...]" to a section handle
* `event` (optional, XPath): XPath expression to the specific event within the page <events> node
* `allow-multiple` (optional, string): Internal use only ("yes", "no"). Whether checkbox is part of a checkbox list. Defaults to "no"
* `allow-multiple-value` (optional, string): Internal use only. Overrides default "yes" value when part of a checkbox list
-->
<xsl:template name="form:checkbox">
<xsl:param name="handle"/>
<xsl:param name="checked"/>
<xsl:param name="checked-by-default" select="'no'"/>
<xsl:param name="class"/>
<xsl:param name="title"/>
<xsl:param name="section" select="'fields'"/>
<xsl:param name="event" select="$form:event"/>
<xsl:param name="allow-multiple" select="'no'"/>
<xsl:param name="allow-multiple-value"/>
<xsl:if test="$allow-multiple='no'">
<input type="hidden" value="no">
<xsl:attribute name="name">
<xsl:call-template name="form:control-name">
<xsl:with-param name="handle" select="$handle"/>
<xsl:with-param name="section" select="$section"/>
</xsl:call-template>
</xsl:attribute>
</input>
</xsl:if>
<xsl:call-template name="form:radio">
<xsl:with-param name="event" select="$event"/>
<xsl:with-param name="handle" select="$handle"/>
<xsl:with-param name="section" select="$section"/>
<xsl:with-param name="value">
<xsl:choose>
<xsl:when test="$allow-multiple='yes'">
<xsl:value-of select="$allow-multiple-value"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$checked"/>
</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
<xsl:with-param name="existing-value">
<xsl:if test="$allow-multiple='yes'">
<xsl:value-of select="$checked"/>
</xsl:if>
</xsl:with-param>
<xsl:with-param name="checked-by-default" select="$checked-by-default"/>
<xsl:with-param name="class" select="$class"/>
<xsl:with-param name="title" select="$title"/>
<xsl:with-param name="type" select="'checkbox'"/>
<xsl:with-param name="allow-multiple" select="$allow-multiple"/>
</xsl:call-template>
</xsl:template>
<!--
Name: form:radio
Description: Renders an HTML radio `input` element
Returns: HTML <input> element
Parameters:
* `handle` (mandatory, string): Handle of the field name
* `value` (optional, string): The selected value for this radio sent when the form is submitted
* `existing-value` (optional, string): An initial value. Selects radio if it matches $value
* `checked-by-default` (optional, string): When there is no initial $existing-value (a fresh form), select by default ("yes", "no"). Defaults to "no"
* `class` (optional, string): Class attribute value
* `title` (optional, string): Title attribute value
* `type` (optional, string): Internal use only ("radio", "checkbox"). Defaults to "radio"
* `section` (optional, string): Use with EventEx to change "fields[...]" to a section handle
* `event` (optional, XPath): XPath expression to the specific event within the page <events> node
* `allow-multiple` (optional, string): Internal use only ("yes", "no"). Whether control is part of a radio/checkbox list. Defaults to "no"
-->
<xsl:template name="form:radio">
<xsl:param name="handle"/>
<xsl:param name="value"/>
<xsl:param name="existing-value"/>
<xsl:param name="checked-by-default" select="'no'"/>
<xsl:param name="class"/>
<xsl:param name="title"/>
<xsl:param name="event" select="$form:event"/>
<xsl:param name="section" select="'fields'"/>
<xsl:param name="type" select="'radio'"/>
<xsl:param name="allow-multiple" select="'no'"/>
<xsl:variable name="value" select="normalize-space($value)"/>
<xsl:variable name="selected-value" select="normalize-space($existing-value)"/>
<xsl:variable name="postback-value">
<xsl:call-template name="form:postback-value">
<xsl:with-param name="event" select="$event"/>
<xsl:with-param name="handle" select="$handle"/>
<xsl:with-param name="section" select="$section"/>
</xsl:call-template>
</xsl:variable>
<xsl:element name="input" use-attribute-sets="form:attributes-general">
<xsl:attribute name="type"><xsl:value-of select="$type"/></xsl:attribute>
<xsl:if test="$allow-multiple='yes'">
<xsl:attribute name="name">
<xsl:call-template name="form:control-name">
<xsl:with-param name="handle" select="$handle"/>
<xsl:with-param name="section" select="$section"/>
</xsl:call-template>
<xsl:text>[]</xsl:text>
</xsl:attribute>
</xsl:if>
<xsl:attribute name="value">
<xsl:choose>
<xsl:when test="$type='checkbox' and $allow-multiple='no'">
<xsl:text>yes</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$value"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:choose>
<xsl:when test="$type='radio'">
<xsl:choose>
<xsl:when test="$value=$postback-value">
<xsl:attribute name="checked">checked</xsl:attribute>
</xsl:when>
<xsl:when test="$postback-value='' and $value=$selected-value">
<xsl:attribute name="checked">checked</xsl:attribute>
</xsl:when>
<xsl:when test="$postback-value='' and $selected-value='' and $checked-by-default='yes'">
<xsl:attribute name="checked">checked</xsl:attribute>
</xsl:when>
</xsl:choose>
</xsl:when>
<xsl:when test="$type='checkbox'">
<xsl:choose>
<!-- checked from the event -->
<xsl:when test="$postback-value='Yes' or $postback-value='yes'">
<xsl:attribute name="checked">checked</xsl:attribute>
</xsl:when>
<!-- checked from an initial value -->
<xsl:when test="$postback-value='' and ($value='Yes' or $value='yes')">
<xsl:attribute name="checked">checked</xsl:attribute>
</xsl:when>
<!-- if no event and no initial value, see it checked by default -->
<xsl:when test="$postback-value='' and $value='' and $checked-by-default='yes'">
<xsl:attribute name="checked">checked</xsl:attribute>
</xsl:when>
<!-- when allowing multiple, check if this value exists -->
<xsl:when test="$allow-multiple='yes' and $selected-value='yes'">
<xsl:attribute name="checked">checked</xsl:attribute>
</xsl:when>
</xsl:choose>
</xsl:when>
</xsl:choose>
</xsl:element>
</xsl:template>
<!--
Name: form:input
Description: Renders an HTML text `input` element with support for `password` and `file` types
Returns: HTML <input> element
Parameters:
* `handle` (mandatory, string): Handle of the field name
* `value` (optional, string): Initial value of form control. Will not work for `file` inputs.
* `type` (optional, string): Type attribute value ("text", "password" "file", "hidden"). For "checkbox" and "radio" types see form:checkbox and form:radio templates. Defaults to "text"
* `class` (optional, string): Class attribute value
* `title` (optional, string): Title attribute value
* `size` (optional, string): Size attribute value
* `maxlength` (optional, string): Maxlength attribute value
* `autocomplete` (optional, string): Autocomplete attribute value ("off"). Not set by default
* `placeholder` (optional, string): Placeholder text. Not set by default
* `section` (optional, string): Use with EventEx to change "fields[...]" to a section handle
* `event` (optional, XPath): XPath expression to the specific event within the page <events> node
-->
<xsl:template name="form:input">
<xsl:param name="handle"/>
<xsl:param name="value"/>
<xsl:param name="class"/>
<xsl:param name="title"/>
<xsl:param name="type" select="'text'"/>
<xsl:param name="size"/>
<xsl:param name="maxlength"/>
<xsl:param name="autocomplete"/>
<xsl:param name="placeholder"/>
<xsl:param name="section" select="'fields'"/>
<xsl:param name="event" select="$form:event"/>
<xsl:variable name="initial-value" select="normalize-space($value)"/>
<xsl:variable name="postback-value">
<xsl:call-template name="form:postback-value">
<xsl:with-param name="event" select="$event"/>
<xsl:with-param name="handle" select="$handle"/>
<xsl:with-param name="section" select="$section"/>
</xsl:call-template>
</xsl:variable>
<xsl:element name="input" use-attribute-sets="form:attributes-general">
<xsl:attribute name="type"><xsl:value-of select="$type"/></xsl:attribute>
<xsl:if test="$size">
<xsl:attribute name="size"><xsl:value-of select="$size"/></xsl:attribute>
</xsl:if>
<xsl:if test="$maxlength">
<xsl:attribute name="maxlength"><xsl:value-of select="$maxlength"/></xsl:attribute>
</xsl:if>
<xsl:if test="$autocomplete='off'">
<xsl:attribute name="autocomplete"><xsl:value-of select="$autocomplete"/></xsl:attribute>
</xsl:if>
<xsl:if test="$placeholder">
<xsl:attribute name="placeholder"><xsl:value-of select="$placeholder"/></xsl:attribute>
</xsl:if>
<xsl:attribute name="value">
<xsl:choose>
<xsl:when test="$event and ($initial-value != $postback-value)">
<xsl:value-of select="$postback-value"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$initial-value"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:element>
</xsl:template>
<!--
Name: form:textarea
Description: Renders an HTML `textarea` element
Returns: HTML <textarea> element
Parameters:
* `handle` (mandatory, string): Handle of the field name
* `value` (optional, string): Contents of the textarea
* `class` (optional, string): Class attribute value
* `rows` (optional, string): Rows attribute value
* `cols` (optional, string): cols attribute value
* `section` (optional, string): Use with EventEx to change "fields[...]" to a section handle
* `event` (optional, XPath): XPath expression to the specific event within the page <events> node
-->
<xsl:template name="form:textarea">
<xsl:param name="handle"/>
<xsl:param name="value"/>
<xsl:param name="class"/>
<xsl:param name="title"/>
<xsl:param name="rows"/>
<xsl:param name="cols"/>
<xsl:param name="section" select="'fields'"/>
<xsl:param name="event" select="$form:event"/>
<xsl:variable name="initial-value" select="$value"/>
<xsl:variable name="initial-value-normalized" select="normalize-space($value)"/>
<xsl:variable name="postback-value">
<xsl:call-template name="form:postback-value">
<xsl:with-param name="event" select="$event"/>
<xsl:with-param name="handle" select="$handle"/>
<xsl:with-param name="section" select="$section"/>
<xsl:with-param name="normalize" select="'no'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="postback-value-normalized">
<xsl:call-template name="form:postback-value">
<xsl:with-param name="event" select="$event"/>
<xsl:with-param name="handle" select="$handle"/>
<xsl:with-param name="section" select="$section"/>
</xsl:call-template>
</xsl:variable>
<xsl:element name="textarea" use-attribute-sets="form:attributes-general">
<xsl:if test="$rows">
<xsl:attribute name="rows"><xsl:value-of select="$rows"/></xsl:attribute>
</xsl:if>
<xsl:if test="$cols">
<xsl:attribute name="cols"><xsl:value-of select="$cols"/></xsl:attribute>
</xsl:if>
<xsl:choose>
<xsl:when test="$event and ($initial-value-normalized != $postback-value-normalized)">
<xsl:value-of select="$postback-value"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$initial-value"/>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:template>
<!--
Name: form:select
Description: Renders an HTML `select` element
Returns: HTML <select> element
Parameters:
* `handle` (mandatory, string): Handle of the field name
* `options` (mandatory, XPath/XML): Options to build a list of <option> elements. Has presets! See examples.
* `value` (optional, string/XML): Initial selected value
* `class` (optional, string): Class attribute value
* `title` (optional, string): Title attribute value
* `allow-multiple` (optional, string): Allow selection of multiple options ("yes", "no"). Defaults to "no"
* `section` (optional, string): Use with EventEx to change "fields[...]" to a section handle
* `event` (optional, XPath): XPath expression to the specific event within the page <events> node
-->
<xsl:template name="form:select">
<xsl:param name="handle"/>
<xsl:param name="value"/>
<xsl:param name="class"/>
<xsl:param name="title"/>
<xsl:param name="options"/>
<xsl:param name="allow-multiple"/>
<xsl:param name="section" select="'fields'"/>
<xsl:param name="event" select="$form:event"/>
<xsl:variable name="initial-value">
<xsl:choose>
<xsl:when test="exsl:node-set($value)/*">
<xsl:copy-of select="$value"/>
</xsl:when>
<xsl:otherwise>
<value><xsl:value-of select="normalize-space($value)"/></value>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="postback-value">
<xsl:call-template name="form:postback-value">
<xsl:with-param name="event" select="$event"/>
<xsl:with-param name="handle" select="$handle"/>
<xsl:with-param name="section" select="$section"/>
</xsl:call-template>
</xsl:variable>
<xsl:element name="select" use-attribute-sets="form:attributes-general">
<xsl:if test="$allow-multiple='yes'">
<xsl:attribute name="multiple">multiple</xsl:attribute>
<xsl:variable name="name">
<xsl:call-template name="form:control-name">
<xsl:with-param name="handle" select="$handle"/>
<xsl:with-param name="section" select="$section"/>
</xsl:call-template>
<xsl:text>[]</xsl:text>
</xsl:variable>
<xsl:attribute name="name"><xsl:value-of select="$name"/></xsl:attribute>
</xsl:if>
<xsl:variable name="options">
<xsl:choose>
<xsl:when test="starts-with(string($options),'days')">
<xsl:if test="not(contains(string($options),'no-label'))">
<option value="">Day</option>
</xsl:if>
<xsl:call-template name="form:incrementor">
<xsl:with-param name="start" select="'1'"/>
<xsl:with-param name="iterations" select="31"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="starts-with(string($options),'months')">
<xsl:if test="not(contains(string($options),'no-label'))">
<option value="">Month</option>
</xsl:if>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</xsl:when>
<xsl:when test="contains(string($options),'years')">
<xsl:if test="not(contains(string($options),'no-label'))">
<option value="">Year</option>
</xsl:if>
<xsl:choose>
<xsl:when test="contains(string($options),'years-')">
<xsl:call-template name="form:incrementor">
<xsl:with-param name="start" select="$this-year"/>
<xsl:with-param name="iterations" select="number(substring-after($options,'-') + 1)"/>
<xsl:with-param name="direction" select="'-'"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="contains(string($options),'years+')">
<xsl:call-template name="form:incrementor">
<xsl:with-param name="start" select="$this-year"/>
<xsl:with-param name="iterations" select="number(substring-after($options,'+') + 1)"/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="exsl:node-set($options)/* | exsl:node-set($options)">
<xsl:if test="text()!=''">
<option>
<xsl:if test="@handle or @id or @link-id or @link-handle or @value">
<xsl:attribute name="value">
<xsl:value-of select="@handle | @id | @link-id | @link-handle | @value"/>
</xsl:attribute>
</xsl:if>
<xsl:value-of select="text()"/>
</option>
</xsl:if>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:for-each select="exsl:node-set($options)/option">
<xsl:variable name="option-value">
<xsl:choose>
<xsl:when test="@value">
<xsl:value-of select="@value"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="text()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<option>
<xsl:if test="@value">
<xsl:attribute name="value"><xsl:value-of select="@value"/></xsl:attribute>
</xsl:if>
<xsl:if test="
($event and (
$option-value = $postback-value or
exsl:node-set($postback-value)//*[text()=$option-value] or
exsl:node-set($postback-value)//*[@id=$option-value]
)) or
(not($event) and (
$option-value = $initial-value or
exsl:node-set($initial-value)//*[text()=$option-value] or
exsl:node-set($initial-value)//*[@id=$option-value]
))
">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>
<xsl:value-of select="text()"/>
</option>
</xsl:for-each>
</xsl:element>
</xsl:template>
<!--
Name: form:radiobutton-list
Description: Renders a collection of HTML radio `input` elements wrapped with `label` elements
Returns: HTML <select> element
Parameters:
* `handle` (mandatory, string): Handle of the field name
* `options` (mandatory, XPath/XML): Options to build a list of <option> elements. Has presets! See examples.
* `value` (optional, string): Initial selected value
* `class` (optional, string): Class attribute value
* `title` (optional, string): Title attribute value
* `section` (optional, string): Use with EventEx to change "fields[...]" to a section handle
* `event` (optional, XPath): XPath expression to the specific event within the page <events> node
-->
<xsl:template name="form:radiobutton-list">
<xsl:param name="handle"/>
<xsl:param name="value"/>
<xsl:param name="class"/>
<xsl:param name="title"/>
<xsl:param name="options"/>
<xsl:param name="section" select="'fields'"/>
<xsl:param name="event" select="$form:event"/>
<xsl:variable name="select">
<xsl:call-template name="form:select">
<xsl:with-param name="event" select="$event"/>
<xsl:with-param name="handle" select="$handle"/>
<xsl:with-param name="section" select="$section"/>
<xsl:with-param name="value" select="$value"/>
<xsl:with-param name="class" select="$class"/>
<xsl:with-param name="title" select="$title"/>
<xsl:with-param name="options" select="$options"/>
</xsl:call-template>
</xsl:variable>
<xsl:for-each select="exsl:node-set($select)//option">
<xsl:call-template name="form:label">
<xsl:with-param name="event" select="$event"/>
<xsl:with-param name="handle" select="$handle"/>
<xsl:with-param name="section" select="$section"/>
<xsl:with-param name="text" select="."/>
<xsl:with-param name="child-position" select="'before'"/>
<xsl:with-param name="child">
<xsl:call-template name="form:radio">
<xsl:with-param name="event" select="$event"/>
<xsl:with-param name="handle" select="$handle"/>
<xsl:with-param name="section" select="$section"/>
<xsl:with-param name="value">
<xsl:choose>
<xsl:when test="@value"><xsl:value-of select="@value"/></xsl:when>
<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
</xsl:choose>
</xsl:with-param>
<xsl:with-param name="existing-value">
<xsl:if test="@selected">
<xsl:choose>
<xsl:when test="@value"><xsl:value-of select="@value"/></xsl:when>
<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:with-param>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
<!--
Name: form:checkbox-list
Description: Renders a collection of HTML checkbox `input` elements wrapped with `label` elements
Returns: HTML <select> element
Parameters:
* `handle` (mandatory, string): Handle of the field name
* `options` (mandatory, XPath/XML): Options to build a list of <option> elements. Has presets! See examples.
* `value` (optional, string/XML): Initial selected value
* `class` (optional, string): Class attribute value
* `title` (optional, string): Title attribute value
* `section` (optional, string): Use with EventEx to change "fields[...]" to a section handle
* `event` (optional, XPath): XPath expression to the specific event within the page <events> node
-->
<xsl:template name="form:checkbox-list">
<xsl:param name="handle"/>
<xsl:param name="value"/>
<xsl:param name="class"/>
<xsl:param name="title"/>
<xsl:param name="options"/>
<xsl:param name="section" select="'fields'"/>
<xsl:param name="event" select="$form:event"/>
<xsl:variable name="select">
<xsl:call-template name="form:select">
<xsl:with-param name="event" select="$event"/>
<xsl:with-param name="handle" select="$handle"/>
<xsl:with-param name="section" select="$section"/>
<xsl:with-param name="value" select="$value"/>
<xsl:with-param name="class" select="$class"/>
<xsl:with-param name="title" select="$title"/>
<xsl:with-param name="options" select="$options"/>
<xsl:with-param name="allow-multiple" select="'yes'"/>
</xsl:call-template>
</xsl:variable>
<xsl:for-each select="exsl:node-set($select)//option">
<xsl:call-template name="form:label">
<xsl:with-param name="event" select="$event"/>
<xsl:with-param name="handle" select="$handle"/>
<xsl:with-param name="section" select="$section"/>
<xsl:with-param name="text" select="."/>
<xsl:with-param name="child-position" select="'before'"/>
<xsl:with-param name="child">
<xsl:call-template name="form:checkbox">
<xsl:with-param name="event" select="$event"/>
<xsl:with-param name="handle" select="$handle"/>
<xsl:with-param name="section" select="$section"/>
<xsl:with-param name="allow-multiple" select="'yes'"/>
<xsl:with-param name="allow-multiple-value">
<xsl:choose>
<xsl:when test="@value"><xsl:value-of select="@value"/></xsl:when>
<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
</xsl:choose>
</xsl:with-param>
<xsl:with-param name="checked">
<xsl:if test="@selected">yes</xsl:if>
</xsl:with-param>
<xsl:with-param name="existing-value">
<xsl:choose>
<xsl:when test="@value"><xsl:value-of select="@value"/></xsl:when>
<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
</xsl:choose>
</xsl:with-param>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
<!-- Attributes common to all form controls (name, id, title) -->
<xsl:attribute-set name="form:attributes-general" use-attribute-sets="form:attribute-class">
<xsl:attribute name="name">
<xsl:call-template name="form:control-name">
<xsl:with-param name="handle" select="$handle"/>
<xsl:with-param name="section" select="$section"/>
</xsl:call-template>
</xsl:attribute>
<xsl:attribute name="id">
<xsl:call-template name="form:control-id">
<xsl:with-param name="name">
<xsl:call-template name="form:control-name">
<xsl:with-param name="handle" select="$handle"/>
<xsl:with-param name="section" select="$section"/>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:attribute>
<xsl:attribute name="title">
<xsl:value-of select="$title"/>
</xsl:attribute>
</xsl:attribute-set>
<!-- Class attribute separate so it can be applied independently (for label elements) -->
<xsl:attribute-set name="form:attribute-class">
<xsl:attribute name="class">
<xsl:variable name="valid">
<xsl:call-template name="form:control-is-valid">
<xsl:with-param name="event" select="$event"/>
<xsl:with-param name="handle" select="$handle"/>
<xsl:with-param name="section" select="$section"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="$class or $valid='false'">
<xsl:value-of select="$class"/>
<xsl:if test="$valid='false'">
<xsl:if test="$class!=''">
<xsl:text> </xsl:text>
</xsl:if>
<xsl:value-of select="$form:invalid-class"/>
</xsl:if>
</xsl:if>