Debian Codegraph @

lib/HTML/FormHandler/Manual/Fields.pod

raw
1 package HTML::FormHandler::Manual::Fields;
2 # ABSTRACT: brief documentation of available fields
3
4 __END__
5
6 =pod
7
8 =encoding UTF-8
9
10 =head1 NAME
11
12 HTML::FormHandler::Manual::Fields - brief documentation of available fields
13
14 =head1 VERSION
15
16 version 0.410002
17
18 =head1 SYNOPSIS
19
20 L<Manual Index|HTML::FormHandler::Manual>
21
22 See also L<HTML::FormHandler::Field> for a description of the base
23 field attributes.
24
25 The inheritance hierarchy of HTML::FormHandler Fields
26
27 Text
28 Money
29 Password
30 Hidden
31 Integer
32 PosInteger
33 Float
34 Date
35 DateMDY
36 Email
37 TextCSV
38 TextArea
39 Select
40 Multiple
41 SelectCSV
42 BoolSelect
43 IntRange
44 Hour
45 Minute
46 MonthDay
47 Month
48 Second
49 Year
50 MonthName
51 Weekday
52 Checkbox
53 Boolean
54 Compound
55 Repeatable
56 Duration
57 DateTime
58 NoValue
59 Submit
60 Reset
61 Button
62 Display
63 AddElement
64 RmElement
65 NonEditable
66 PrimaryKey
67 Upload
68 File
69
70 =head1 DESCRIPTION
71
72 A form's fields are created from the 'has_field' and 'field_list' definitions.
73 FormHandler processes the field lists and creates an array of
74 L<HTML::FormHandler::Field> objects. The "type" of a field
75 determines which field class to use. The field class determines which
76 attributes are valid for a particular field. A number of field classes are
77 provided by FormHandler. You can customize the validation in your form on a
78 per field basis, but validation that will be used for more than one field
79 might be more easily handled in a custom field class.
80
81 Fields are accessed with C<< form->field('name') >>.
82 Field errors are in C<< $field->errors >>.
83
84 If the 'field_name_space' is not set, fields will be loaded from the
85 HTML::FormHandler::Field name space. If you provide a 'field_name_space'
86 it will be searched before FormHandler. If you want to explicitly list the
87 field's package, prefix it with a plus sign. The field_name_space plus the
88 default name spaces 'HTML::FormHandler::Field' and 'HTML::FormHandlerX::Field'
89 will be searched for fields.
90
91 has '+field_name_space' => ( default => 'MyApp::Form::Field' );
92 has_field 'name' => ( type => 'Text' ); # HTML::FormHandler::Field::Text
93 has_field 'name' => ( type => '+My::FieldType' ); # My::Fieldtype
94 has_field 'foo' => ( type => '+Foo' ); # MyApp::Form::Field::Foo
95 or
96 has_field 'foo' => ( type => 'Foo' ); # MyApp::Form::Field::Foo
97
98 The most basic type is "Text", which is usually a 'text' HTML element and
99 a string data type. (If the type of a field is not specified, it will be set to
100 'Text'.) A "Select" field type is a HTML select element, and validates against
101 the list of values provided in the 'options'. A "Multiple" type is like "Select"
102 but it allows selecting more than one value at a time.
103
104 Many field classes contain only a list of constraints and transformations
105 to apply. Some use the 'validate' method, which is called after the actions
106 are applied. Some build a custom select list using 'build_options'.
107
108 There are two rough categories of Field classes: those that do extra processing
109 and those that are simple validators. The 'Compound', 'Repeatable', and
110 'Select' fields are fields that are functional.
111
112 =head1 Field names
113
114 The standard way to use FormHandler is with field names that match your
115 database accessors. If you want to prepend the HTML field names with a
116 name plus dot, you can set the form 'name' and use the 'html_prefix'
117 flag. "$name." will be stripped from the beginning of the HTML fields
118 before processing by HFH, and will be added back in 'fif'. The field's
119 'html_name' convenience attribute will return this name for use in templates.
120
121 If you want the FormHandler field name to be different than the
122 database accessor, set 'accessor' on your fields. (It defaults to the field
123 name.) You could then use any name that you want for your field.
124
125 There are a number of name-related field attributes. The 'name' is
126 the name used to identify this particular field in this fields array.
127 The 'full_name' includes the names of all parents of this field,
128 like 'address.street.streetname'. The 'html_name' is the same as the
129 'full_name' unless you have set the 'html_prefix' flag, in which case
130 it includes the form name: 'myform.address.street.streetname'.
131
132 To retrieve a field by name, you can use either the full_name or a
133 chain: C<< $form->field('address')->field('street')->field('streetname') >>
134 or: C<< $form->field('address.street.streetname') >>.
135
136 =head1 Creating custom fields
137
138 Subclass a custom field from L<HTML::FormHandler::Field>, or one of the
139 existing subclasses. Almost everything that is done in a custom field
140 class can also be done in a form. The advantage of a field class
141 is that it can simplify declaration of often-repeated sets of attributes.
142
143 The simplest subclasses contain only a 'validate' routine or an 'apply' attribute,
144 which is called by the base Field class from 'process'. Look at
145 L<HTML::FormHandler::Field::Email>, for example.
146
147 If the field's value will be an object instead of a simple scalar, such
148 as a DateTime, and you want to use the transformed value to fill in the
149 form, then you will also need a deflation or field class 'deflate' method
150 to reformat the object into a form suitable for an HTML form field.
151 See L<HTML::FormHandler::Manual::InflationDeflation> for more info.
152
153 Some custom fields might only require setting certain attributes to
154 defaults, such as the L<HTML::FormHandler::Field::Hour> field, which
155 set 'range_start' to 0 and 'range_end' to 23. A 'select' field might
156 override the 'build_options' builder for the 'options' array, like
157 L<HTML::FormHandler::Field::IntRange>. A field may add additional
158 attributes, such as 'label_format' in L<HTML::FormHandler::Field::IntRange>,
159 or set the 'required' message.
160
161 An alternative to new field classes for many field validations might
162 be roles with collections of validations.
163
164 =head1 Other field packages
165
166 Some custom fields are supplied as CPAN packages, in the HTML::FormHandlerX
167 name space.
168
169 L<reCAPTCHA|HTML::FormHandlerX::Field::reCAPTCHA>
170
171 L<DateTimeNatural|HTML::FormHandlerX::Field::DateTimeNatural>
172
173 L<URI::HTTP|HTML::FormHandlerX::Field::URI::HTTP>
174
175 =head1 Fields supplied by FormHandler
176
177 =head2 Basic Fields
178
179 Although there are a lot of fields provided (probably too many) a lot of them
180 are "convenience" fields or "name" fields, where the main benefit is
181 that the field type is a name that gives the main purpose of the field.
182 Most of these fields could be replaced by a basic field with a bit of validation
183 or some select options. A few of the fields are special purpose fields
184 that won't be used very often.
185
186 The fields in this section are the basic fields, the commonly used fields
187 that will be most often used in a form.
188
189 =head3 Text
190
191 A string data type that will be formatted as an HTML text field. Has 'minlength'
192 and 'maxlength' attributes.
193
194 L<HTML::FormHandler::Field::Text>
195
196 =head3 Select
197
198 A field formatted as a select element.
199
200 L<HTML::FormHandler::Field::Select>
201
202 This also handles radio buttons and checkboxes, by means of widgets.
203
204 =head3 Checkbox
205
206 A field formatted as a checkbox. If not in params, will be forced to
207 'false' value by 'input_without_param' attribute (0 by default).
208
209 L<HTML::FormHandler::Field::Checkbox>
210
211 =head3 Hidden
212
213 A hidden field.
214
215 L<HTML::FormHandler::Field::Hidden>
216
217 =head3 Password
218
219 A password field. The value is not re-displayed.
220
221 L<HTML::FormHandler::Field::Password>
222
223 =head3 TextArea
224
225 A textarea field. Has 'cols' and 'rows' attributes.
226
227 L<HTML::FormHandler::Field::TextArea>
228
229 =head3 Upload
230
231 A file upload field that takes a filehandle or a Catalyst upload object (an
232 object with a 'size' method).
233
234 L<HTML::FormHandler::Field::Upload>
235
236 =head3 Submit
237
238 A submit field.
239
240 L<HTML::FormHandler::Field::Submit>
241
242 =head3 Reset
243
244 A reset field.
245
246 L<HTML::FormHandler::Field::Reset>
247
248 =head2 Complex Fields (Compound and Repeatable)
249
250 These fields are complex fields which contain a fair amount of
251 special code. They do not map to a single HTML element; they contain
252 multiple subfields.
253
254 =head3 Compound
255
256 A compound field is a field that has sub-fields. Compound fields can be
257 created in two ways: 1) using a field class, 2) by declaration.
258
259 To create a compound field class, you must extend
260 L<HTML::FormHandler::Field::Compound> and use L<HTML::FormHandler::Moose> to
261 allow declaring fields:
262
263 package MyApp::Field::Duration;
264
265 use HTML::FormHandler::Moose;
266 extends 'HTML::FormHandler::Field::Compound';
267
268 has_field 'month' => (type => 'Integer');
269 has_field 'day' => ( type => 'Integer' );
270 has_field 'minutes' => ( type => 'Integer' );
271
272 Then in the form:
273
274 has_field 'my_duration' => ( type => '+Duration' );
275
276 To create a compound field by declaration, declare the containing
277 compound field and subfields, prefixing the subfield names
278 with the name of the containing compound field plus a dot:
279
280 package MyApp::Form;
281
282 use HTML::FormHandler::Moose;
283 extends 'HTML::FormHandler';
284
285 has_field 'duration' => ( type => 'Compound' );
286 has_field 'duration.month' => ( type => 'Integer' );
287 has_field 'duration.day' => ( type => 'Integer' );
288 has_field 'duration.year' => ( type => 'Integer' );
289
290 In an HTML form the name of the field must be the complete name
291 with dots. The 'html_name' field attribute can be used to get
292 this name, C<< $field->html_name >>.
293
294 A compound field can be used for a database relation that will have only
295 one row (belongs_to or has_one). If the relation has a compound primary
296 key, you may need to provide the primary key columns, either through
297 hidden fields or by setting them in the C<< $form->value >> hash before
298 'update_model' is called.
299
300 See also L<HTML::FormHandler::Field::Compound>.
301
302 =head3 Repeatable
303
304 Repeatable fields are used for arrays of compound fields.
305
306 has_field 'addresses' => ( type => 'Repeatable' );
307 has_field 'addresses.address_id' => ( type => 'PrimaryKey' );
308 has_field 'addresses.street';
309 has_field 'addresses.city';
310 has_field 'addresses.country' => ( type => 'Select' );
311
312 The arrays will be built from arrays passed in the params, or from
313 related ('has_many') rows in the database.
314
315 It is also used for arrays of single fields using the 'contains' keyword:
316
317 has_field 'tags' => ( type => 'Repeatable' );
318 has_field 'tags.contains' => ( type => '+Tag' );
319
320 See L<HTML::FormHandler::Field::Repeatable> for more information.
321
322 =head2 Text Fields
323
324 Fields subclassed from the Text field.
325
326 =head3 Text
327
328 Text field.
329
330 L<HTML::FormHandler::Field::Text>
331
332 =head3 Money
333
334 Positive or negative real value, formatted to two decimal places.
335
336 L<HTML::FormHandler::Field::Money>
337
338 =head3 Date
339
340 Date field that can be used by jQuery datepicker plugin.
341
342 L<HTML::FormHandler::Field::Date>
343
344 =head3 DateMDY
345
346 A subclass of 'Date' with the "%m/%d/%Y" format.
347
348 L<HTML::FormHandler::Field::DateMDY>
349
350 =head3 Email
351
352 Uses Email::Valid for validation.
353
354 L<HTML::FormHandler::Field::Email>
355
356 =head3 Integer
357
358 Positive and negative integers. Can use range_start and range_end.
359
360 L<HTML::FormHandler::Field::Integer>
361
362 =head3 PosInteger
363
364 A positive integer field.
365
366 L<HTML::FormHandler::Field::PosInteger>
367
368 =head3 Float
369
370 Float field that allows you to set size, precision, decimal_symbol, and
371 decimal_symbol_for_db.
372
373 L<HTML::FormHandler::Field::Float>
374
375 =head3 TextCSV
376
377 A text field that takes multiple values from a database and converts
378 them to comma-separated values. This is intended for javascript fields
379 that require that, such as 'select2'. This is the only 'multiple' text
380 field. This text field would be a select-type field for the user.
381
382 L<HTML::FormHandler::Field::TextCSV>
383
384 =head2 Compound Fields
385
386 Fields subclassed from 'Compound'.
387
388 =head3 Compound
389
390 L<HTML::FormHandler::Field::Compound>
391
392 =head3 Repeatable
393
394 L<HTML::FormHandler::Field::Repeatable>
395
396 =head3 Duration
397
398 Compound field with possible subfields: years, months, weeks, days, hours,
399 minutes, seconds, nanoseconds.
400
401 L<HTML::FormHandler::Field::Duration>
402
403 =head3 DateTime
404
405 A compound field that requires you to provide the subfields that
406 you want. (month/day/year/hour/minutes)
407
408 L<HTML::FormHandler::Field::DateTime>
409
410 =head2 Checkbox Fields
411
412 Fields that inherit from 'Checkbox'.
413
414 =head3 Checkbox
415
416 L<HTML::FormHandler::Field::Checkbox>
417
418 =head3 Boolean
419
420 Checkbox that return 1 or 0.
421
422 L<HTML::FormHandler::Field::Boolean>
423
424 =head2 Select Fields
425
426 Fields that inherit from 'Select'.
427
428 =head3 Select
429
430 L<HTML::FormHandler::Field::Select>
431
432 =head3 Multiple
433
434 Multiple select. Also sorts the selected options to the top of
435 the select list.
436
437 L<HTML::FormHandler::Field::Multiple>
438
439 =head2 SelectCSV
440
441 A multiple select field for comma-separated values in the database.
442 It expects database values like: '1,5,7'. The string will be inflated
443 into an arrayref for validation and form filling, and will be deflated
444 into a comma-separated string in the output value.
445
446 L<HTML::FormHandler::Field::SelectCSV>
447
448 =head3 BoolSelect
449
450 A field with three possible values: empty/0/1.
451
452 L<HTML::FormHandler::Field::BoolSelect>
453
454 =head3 Hour
455
456 Integer select range field from 0-23.
457
458 L<HTML::FormHandler::Field::Hour>
459
460 =head3 Second
461
462 Select field with range from 0-59.
463
464 L<HTML::FormHandler::Field::Second>
465
466 =head3 IntRange
467
468 An integer select field. Can set label format with 'label_format'.
469
470 L<HTML::FormHandler::Field::IntRange>
471
472 =head3 Month
473
474 Select field with range from 1 - 12.
475
476 L<HTML::FormHandler::Field::Month>
477
478 =head3 MonthDay
479
480 Select field with range from 1 - 31.
481
482 L<HTML::FormHandler::Field::MonthDay>
483
484 =head3 MonthName
485
486 Select field with month name labels, value 1-12.
487
488 L<HTML::FormHandler::Field::MonthName>
489
490 =head3 Minute
491
492 Select field with range from 0-59.
493
494 L<HTML::FormHandler::Field::Minute>
495
496 =head3 Weekday
497
498 A select field where the labels are the names of the week, and the
499 values are 0-6.
500
501 L<HTML::FormHandler::Field::Weekday>
502
503 =head3 Year
504
505 Select field providing year list 5 years back and 10 years forward.
506
507 L<HTML::FormHandler::Field::Year>
508
509 =head2 NoValue fields
510
511 Fields that inherit from 'NoValue'. None of these fields will
512 provide a 'value' in the C<< $form->value >> hashref.
513
514 =head3 NoValue
515
516 Base class for fields that don't produce a 'value'.
517
518 L<HTML::FormHandler::Field::NoValue>
519
520 =head3 Submit
521
522 L<HTML::FormHandler::Field::Submit>
523
524 =head3 Reset
525
526 L<HTML::FormHandler::Field::Reset>
527
528 =head3 Button
529
530 Button field that is rendered by the Button widget.
531
532 L<HTML::FormHandler::Field::Button>
533
534 =head3 Display
535
536 Non-data field used for inserting HTML into the form. Probably
537 now better handled by a Block or a rendering tag.
538
539 L<HTML::FormHandler::Field::Display>
540
541 =head3 AddElement
542
543 Example field for adding a repeatable element.
544
545 L<HTML::FormHandler::Field::AddElement>
546
547 =head3 RmElement
548
549 Example field for removing a repeatable element
550
551 L<HTML::FormHandler::Field::RmElement>
552
553 =head3 NonEditable
554
555 For Bootstrap-style non-editable fields.
556
557 =head2 TextArea fields
558
559 Fields that inherit from 'TextArea'.
560
561 =head3 TextArea
562
563 L<HTML::FormHandler::Field::TextArea>
564
565 =head2 Password fields
566
567 =head3 Password
568
569 Password field. Sets 'noupdate' flag if empty and not required.
570
571 L<HTML::FormHandler::Field::Password>
572
573 =head3 PasswordConf
574
575 Password confirmation field.
576
577 L<HTML::FormHandler::Field::PasswordConf>
578
579 =head2 Other fields
580
581 These fields inherit just from 'Field'.
582
583 =head3 File
584
585 A file field that does no processing. Most people probably want to
586 use 'Upload' instead.
587
588 L<HTML::FormHandler::Field::File>
589
590 =head3 PrimaryKey
591
592 Hidden field that provides the primary key for Repeatable fields.
593
594 L<HTML::FormHandler::Field::PrimaryKey>
595
596 =head3 Captcha
597
598 A Captcha field using GD::SecurityImage. Requires the use of the
599 L<HTML::FormHandler::TraitFor::Captcha> role, or similar code.
600
601 L<HTML::FormHandler::Field::Captcha>
602
603 =head1 AUTHOR
604
605 FormHandler Contributors - see HTML::FormHandler
606
607 =head1 COPYRIGHT AND LICENSE
608
609 This software is copyright (c) 2017 by Gerda Shank.
610
611 This is free software; you can redistribute it and/or modify it under
612 the same terms as the Perl 5 programming language system itself.
613
614 =cut