Ewl » Canonical The canonical version of EWL Read More
Clone URL:  
FormItemStatics.cs
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
using System; using System.Collections.Generic; using System.IO; using System.Linq; using Humanizer; namespace EnterpriseWebLibrary.DevelopmentUtility.Operations.CodeGeneration { internal static class FormItemStatics { internal static void WriteFormItemGetters( TextWriter writer, ModificationField field ) { // Some of these form item getters need modification methods to be executed to work properly. They return these methods, as out parameters, instead of // just adding them to the data modification. This allows client code on a page to specify the order of modification methods, which is important because // there may be both child modifications (like file collections) and one-to-many modifications (like M+Vision references for an applicant) on the same // page, and the main modification needs to execute between these. writeStringFormItemGetters( writer, field ); writer.WriteLine( "#pragma warning disable CS0618" ); // remove when EwfCheckBox and BlockCheckBox are gone writeNumericFormItemGetters( writer, field ); writeCheckboxFormItemGetters( writer, field ); writeBoolFormItemGetters( writer, field ); writer.WriteLine( "#pragma warning restore CS0618" ); // remove when EwfCheckBox and BlockCheckBox are gone writeDateFormItemGetters( writer, field ); writeEnumerableFormItemGetters( writer, field ); writeGuidFormItemGetters( writer, field ); writeGenericGetter( writer, field ); writeGenericGetterWithoutValueParams( writer, field, null ); writeGenericGetterWithoutValueParams( writer, field, false ); writeGenericGetterWithoutValueParams( writer, field, true ); writeGenericGetterWithValueParams( writer, field, null ); writeGenericGetterWithValueParams( writer, field, false ); writeGenericGetterWithValueParams( writer, field, true ); } private static void writeStringFormItemGetters( TextWriter writer, ModificationField field ) { if( !field.TypeIs( typeof( string ) ) ) return; writeFormItemGetter( writer, field, "TextControl", getAllowEmptyParameter( false ).ToCollection(), false, new CSharpParameter( "TextControlSetup", "controlSetup", defaultValue: "null" ).ToCollection(), "string", Enumerable.Empty<CSharpParameter>(), true, dv => "{0}.ToTextControl( allowEmpty, setup: controlSetup, value: value, maxLength: {1}, additionalValidationMethod: additionalValidationMethod ).ToFormItem( setup: formItemSetup, label: label )" .FormatWith( dv, field.Size?.ToString() ?? "null" ) ); writeFormItemGetter( writer, field, "EmailAddressControl", getAllowEmptyParameter( false ).ToCollection(), false, new CSharpParameter( "EmailAddressControlSetup", "controlSetup", defaultValue: "null" ).ToCollection(), "string", Enumerable.Empty<CSharpParameter>(), true, dv => "{0}.ToEmailAddressControl( allowEmpty, setup: controlSetup, value: value, maxLength: {1}, additionalValidationMethod: additionalValidationMethod ).ToFormItem( setup: formItemSetup, label: label )" .FormatWith( dv, field.Size?.ToString() ?? "null" ) ); writeFormItemGetter( writer, field, "TelephoneNumberControl", getAllowEmptyParameter( false ).ToCollection(), false, new CSharpParameter( "TelephoneNumberControlSetup", "controlSetup", defaultValue: "null" ).ToCollection(), "string", Enumerable.Empty<CSharpParameter>(), true, dv => "{0}.ToTelephoneNumberControl( allowEmpty, setup: controlSetup, value: value, maxLength: {1}, additionalValidationMethod: additionalValidationMethod ).ToFormItem( setup: formItemSetup, label: label )" .FormatWith( dv, field.Size?.ToString() ?? "null" ) ); writeFormItemGetter( writer, field, "UrlControl", getAllowEmptyParameter( false ).ToCollection(), false, new CSharpParameter( "UrlControlSetup", "controlSetup", defaultValue: "null" ).ToCollection(), "string", Enumerable.Empty<CSharpParameter>(), true, dv => "{0}.ToUrlControl( allowEmpty, setup: controlSetup, value: value, maxLength: {1}, additionalValidationMethod: additionalValidationMethod ).ToFormItem( setup: formItemSetup, label: label )" .FormatWith( dv, field.Size?.ToString() ?? "null" ) ); writeFormItemGetter( writer, field, "NumericTextControl", getAllowEmptyParameter( false ).ToCollection(), false, new CSharpParameter( "NumericTextControlSetup", "controlSetup", defaultValue: "null" ).ToCollection(), "string", new CSharpParameter( "int?", "minLength", defaultValue: "null" ).ToCollection(), true, dv => "{0}.ToNumericTextControl( allowEmpty, setup: controlSetup, value: value, minLength: minLength, maxLength: {1}, additionalValidationMethod: additionalValidationMethod ).ToFormItem( setup: formItemSetup, label: label )" .FormatWith( dv, field.Size?.ToString() ?? "null" ) ); writeFormItemGetters( writer, field, "SelectList<string>", "RadioList", "string", "\"\"", new[] { new CSharpParameter( "IEnumerable<SelectListItem<string>>", "items" ) }, new CSharpParameter[ 0 ], new[] { new CSharpParameter( "bool", "useHorizontalLayout", "false" ), new CSharpParameter( "string", "defaultValueItemLabel", "\"\"" ), new CSharpParameter( "bool", "disableSingleButtonDetection", "false" ), new CSharpParameter( "FormAction", "action", "null" ), new CSharpParameter( "bool", "autoPostBack", "false" ) }, new CSharpParameter[ 0 ], "SelectList.CreateRadioList( items, v, useHorizontalLayout: useHorizontalLayout, defaultValueItemLabel: defaultValueItemLabel, disableSingleButtonDetection: disableSingleButtonDetection, action: action, autoPostBack: autoPostBack )", "control.ValidateAndGetSelectedItemIdInPostBack( postBackValues, validator )", "" ); writeFormItemGetters( writer, field, "SelectList<string>", "DropDown", "string", "\"\"", new[] { new CSharpParameter( "IEnumerable<SelectListItem<string>>", "items" ) }, new CSharpParameter[ 0 ], new[] { new CSharpParameter( "Unit?", "width", "null" ), new CSharpParameter( "string", "defaultValueItemLabel", "\"\"" ), new CSharpParameter( "bool", "placeholderIsValid", "false" ), new CSharpParameter( "string", "placeholderText", "\"Please select\"" ), new CSharpParameter( "FormAction", "action", "null" ), new CSharpParameter( "bool", "autoPostBack", "false" ) }, new CSharpParameter[ 0 ], "SelectList.CreateDropDown( items, v, width: width, defaultValueItemLabel: defaultValueItemLabel, placeholderIsValid: placeholderIsValid, placeholderText: placeholderText, action: action, autoPostBack: autoPostBack )", "control.ValidateAndGetSelectedItemIdInPostBack( postBackValues, validator )", "" ); writeFormItemGetter( writer, field, "Html", getAllowEmptyParameter( false ).ToCollection(), false, new CSharpParameter( "WysiwygHtmlEditorSetup", "editorSetup", "null" ).ToCollection(), "string", new CSharpParameter[ 0 ], true, dv => "{0}.ToHtmlEditor( allowEmpty, setup: editorSetup, value: value, maxLength: {1}, additionalValidationMethod: additionalValidationMethod ).ToFormItem( setup: formItemSetup, label: label )" .FormatWith( dv, field.Size?.ToString() ?? "null" ), additionalSummarySentences: new[] { "WARNING: Do not use this form-item getter unless you know exactly what you're doing.", "If you want to store HTML, it is almost always better to use an HTML block instead of just a string field.", "HTML blocks have special handling for intra-site URIs and may include additional features in the future.", "They also cause all of your HTML to be stored in one place, which is usually a good practice." } ); } private static void writeNumericFormItemGetters( TextWriter writer, ModificationField field ) { if( field.TypeIs( typeof( int ) ) || field.TypeIs( typeof( long ) ) ) writeFormItemGetter( writer, field, "TextControl", Enumerable.Empty<CSharpParameter>(), false, new CSharpParameter( "NumericTextControlSetup", "controlSetup", defaultValue: "null" ).ToCollection(), "SpecifiedValue<{0}>".FormatWith( field.NullableTypeName ), new CSharpParameter( field.NullableTypeName, "minValue", "null" ).ToCollection() .Append( new CSharpParameter( field.NullableTypeName, "maxValue", "null" ) ), true, dv => "{0}.ToTextControl( setup: controlSetup, value: value, minValue: minValue, maxValue: maxValue, additionalValidationMethod: additionalValidationMethod ).ToFormItem( setup: formItemSetup, label: label )" .FormatWith( dv ) ); if( field.TypeIs( typeof( int? ) ) || field.TypeIs( typeof( long? ) ) ) writeFormItemGetter( writer, field, "TextControl", Enumerable.Empty<CSharpParameter>(), false, new CSharpParameter( "NumericTextControlSetup", "controlSetup", defaultValue: "null" ).ToCollection(), "SpecifiedValue<{0}>".FormatWith( field.NullableTypeName ), getAllowEmptyParameter( true ) .ToCollection() .Append( new CSharpParameter( field.NullableTypeName, "minValue", "null" ) ) .Append( new CSharpParameter( field.NullableTypeName, "maxValue", "null" ) ), true, dv => "{0}.ToTextControl( setup: controlSetup, value: value, allowEmpty: allowEmpty, minValue: minValue, maxValue: maxValue, additionalValidationMethod: additionalValidationMethod ).ToFormItem( setup: formItemSetup, label: label )" .FormatWith( dv ) ); if( field.TypeIs( typeof( int ) ) || field.TypeIs( typeof( long ) ) || field.TypeIs( typeof( short ) ) || field.TypeIs( typeof( byte ) ) || field.TypeIs( typeof( decimal ) ) ) writeFormItemGetter( writer, field, "NumberControl", Enumerable.Empty<CSharpParameter>(), false, new CSharpParameter( "NumberControlSetup", "controlSetup", defaultValue: "null" ).ToCollection(), "SpecifiedValue<{0}>".FormatWith( field.NullableTypeName ), new CSharpParameter( field.NullableTypeName, "minValue", "null" ).ToCollection() .Append( new CSharpParameter( field.NullableTypeName, "maxValue", "null" ) ) .Append( new CSharpParameter( field.NullableTypeName, "valueStep", "null" ) ), true, dv => "{0}.ToNumberControl( setup: controlSetup, value: value, minValue: minValue, maxValue: maxValue, valueStep: valueStep, additionalValidationMethod: additionalValidationMethod ).ToFormItem( setup: formItemSetup, label: label )" .FormatWith( dv ), preFormItemStatements: getNumberControlValueStepStatements( field ) ); if( field.TypeIs( typeof( int? ) ) || field.TypeIs( typeof( long? ) ) || field.TypeIs( typeof( short? ) ) || field.TypeIs( typeof( byte? ) ) || field.TypeIs( typeof( decimal? ) ) ) writeFormItemGetter( writer, field, "NumberControl", Enumerable.Empty<CSharpParameter>(), false, new CSharpParameter( "NumberControlSetup", "controlSetup", defaultValue: "null" ).ToCollection(), "SpecifiedValue<{0}>".FormatWith( field.NullableTypeName ), getAllowEmptyParameter( true ) .ToCollection() .Append( new CSharpParameter( field.NullableTypeName, "minValue", "null" ) ) .Append( new CSharpParameter( field.NullableTypeName, "maxValue", "null" ) ) .Append( new CSharpParameter( field.NullableTypeName, "valueStep", "null" ) ), true, dv => "{0}.ToNumberControl( setup: controlSetup, value: value, allowEmpty: allowEmpty, minValue: minValue, maxValue: maxValue, valueStep: valueStep, additionalValidationMethod: additionalValidationMethod ).ToFormItem( setup: formItemSetup, label: label )" .FormatWith( dv ), preFormItemStatements: getNumberControlValueStepStatements( field ) ); if( field.TypeIs( typeof( int ) ) || field.TypeIs( typeof( long ) ) || field.TypeIs( typeof( short ) ) || field.TypeIs( typeof( byte ) ) || field.TypeIs( typeof( decimal ) ) ) writeFormItemGetter( writer, field, "ImpreciseNumberControl", new CSharpParameter( field.TypeName, "minValue" ).ToCollection().Append( new CSharpParameter( field.TypeName, "maxValue" ) ), false, new CSharpParameter( "ImpreciseNumberControlSetup", "controlSetup", defaultValue: "null" ).ToCollection(), field.NullableTypeName, new CSharpParameter( field.NullableTypeName, "valueStep", "null" ).ToCollection(), true, dv => "{0}.ToImpreciseNumberControl( minValue, maxValue, setup: controlSetup, value: value, valueStep: valueStep, additionalValidationMethod: additionalValidationMethod ).ToFormItem( setup: formItemSetup, label: label )" .FormatWith( dv ), preFormItemStatements: getNumberControlValueStepStatements( field ) ); if( field.TypeIs( typeof( int ) ) ) { writeNumberAsSelectListFormItemGetters( writer, field ); writeDurationFormItemGetter( writer, field ); writeHtmlAndFileFormItemGetters( writer, field, "int?" ); writeFileCollectionFormItemGetters( writer, field, "int" ); } if( field.TypeIs( typeof( int? ) ) ) { writeNumberAsSelectListFormItemGetters( writer, field ); writeDurationFormItemGetter( writer, field ); writeHtmlAndFileFormItemGetters( writer, field, "int?" ); } if( field.TypeIs( typeof( short ) ) ) writeNumberAsSelectListFormItemGetters( writer, field ); if( field.TypeIs( typeof( short? ) ) ) writeNumberAsSelectListFormItemGetters( writer, field ); if( field.TypeIs( typeof( byte ) ) ) writeNumberAsSelectListFormItemGetters( writer, field ); if( field.TypeIs( typeof( byte? ) ) ) writeNumberAsSelectListFormItemGetters( writer, field ); if( field.TypeIs( typeof( decimal ) ) ) { writeNumberAsSelectListFormItemGetters( writer, field ); writeLegacyCheckBoxFormItemGetters( writer, field, "decimal" ); writeDurationFormItemGetter( writer, field ); writeHtmlAndFileFormItemGetters( writer, field, "decimal?" ); writeFileCollectionFormItemGetters( writer, field, "decimal" ); } if( field.TypeIs( typeof( decimal? ) ) ) { writeNumberAsSelectListFormItemGetters( writer, field ); writeDurationFormItemGetter( writer, field ); writeHtmlAndFileFormItemGetters( writer, field, "decimal?" ); } } private static string getNumberControlValueStepStatements( ModificationField field ) { if( ( !field.TypeIs( typeof( decimal ) ) && !field.TypeIs( typeof( decimal? ) ) ) || !field.NumericScale.HasValue ) return ""; var minStep = field.NumericScale.Value == 0 ? "1" : ".{0}1m".FormatWith( string.Concat( Enumerable.Repeat( '0', Math.Min( field.NumericScale.Value, (short)28 /* max scale for decimal */ ) - 1 ) ) ); return StringTools.ConcatenateWithDelimiter( Environment.NewLine, "if( !valueStep.HasValue ) valueStep = {0};".FormatWith( minStep ), "else if( valueStep.Value % {0} != 0 ) throw new System.ApplicationException( \"The specified step is not a multiple of the field’s minimum step.\" );" .FormatWith( minStep ) ); } private static void writeCheckboxFormItemGetters( TextWriter writer, ModificationField field ) { if( !field.TypeIs( typeof( bool ) ) && !field.TypeIs( typeof( decimal ) ) ) return; // checkboxes writeFormItemGetter( writer, field, "Checkbox", new CSharpParameter[ 0 ], true, new[] { new CSharpParameter( "CheckboxSetup", "checkboxSetup", "null" ) }, field.NullableTypeName, new CSharpParameter[ 0 ], true, dv => "{0}.ToCheckbox( label, setup: checkboxSetup, value: value, additionalValidationMethod: additionalValidationMethod ).ToFormItem( setup: formItemSetup, label: formItemLabel )" .FormatWith( dv ) ); writeFormItemGetter( writer, field, "FlowCheckbox", new CSharpParameter[ 0 ], true, new[] { new CSharpParameter( "FlowCheckboxSetup", "checkboxSetup", "null" ) }, field.NullableTypeName, new CSharpParameter[ 0 ], true, dv => "{0}.ToFlowCheckbox( label, setup: checkboxSetup, value: value, additionalValidationMethod: additionalValidationMethod ).ToFormItem( setup: formItemSetup, label: formItemLabel )" .FormatWith( dv ) ); // radio buttons writeFormItemGetter( writer, field, "RadioButton", new CSharpParameter( "RadioButtonGroup", "group" ).ToCollection(), true, new[] { new CSharpParameter( "RadioButtonSetup", "radioButtonSetup", "null" ) }, field.NullableTypeName, new CSharpParameter[ 0 ], true, dv => "{0}.ToRadioButton( group, label, setup: radioButtonSetup, value: value, additionalValidationMethod: additionalValidationMethod ).ToFormItem( setup: formItemSetup, label: formItemLabel )" .FormatWith( dv ) ); writeFormItemGetter( writer, field, "FlowRadioButton", new CSharpParameter( "RadioButtonGroup", "group" ).ToCollection(), true, new[] { new CSharpParameter( "FlowRadioButtonSetup", "radioButtonSetup", "null" ) }, field.NullableTypeName, new CSharpParameter[ 0 ], true, dv => "{0}.ToFlowRadioButton( group, label, setup: radioButtonSetup, value: value, additionalValidationMethod: additionalValidationMethod ).ToFormItem( setup: formItemSetup, label: formItemLabel )" .FormatWith( dv ) ); } private static void writeFileCollectionFormItemGetters( TextWriter writer, ModificationField field, string valueParamTypeName ) { writeFormItemGetters( writer, field, "BlobFileCollectionManager", "FileCollection", valueParamTypeName, "-1", new CSharpParameter( "bool", "sortByName" ).ToCollection(), new CSharpParameter[ 0 ], new CSharpParameter[ 0 ], new CSharpParameter[ 0 ], "{ var control = new BlobFileCollectionManager( sortByName: sortByName ); control.LoadData( (int)v ); return control; }", "", "" ); } private static void writeDurationFormItemGetter( TextWriter writer, ModificationField field ) => writeFormItemGetter( writer, field, "DurationControl", Enumerable.Empty<CSharpParameter>(), false, new CSharpParameter( "DurationControlSetup", "controlSetup", defaultValue: "null" ).ToCollection(), "SpecifiedValue<{0}>".FormatWith( field.NullableTypeName ), field.TypeName == field.NullableTypeName ? getAllowEmptyParameter( true ).ToCollection() : Enumerable.Empty<CSharpParameter>(), true, dv => field.TypeName == field.NullableTypeName ? "{0}.ToDurationControl( setup: controlSetup, value: value, allowEmpty: allowEmpty, additionalValidationMethod: additionalValidationMethod ).ToFormItem( setup: formItemSetup, label: label )" .FormatWith( dv ) : "{0}.ToDurationControl( setup: controlSetup, value: value, additionalValidationMethod: additionalValidationMethod ).ToFormItem( setup: formItemSetup, label: label )" .FormatWith( dv ) ); private static void writeHtmlAndFileFormItemGetters( TextWriter writer, ModificationField field, string valueParamTypeName ) { writeFormItemGetter( writer, field, "Html", new CSharpParameter( "out HtmlBlockEditorModification", "mod" ).ToCollection(), false, new CSharpParameter( "HtmlBlockEditorSetup", "editorSetup", "null" ).ToCollection(), "SpecifiedValue<{0}>".FormatWith( valueParamTypeName ), new CSharpParameter[ 0 ], false, dv => "new HtmlBlockEditor( (int?)( value != null ? value.Value : {0}.Value ), id => {0}.Value = id, out m, setup: editorSetup ).ToFormItem( setup: formItemSetup, label: label )" .FormatWith( dv ), preFormItemStatements: "HtmlBlockEditorModification m = null;", postFormItemStatements: "mod = m;" ); writeFormItemGetters( writer, field, "BlobFileManager", "File", valueParamTypeName, "null", new CSharpParameter( "out System.Action", "modificationMethod" ).ToCollection(), new CSharpParameter[ 0 ], new CSharpParameter[ 0 ], new CSharpParameter( "bool", "requireUploadIfNoFile", "false" ).ToCollection(), "{ var control = new BlobFileManager( (int?)v ); mm = () => " + field.Name + " = control.ModifyData(); return control; }", "control.ValidateFormValues( validator, subject, requireUploadIfNoFile )", "", preFormItemGetterStatements: "System.Action mm = null;", postFormItemGetterStatements: "modificationMethod = mm;" ); } private static void writeBoolFormItemGetters( TextWriter writer, ModificationField field ) { if( field.TypeIs( typeof( bool ) ) ) { writeLegacyCheckBoxFormItemGetters( writer, field, "bool" ); writeNumberAsSelectListFormItemGetters( writer, field ); } if( field.TypeIs( typeof( bool? ) ) ) writeNumberAsSelectListFormItemGetters( writer, field ); } private static void writeLegacyCheckBoxFormItemGetters( TextWriter writer, ModificationField field, string valueParamTypeName ) { writeFormItemGetter( writer, field, "BlockCheckbox", new CSharpParameter[ 0 ], true, new[] { new CSharpParameter( "BlockCheckBoxSetup", "checkboxSetup", "null" ) }, valueParamTypeName + "?", new CSharpParameter[ 0 ], true, dv => "{0}.ToBlockCheckbox( label, setup: checkboxSetup, value: value, additionalValidationMethod: additionalValidationMethod ).ToFormItem( setup: formItemSetup, label: formItemLabel )" .FormatWith( dv ) ); } private static void writeDateFormItemGetters( TextWriter writer, ModificationField field ) { if( field.TypeIs( typeof( DateTime ) ) ) writeFormItemGetter( writer, field, "DateControl", Enumerable.Empty<CSharpParameter>(), false, new CSharpParameter( "DateControlSetup", "controlSetup", defaultValue: "null" ).ToCollection(), "SpecifiedValue<{0}>".FormatWith( field.NullableTypeName ), new CSharpParameter( "LocalDate?", "minValue", "null" ).ToCollection().Append( new CSharpParameter( "LocalDate?", "maxValue", "null" ) ), true, dv => "{0}.ToDateControl( setup: controlSetup, value: value, minValue: minValue, maxValue: maxValue, additionalValidationMethod: additionalValidationMethod ).ToFormItem( setup: formItemSetup, label: label )" .FormatWith( dv ) ); if( field.TypeIs( typeof( DateTime? ) ) ) writeFormItemGetter( writer, field, "DateControl", Enumerable.Empty<CSharpParameter>(), false, new CSharpParameter( "DateControlSetup", "controlSetup", defaultValue: "null" ).ToCollection(), "SpecifiedValue<{0}>".FormatWith( field.NullableTypeName ), getAllowEmptyParameter( true ) .ToCollection() .Append( new CSharpParameter( "LocalDate?", "minValue", "null" ) ) .Append( new CSharpParameter( "LocalDate?", "maxValue", "null" ) ), true, dv => "{0}.ToDateControl( setup: controlSetup, value: value, allowEmpty: allowEmpty, minValue: minValue, maxValue: maxValue, additionalValidationMethod: additionalValidationMethod ).ToFormItem( setup: formItemSetup, label: label )" .FormatWith( dv ) ); } private static void writeEnumerableFormItemGetters( TextWriter writer, ModificationField field ) { if( !field.EnumerableElementTypeName.Any() ) return; writeFormItemGetters( writer, field, "EwfCheckBoxList<" + field.EnumerableElementTypeName + ">", "CheckBoxList", field.TypeName, "null", new CSharpParameter( "IEnumerable<SelectListItem<" + field.EnumerableElementTypeName + ">>", "items" ).ToCollection(), new CSharpParameter[ 0 ], new[] { new CSharpParameter( "string", "caption", "\"\"" ), new CSharpParameter( "bool", "includeSelectAndDeselectAllButtons", "false" ), new CSharpParameter( "byte", "numberOfColumns", "1" ), new CSharpParameter( "FormAction", "action", "null" ) }, new CSharpParameter[ 0 ], "new EwfCheckBoxList<" + field.EnumerableElementTypeName + ">( items, v ?? new " + field.EnumerableElementTypeName + "[ 0 ], caption: caption, includeSelectAndDeselectAllButtons: includeSelectAndDeselectAllButtons, numberOfColumns: numberOfColumns, action: action )", "control.GetSelectedItemIdsInPostBack( postBackValues )", "" ); } private static void writeGuidFormItemGetters( TextWriter writer, ModificationField field ) { if( !field.TypeIs( typeof( Guid ) ) ) return; writeNumberAsSelectListFormItemGetters( writer, field ); } private static void writeNumberAsSelectListFormItemGetters( TextWriter writer, ModificationField field ) { var nonNullableField = field.TypeName != field.NullableTypeName; writeFormItemGetters( writer, field, "SelectList<" + field.NullableTypeName + ">", "RadioList", field.NullableTypeName, "null", new[] { new CSharpParameter( "IEnumerable<SelectListItem<" + field.NullableTypeName + ">>", "items" ) }, new CSharpParameter[ 0 ], new CSharpParameter( "bool", "useHorizontalLayout", "false" ).ToCollection() .Concat( nonNullableField ? new CSharpParameter[ 0 ] : new CSharpParameter( "string", "defaultValueItemLabel", "\"None\"" ).ToCollection() ) .Concat( new[] { new CSharpParameter( "bool", "disableSingleButtonDetection", "false" ), new CSharpParameter( "FormAction", "action", "null" ), new CSharpParameter( "bool", "autoPostBack", "false" ), new CSharpParameter( "PageModificationValue<{0}>".FormatWith( field.NullableTypeName ), "itemIdPageModificationValue", "null" ), new CSharpParameter( "IEnumerable<ListItemMatchPageModificationSetup<{0}>>".FormatWith( field.NullableTypeName ), "itemMatchPageModificationSetups", "null" ) } ), new CSharpParameter[ 0 ], "SelectList.CreateRadioList( items, v, useHorizontalLayout: useHorizontalLayout, defaultValueItemLabel: " + ( nonNullableField ? "\"\"" : "defaultValueItemLabel" ) + ", disableSingleButtonDetection: disableSingleButtonDetection, action: action, autoPostBack: autoPostBack, itemIdPageModificationValue: itemIdPageModificationValue, itemMatchPageModificationSetups: itemMatchPageModificationSetups )", "{ var selectedItemIdInPostBack = control.ValidateAndGetSelectedItemIdInPostBack( postBackValues, validator ); return " + ( nonNullableField ? "selectedItemIdInPostBack.HasValue ? selectedItemIdInPostBack.Value : default( " + field.TypeName + " )" : "selectedItemIdInPostBack" ) + "; }", "" ); writeFormItemGetters( writer, field, "SelectList<" + field.NullableTypeName + ">", "DropDown", field.NullableTypeName, "null", new CSharpParameter( "IEnumerable<SelectListItem<" + field.NullableTypeName + ">>", "items" ).ToCollection() .Concat( nonNullableField ? new CSharpParameter[ 0 ] : new CSharpParameter( "string", "defaultValueItemLabel" ).ToCollection() ), new CSharpParameter[ 0 ], new CSharpParameter( "Unit?", "width", "null" ).ToCollection() .Concat( nonNullableField ? new CSharpParameter[ 0 ] : new CSharpParameter( "bool", "placeholderIsValid", "true" ).ToCollection() ) .Concat( new[] { new CSharpParameter( "string", "placeholderText", "\"Please select\"" ), new CSharpParameter( "FormAction", "action", "null" ), new CSharpParameter( "bool", "autoPostBack", "false" ), new CSharpParameter( "PageModificationValue<{0}>".FormatWith( field.NullableTypeName ), "itemIdPageModificationValue", "null" ), new CSharpParameter( "IEnumerable<ListItemMatchPageModificationSetup<{0}>>".FormatWith( field.NullableTypeName ), "itemMatchPageModificationSetups", "null" ) } ), new CSharpParameter[ 0 ], "SelectList.CreateDropDown( items, v, width: width, defaultValueItemLabel: " + ( nonNullableField ? "\"\"" : "defaultValueItemLabel" ) + ", placeholderIsValid: " + ( nonNullableField ? "false" : "placeholderIsValid" ) + ", placeholderText: placeholderText, action: action, autoPostBack: autoPostBack, itemIdPageModificationValue: itemIdPageModificationValue, itemMatchPageModificationSetups: itemMatchPageModificationSetups )", "{ var selectedItemIdInPostBack = control.ValidateAndGetSelectedItemIdInPostBack( postBackValues, validator ); return " + ( nonNullableField ? "selectedItemIdInPostBack.HasValue ? selectedItemIdInPostBack.Value : default( " + field.TypeName + " )" : "selectedItemIdInPostBack" ) + "; }", "" ); } private static void writeFormItemGetter( TextWriter writer, ModificationField field, string controlTypeForName, IEnumerable<CSharpParameter> requiredParams, bool controlIsLabeled, IEnumerable<CSharpParameter> preValueOptionalParams, string valueParamTypeName, IEnumerable<CSharpParameter> postValueOptionalParams, bool includeAdditionalValidationMethodParam, Func<string, string> formItemExpressionGetter, string preFormItemStatements = "", string postFormItemStatements = "", IEnumerable<string> additionalSummarySentences = null ) { CodeGenerationStatics.AddSummaryDocComment( writer, getFormItemGetterSummary( field, controlTypeForName, additionalSummarySentences ?? new string[ 0 ] ) ); if( includeAdditionalValidationMethodParam ) CodeGenerationStatics.AddParamDocComment( writer, "additionalValidationMethod", "A method that takes the form control’s validator and performs additional validation." ); var parameters = new List<CSharpParameter>(); parameters.AddRange( requiredParams ); parameters.Add( new CSharpParameter( "FormItemSetup", "formItemSetup", "null" ) ); parameters.Add( new CSharpParameter( "IReadOnlyCollection<PhrasingComponent>", "label", "null" ) ); if( controlIsLabeled ) parameters.Add( new CSharpParameter( "IReadOnlyCollection<PhrasingComponent>", "formItemLabel", "null" ) ); parameters.AddRange( preValueOptionalParams ); parameters.Add( new CSharpParameter( valueParamTypeName, "value", "null" ) ); parameters.AddRange( postValueOptionalParams ); if( includeAdditionalValidationMethodParam ) parameters.Add( new CSharpParameter( "System.Action<Validator>", "additionalValidationMethod", "null" ) ); writer.WriteLine( "public FormItem " + EwlStatics.GetCSharpIdentifier( "Get" + field.PascalCasedName + controlTypeForName + "FormItem" ) + "( " + parameters.Select( i => i.MethodSignatureDeclaration ).GetCommaDelimitedStringFromCollection() + " ) {" ); writer.WriteLine( "label = label ?? \"{0}\".ToComponents();".FormatWith( getDefaultLabel( field ) ) ); writer.WriteLine( StringTools.ConcatenateWithDelimiter( Environment.NewLine, preFormItemStatements, "var formItem = {0};".FormatWith( formItemExpressionGetter( EwlStatics.GetCSharpIdentifier( field.PrivateFieldName ) ) ), postFormItemStatements, "return formItem;" ) ); writer.WriteLine( "}" ); } private static void writeFormItemGetters( TextWriter writer, ModificationField field, string controlType, string controlTypeForName, string valueParamTypeName, string valueParamDefaultValue, IEnumerable<CSharpParameter> requiredControlParams, IEnumerable<CSharpParameter> requiredValidationParams, IEnumerable<CSharpParameter> optionalControlParams, IEnumerable<CSharpParameter> optionalValidationParams, string controlGetterExpressionOrBlock, string validationMethodExpressionOrBlock, string labelOverrideNullCoalescingExpression, string preFormItemGetterStatements = "", string postFormItemGetterStatements = "", IEnumerable<string> additionalSummarySentences = null ) { writeFormItemGetterWithoutValueParams( writer, controlType, field, controlTypeForName, validationMethodExpressionOrBlock.Any(), requiredControlParams, requiredValidationParams, optionalControlParams, optionalValidationParams, additionalSummarySentences ?? new string[ 0 ] ); writeFormItemGetterWithValueParams( writer, controlType, field, controlTypeForName, valueParamTypeName, valueParamDefaultValue, requiredControlParams, requiredValidationParams, optionalControlParams, optionalValidationParams, preFormItemGetterStatements, controlGetterExpressionOrBlock, validationMethodExpressionOrBlock, labelOverrideNullCoalescingExpression, postFormItemGetterStatements, additionalSummarySentences ?? new string[ 0 ] ); } private static void writeFormItemGetterWithoutValueParams( TextWriter writer, string controlType, ModificationField field, string controlTypeForName, bool includeValidationParams, IEnumerable<CSharpParameter> requiredControlParams, IEnumerable<CSharpParameter> requiredValidationParams, IEnumerable<CSharpParameter> optionalControlParams, IEnumerable<CSharpParameter> optionalValidationParams, IEnumerable<string> additionalSummarySentences ) { // NOTE: The "out" parameter logic is a hack. We need to improve CSharpParameter. var body = "return " + EwlStatics.GetCSharpIdentifier( "Get" + field.PascalCasedName + controlTypeForName + "FormItem" ) + "( false, " + requiredControlParams.Concat( requiredValidationParams ) .Select( i => ( i.MethodSignatureDeclaration.StartsWith( "out " ) ? "out " : "" ) + i.Name ) .GetCommaDelimitedStringFromCollection() .AppendDelimiter( ", " ) + "formItemSetup: formItemSetup, labelAndSubject: labelAndSubject, labelOverride: labelOverride" + optionalControlParams.Select( i => i.Name + ": " + i.Name ).GetCommaDelimitedStringFromCollection().PrependDelimiter( ", " ) + ( includeValidationParams ? ", validationPredicate: validationPredicate" : "" ) + optionalValidationParams.Select( i => i.Name + ": " + i.Name ).GetCommaDelimitedStringFromCollection().PrependDelimiter( ", " ) + ( includeValidationParams ? ", validationErrorNotifier: validationErrorNotifier, additionalValidationMethod: additionalValidationMethod" : "" ) + " );"; writeFormItemGetter( writer, controlType, field, controlTypeForName, "", requiredControlParams, requiredValidationParams, "", optionalControlParams, includeValidationParams, optionalValidationParams, body, additionalSummarySentences ); } private static void writeFormItemGetterWithValueParams( TextWriter writer, string controlType, ModificationField field, string controlTypeForName, string valueParamTypeName, string valueParamDefaultValue, IEnumerable<CSharpParameter> requiredControlParams, IEnumerable<CSharpParameter> requiredValidationParams, IEnumerable<CSharpParameter> optionalControlParams, IEnumerable<CSharpParameter> optionalValidationParams, string preFormItemGetterStatements, string controlGetterExpressionOrBlock, string validationMethodExpressionOrBlock, string labelOverrideNullCoalescingExpression, string postFormItemGetterStatements, IEnumerable<string> additionalSummarySentences ) { var validationMethod = "( control, postBackValues, subject, validator ) => " + validationMethodExpressionOrBlock; var formItemGetterStatement = "var formItem = " + EwlStatics.GetCSharpIdentifier( "Get" + field.PascalCasedName + "FormItem" ) + "( useValueParameter, " + "( {0} ) => {1}".FormatWith( controlType.Any() ? "v, ls" : "v, ls, vs", controlGetterExpressionOrBlock ) + ( validationMethodExpressionOrBlock.Any() ? ", " + validationMethod : "" ) + ", setup: formItemSetup, labelAndSubject: labelAndSubject, labelOverride: labelOverride" + labelOverrideNullCoalescingExpression.PrependDelimiter( " ?? " ) + ", value: value" + ( validationMethodExpressionOrBlock.Any() ? ", validationPredicate: validationPredicate, validationErrorNotifier: validationErrorNotifier, additionalValidationMethod: additionalValidationMethod" : "" ) + " );"; writeFormItemGetter( writer, controlType, field, controlTypeForName, valueParamTypeName, requiredControlParams, requiredValidationParams, valueParamDefaultValue, optionalControlParams, validationMethodExpressionOrBlock.Any(), optionalValidationParams, StringTools.ConcatenateWithDelimiter( Environment.NewLine, preFormItemGetterStatements, formItemGetterStatement, postFormItemGetterStatements, "return formItem;" ), additionalSummarySentences ); } private static void writeFormItemGetter( TextWriter writer, string controlType, ModificationField field, string controlTypeForName, string valueParamTypeName, IEnumerable<CSharpParameter> requiredControlParams, IEnumerable<CSharpParameter> requiredValidationParams, string valueParamDefaultValue, IEnumerable<CSharpParameter> optionalControlParams, bool includeValidationParams, IEnumerable<CSharpParameter> optionalValidationParams, string body, IEnumerable<string> additionalSummarySentences ) { CodeGenerationStatics.AddSummaryDocComment( writer, getFormItemGetterSummary( field, controlTypeForName, additionalSummarySentences ) ); CodeGenerationStatics.AddParamDocComment( writer, "additionalValidationMethod", "Passes the labelAndSubject and a validator to the function." ); var parameters = new List<CSharpParameter>(); if( valueParamTypeName.Length > 0 ) parameters.Add( new CSharpParameter( "bool", "useValueParameter" ) ); parameters.AddRange( requiredControlParams ); parameters.AddRange( requiredValidationParams ); parameters.Add( new CSharpParameter( "FormItemSetup", "formItemSetup", "null" ) ); parameters.Add( new CSharpParameter( "string", "labelAndSubject", "\"" + getDefaultLabel( field ) + "\"" ) ); parameters.Add( new CSharpParameter( "FormItemLabel", "labelOverride", "null" ) ); if( valueParamTypeName.Length > 0 ) parameters.Add( new CSharpParameter( valueParamTypeName, "value", valueParamDefaultValue ) ); parameters.AddRange( optionalControlParams ); if( includeValidationParams ) parameters.Add( new CSharpParameter( "System.Func<bool>", "validationPredicate", "null" ) ); parameters.AddRange( optionalValidationParams ); if( includeValidationParams ) { parameters.Add( new CSharpParameter( "System.Action", "validationErrorNotifier", "null" ) ); parameters.Add( new CSharpParameter( "System.Action<string,Validator>", "additionalValidationMethod", "null" ) ); } writer.WriteLine( "public FormItem" + controlType.Surround( "<", ">" ) + " " + EwlStatics.GetCSharpIdentifier( "Get" + field.PascalCasedName + controlTypeForName + "FormItem" ) + "( " + parameters.Select( i => i.MethodSignatureDeclaration ).GetCommaDelimitedStringFromCollection() + " ) {" ); writer.WriteLine( body ); writer.WriteLine( "}" ); } private static CSharpParameter getAllowEmptyParameter( bool isOptional ) { return new CSharpParameter( "bool", "allowEmpty", isOptional ? "true" : "" ); } private static void writeGenericGetterWithoutValueParams( TextWriter writer, ModificationField field, bool? includeValidationMethodReturnValue ) { var controlGetter = "( v, ls ) => controlGetter( v" + ( field.TypeName != field.NullableTypeName ? ".Value" : "" ) + ", ls )"; var body = "return " + EwlStatics.GetCSharpIdentifier( "Get" + field.PascalCasedName + "FormItem" ) + "( false, " + controlGetter + ( includeValidationMethodReturnValue.HasValue ? ", validationMethod" : "" ) + ", setup: setup, labelAndSubject: labelAndSubject, labelOverride: labelOverride" + ( includeValidationMethodReturnValue.HasValue ? ", validationPredicate: validationPredicate, validationErrorNotifier: validationErrorNotifier, additionalValidationMethod: additionalValidationMethod" : "" ) + " );"; writeGenericGetter( writer, field, false, includeValidationMethodReturnValue, body ); } private static void writeGenericGetterWithValueParams( TextWriter writer, ModificationField field, bool? includeValidationMethodReturnValue ) { var control = "useValueParameter ? controlGetter( value, labelAndSubject ) : controlGetter( " + EwlStatics.GetCSharpIdentifier( field.Name ) + ", labelAndSubject )"; var body = "return FormItem.Create( labelOverride ?? labelAndSubject, " + control + ", setup: setup" + ( includeValidationMethodReturnValue.HasValue ? ", validationGetter: " + getValidationGetter( field, includeValidationMethodReturnValue.Value ) : "" ) + " );"; writeGenericGetter( writer, field, true, includeValidationMethodReturnValue, body ); } private static string getValidationGetter( ModificationField field, bool includeValidationMethodReturnValue ) { var fieldPropertyName = EwlStatics.GetCSharpIdentifier( field.Name ); var statements = new[] { "if( validationPredicate != null && !validationPredicate() ) return;", ( includeValidationMethodReturnValue ? fieldPropertyName + " = " : "" ) + "validationMethod( control, postBackValues, labelAndSubject, validator );", "if( validator.ErrorsOccurred && validationErrorNotifier != null ) validationErrorNotifier();", "if( !validator.ErrorsOccurred && additionalValidationMethod != null ) additionalValidationMethod( labelAndSubject, validator );" }; return "control => new EwfValidation( ( postBackValues, validator ) => { " + StringTools.ConcatenateWithDelimiter( " ", statements ) + " } )"; } private static void writeGenericGetter( TextWriter writer, ModificationField field ) { CodeGenerationStatics.AddSummaryDocComment( writer, getFormItemGetterSummary( field, "", new string[ 0 ] ) ); var parameters = new List<CSharpParameter>(); parameters.Add( new CSharpParameter( "System.Func<{0},IEnumerable<FlowComponent>>".FormatWith( field.NullableTypeName ), "contentGetter" ) ); parameters.Add( new CSharpParameter( "FormItemSetup", "setup", "null" ) ); parameters.Add( new CSharpParameter( "IReadOnlyCollection<PhrasingComponent>", "label", "null" ) ); parameters.Add( new CSharpParameter( field.TypeIs( typeof( string ) ) ? field.NullableTypeName : "SpecifiedValue<{0}>".FormatWith( field.NullableTypeName ), "value", "null" ) ); parameters.Add( new CSharpParameter( "System.Func<System.Action<{0}>,EwfValidation>".FormatWith( field.TypeName ), "validationGetter", "null" ) ); writer.WriteLine( "public FormItem " + EwlStatics.GetCSharpIdentifier( "Get" + field.PascalCasedName + "FormItem" ) + "( " + parameters.Select( i => i.MethodSignatureDeclaration ).GetCommaDelimitedStringFromCollection() + " ) {" ); writer.WriteLine( "label = label ?? \"{0}\".ToComponents();".FormatWith( getDefaultLabel( field ) ) ); writer.WriteLine( "return {0}.ToFormItem( setup: setup, label: label, validation: {1} );".FormatWith( "contentGetter( {0} )".FormatWith( field.TypeIs( typeof( string ) ) ? "value ?? {0}".FormatWith( EwlStatics.GetCSharpIdentifier( field.Name ) ) : "value != null ? value.Value : {0}".FormatWith( EwlStatics.GetCSharpIdentifier( field.Name ) ) ), "validationGetter?.Invoke( v => {0} = v )".FormatWith( EwlStatics.GetCSharpIdentifier( field.Name ) ) ) ); writer.WriteLine( "}" ); } private static void writeGenericGetter( TextWriter writer, ModificationField field, bool includeValueParams, bool? includeValidationMethodReturnValue, string body ) { CodeGenerationStatics.AddSummaryDocComment( writer, getFormItemGetterSummary( field, "", new string[ 0 ] ) ); CodeGenerationStatics.AddParamDocComment( writer, "additionalValidationMethod", "Passes the labelAndSubject and a validator to the function." ); var parameters = new List<CSharpParameter>(); if( includeValueParams ) parameters.Add( new CSharpParameter( "bool", "useValueParameter" ) ); parameters.Add( new CSharpParameter( "System.Func<" + ( includeValueParams ? field.NullableTypeName : field.TypeName ) + ",string,ControlType>", "controlGetter" ) ); if( includeValidationMethodReturnValue.HasValue ) parameters.Add( includeValidationMethodReturnValue.Value ? new CSharpParameter( "System.Func<ControlType,PostBackValueDictionary,string,Validator," + field.TypeName + ">", "validationMethod" ) : new CSharpParameter( "System.Action<ControlType,PostBackValueDictionary,string,Validator>", "validationMethod" ) ); parameters.Add( new CSharpParameter( "FormItemSetup", "setup", "null" ) ); parameters.Add( new CSharpParameter( "string", "labelAndSubject", "\"" + getDefaultLabel( field ) + "\"" ) ); parameters.Add( new CSharpParameter( "FormItemLabel", "labelOverride", "null" ) ); if( includeValueParams ) parameters.Add( new CSharpParameter( field.NullableTypeName, "value", field.TypeIs( typeof( string ) ) ? "\"\"" : "null" ) ); if( includeValidationMethodReturnValue.HasValue ) { parameters.Add( new CSharpParameter( "System.Func<bool>", "validationPredicate", "null" ) ); parameters.Add( new CSharpParameter( "System.Action", "validationErrorNotifier", "null" ) ); parameters.Add( new CSharpParameter( "System.Action<string,Validator>", "additionalValidationMethod", "null" ) ); } writer.WriteLine( "public FormItem<ControlType> " + EwlStatics.GetCSharpIdentifier( "Get" + field.PascalCasedName + "FormItem" ) + "<ControlType>( " + parameters.Select( i => i.MethodSignatureDeclaration ).GetCommaDelimitedStringFromCollection() + " ) where ControlType: Control {" ); writer.WriteLine( body ); writer.WriteLine( "}" ); } private static string getFormItemGetterSummary( ModificationField field, string controlType, IEnumerable<string> additionalSentences ) { var sentences = new[] { "Creates a " + field.Name + controlType.PrependDelimiter( " " ) + " form item, which includes a label, a page component, and a validation.", "The default label is “{0}”.".FormatWith( getDefaultLabel( field ) ), controlType.Any() ? "" : "This is a generic form-item getter; use it only if there is no specific getter for the control type that you need.", "You almost certainly should not call this method from a deferred block of code since this could cause validations to be added to the data modification in the wrong order." }; return StringTools.ConcatenateWithDelimiter( " ", sentences.Concat( additionalSentences ).ToArray() ); } private static string getDefaultLabel( ModificationField field ) { var result = field.PascalCasedName.CamelToEnglish(); if( result.ToLower().EndsWith( " id" ) ) result = result.Substring( 0, result.Length - 3 ); return result; } } }