Debian Codegraph @

lib/HTML/FormHandler/Manual/Rendering.pod

raw
1 package HTML::FormHandler::Manual::Rendering;
2 # ABSTRACT: how to render with FormHandler
3
4 __END__
5
6 =pod
7
8 =encoding UTF-8
9
10 =head1 NAME
11
12 HTML::FormHandler::Manual::Rendering - how to render with FormHandler
13
14 =head1 VERSION
15
16 version 0.410002
17
18 =head1 SYNOPSIS
19
20 L<Manual Index|HTML::FormHandler::Manual>
21
22 Rendering can be done in many different ways, from forms rendered entirely in
23 templates with no information from FormHandler (except possibly the fill-in-the-form
24 values) to forms that are completely rendered by FormHandler.
25
26 =head1 DESCRIPTION
27
28 For most situations, something in between hand-built and completely generated
29 will probably be the best solution. For admin forms that don't need a lot of
30 styling or special HTML, FormHandler's automatic rendering may be appropriate.
31 FormHandler rendering may also be a good solution if you have enough forms that
32 putting time into creating rendering widgets and themes is worthwhile.
33
34 The automatic rendering is also useful when developing a new form. You can
35 get an idea of what it looks like, and then customize it.
36
37 Another situation in which FormHandler rendering may be useful is when
38 the form is complex enough that working in Perl is a better idea than
39 putting lots of logic into templates.
40
41 All of the rendering is designed to be easily replaced with elements of your
42 own, or to be replaced entirely. You can create your own rendering 'widgets'
43 and load them into the fields by designating the directory in the
44 'widget_name_space'. You could also create a completely separate renderer
45 that's a separate object or class that takes a form object,
46 or a role that is applied to your form.
47
48 Note that unless you set 'no_widgets' in the form, the rendering roles are
49 automatically applied. You don't need to include anything else, unless you
50 want to use a different renderer.
51
52 =head2 Mostly templates
53
54 The names of your fields must match the names of your FormHandler fields.
55 If you use compound fields, you must use the FormHandler naming convention.
56
57 Form used in examples:
58
59 package MyApp::Form::Example;
60 use HTML::FormHandler::Moose;
61 extends 'HTML::FormHandler';
62
63 has_field 'foo';
64 has_field 'bar';
65 has_field 'save' => ( type => 'Submit' );
66
67 If you have existing forms in templates or just prefer them, you can use
68 the 'fill-in-form' values provided with the form's 'fif' function.
69
70 my $form = MyApp::Form::Example->new;
71 $form->process( params => $params );
72 $c->stash( fif => $form->fif );
73 ...
74 <form id="myform" action="/edit/example" method="post">
75 <label>Foo</label>
76 <input id="foo" name="foo" value="[% fif.foo %]">
77 <label>Bar</label>
78 <input id="bar" name="bar" value="[% fif.bar %]">
79 <input type="submit" name="submit" value="Save">
80 </form>
81
82 If you are looking for an easy way to get your fields to line up in an evenly spaced manner,
83 all uniformly aligned, and to do so without using templates or tables, you can externally style the
84 default FormHandler output with the following CSS rule (not supported in internet explorer 6).
85
86 *I<This above is useful for simple forms. Complex forms with fieldsets and other extra features>
87 *I<will require further styling of the HTML. The following rule is also HTML 5 compatible.>
88
89 form#id_of_your_form div div label, form#id_of_your_form div div input {
90 float: left;
91 display: inline-block;
92 width: 40%
93 } /* make sure the parent element is sized appropriately. 700px is a good width */
94
95 Going a little bit farther in using FormHandler rendering, you can render
96 each of the fields individually, using 'render' or 'renderx':
97
98 <form id="myform" action="/edit/example" method="post">
99 <fieldset><legend>My Foo</legend>
100 [% form.field('foo').render %]
101 </fieldset>
102 [% form.field('bar').renderx(element_class => 'cb33') %]
103 [% form.field('save').render %]
104 </form>
105
106 If you don't want the wrappers, use a widget_wrapper of 'None'.
107
108 has '+widget_wrapper' => ( default => 'None' );
109
110 Then you can provide the HTML in which the form elements are
111 embedded:
112
113 <div class="my_class">
114 [% form.field('foo').render %]
115 </div>
116 <div class="another_class">
117 [% form.field('bar').renderx(element_class => 'cb33') %]
118 </div>
119
120 You can also use the 'render_element' or 'render_elementx' methodx, if you want to leave the
121 wrapper in place, but sometimes render 'bare' html elements:
122
123 <div class="my_class">
124 [% form.field('foo').render_element %]
125 </div>
126 <div class="my_class">
127 [% form.field('foo').render_elementx(element_class => 'cb33') %]
128 </div>
129
130 If you wish to loop through the fields yourself, use the 'sorted_fields'
131 method, since it skips inactive fields and handles the 'order' attribute.
132
133 A set of Template Toolkit templates is also provided in the 'share'
134 directory. There are individual templates for each 'widget', such as
135 a checkbox, and there is also an all-in-one template that includes
136 blocks for the various 'widgets'. If you want to use these templates
137 you can just copy them to your template directory and specify the form
138 template in your controller.
139
140 See also L<HTML::FormHandler::Manual::Templates>.
141
142 =head2 Automatic rendering
143
144 If you take all the defaults, you can simply render a form with C<< $form->render >>.
145
146 [% form.render %]
147 or
148 [% form.renderx( form_element_class => ['xxx'] ) %]
149
150 This uses the L<HTML::FormHandler::Widget::Form::Simple> role, which is applied to
151 the form by default. You can use a different form rendering role by including
152 it using 'with':
153
154 with 'HTML::FormHandler::Widget::Form::Table';
155 has '+widget_wrapper' => ( default => 'Table' );
156
157 For the 'Table' form widget, you will also need to set the matching
158 Table widget_wrapper.
159
160 A widget role, providing the 'render' method, and a widget wrapper role,
161 providing the 'wrap_field' method, are applied to each field when the form is built.
162 Each field has a default widget, but you can change that by setting 'widget' to a different
163 widget role:
164
165 has_field 'foxy' => ( widget => 'MyWidget', widget_wrapper => 'MyWrapper' );
166
167 Often if you need custom rendering what you need to provide is a custom widget_wrapper.
168 The 'widgets' render only the input elements, and that often doesn't need to be
169 changed. If you have standard HTML that is used when rendering forms, making custom
170 widget_wrappers is often the way to go.
171
172 Default widget roles are found in the HTML::FormHandler::Widget directory, in the
173 'Field', 'Form', and 'Wrapper subdirectories.
174 The name space used to look for the widget roles can be specified on a form or
175 field basis by setting 'widget_name_space' to an arrayref of name spaces:
176
177 has '+widget_name_space' => ( default => sub { ['MyApp::Form::Widget' ] } );
178
179 For the above widget ('MyWidget') and widget_name_space, you need to have a package named
180 'MyApp::Form::Widget::Field::MyWidget'.
181
182 The HTML::FormHandler::Widget name space is always searched as the last name space.
183 This means that you can set up an application or form specific set of widgets.
184 Widgets in a widget directory (specified in widget_name_space) are located in either
185 a 'Field', 'Wrapper', or 'Form' subdirectory. (Blocks are in a 'Blocks' subdirectory.)
186
187 You can also create an 'all-in-one' type rendering role, using
188 L<HTML::FormHandler::Render::Simple> as a basis. It used the method name
189 'render_field' on the form ( C<< $form->render_field('field_name') >> )
190 instead of the 'render' method on the field.
191
192 In addition to the 'Simple' wrapper, there is a 'Bootstrap' wrapper which
193 creates HTML formatted to use the Twitter Bootstrap 2.0 CSS. There's also a sample
194 "theme", L<HTML::FormHandler::Widget::Theme::Bootstrap>, which is a role that sets
195 the widget_wrapper to 'Bootstrap', and provides Bootstrap-type formatting of the
196 form error message.
197
198 There are a lot of different settings that control the rendering. Some of them are
199 attributes in the form or field, and some of them are set using the 'tags' hashref
200 in the field or the 'form_tags' hashref in the form.
201
202 You can make your own copy of an existing wrapper and add features to it.
203 However, there are so many different ways to render the HTML around a field, that it's
204 very difficult to handle more than a short list of standard presentations in
205 one 'wrapper'. It may be better to make a number of more atomic widget wrappers and
206 use those rather than complicate the already fairly complicated "Simple" and
207 "Bootstrap" wrappers more.
208
209 =head2 HTML attributes
210
211 Arbitrary HTML attributes on form elements (such as 'input' elements) can be specified
212 with 'element_attr' on the field. You can also set attributes for the label with
213 'label_attr' and attributes for the wrapper with 'wrapper_attr'. The 'class'
214 attributes are handled separately, and are arrayrefs (element_class, wrapper_class,
215 label_class):
216
217 has_field 'foo' => ( wrapper_class => ['form', 'special' ] );
218
219 See the documentation on L<HTML::FormHandler::Field/Attributes_for_creating_HTML>.
220
221 =head2 Form settings
222
223 =over 4
224
225 =item widget_wrapper
226
227 The short name of the rendering wrapper widget to be applied to the fields. When
228 the fields are constructed this is merged into fields that do not already
229 set a widget wrapper.
230
231 =item do_form_wrapper
232
233 Flag set with 'sub build_do_form_wrapper{ 1 }'. Default is no form wrapper.
234
235 =item form_tags
236
237 Hashref of various tags used in rendering code. See the documentation
238 for L<HTML::FormHandler::Widget::Form::Simple>.
239
240 =item form_element_attr
241
242 Hashref of arbitrary HTML attributes to be included in the form element.
243
244 sub build_form_element_attr { [ ... ] }
245
246 =item form_element_class
247
248 Arrayref of classes to be included in the form element.
249
250 form_element_class => ['hfh', 'admin']
251 -- or in your class --
252 sub build_form_element_class { ['hfh', 'admin'] }
253
254 The above class would produce a form element:
255
256 <form id="myform" method="post" class="hfh admin">
257
258 =item form_wrapper_attr
259
260 Hashref of arbitrary HTML attributes to be included in the form wrapper
261
262 sub build_form_wrapper_attr { { name => 'formname' } }
263
264 =back
265
266 =head2 Form messages
267
268 Some messages are rendered at the top of the form (inside the form tag)
269 by the 'render_form_messages' method, which is implemented in
270 L<HTML::FormHandler::Widget::Form::Simple> and
271 L<HTML::FormHandler::Widget::Theme::BootstrapFormMessages> (which is
272 included by the Bootstrap theme).
273
274 There are three types of form messages: 'error_message', 'success_message',
275 and 'info_message'. The 'error_message' and 'success_message' are set inside
276 the form:
277
278 has '+success_message' => ( default => 'Form successfully submitted' );
279 has '+error_message' => ( default => 'There were errors in your form.' );
280
281 And then are displayed after the form is validated.
282
283 The 'info_message' is cleared out when a form is re-processed, and so would
284 normally be set on the process call, or between new & process.
285
286 $form->process( params => {}, info_message => 'Fill in the form' );
287
288 =head2 Field settings
289
290 has_field 'foo' => ( widget => 'MyWidget', widget_wrapper => 'SpecialWrapper',
291 element_attr => { placeholder => 'enter a foo' }, element_class => 'important',
292 wrapper_class => ['label'], label_class => ['major'],
293 tags => { wrapper_tag => 'fieldset' } );
294
295 =over 4
296
297 =item widget
298
299 Short name of the rendering widget for this field.
300
301 =item widget_wrapper
302
303 Short name of the wrapping widget for this field.
304
305 =item do_wrapper
306
307 Flag that indicates whether or not the 'wrapper' should be rendered.
308
309 =item do_label
310
311 Flag that indicates whether or not a label should be rendered.
312
313 =item element_attr
314
315 Hashref of arbitrary HTML attributes to include in the element. Note that
316 this does not include the 'id' and 'type' attributes, which are handled
317 separately. The 'id' can be changed with the field's 'id' attribute.
318
319 =item element_class
320
321 Arrayref of classes to include in the element.
322
323 =item wrapper_attr
324
325 Hashref of arbitrary HTML attributes to include in the wrapper.
326
327 =item wrapper_class
328
329 Arrayref of classes to include in the wrapper.
330
331 =item label_attr
332
333 Hashref of arbitrary HTML attributes to include in the label.
334
335 =item label_class
336
337 Arrayref of classes to include in the label.
338
339 =item build_id_method
340
341 Coderef to construct the 'id'. Useful if your javascript needs a different
342 format for the 'id'.
343
344 =item build_label_method
345
346 Coderef to construct the label.
347
348 =item wrap_label_method
349
350 Coderef to wrap the label. Used by the Simple and Bootstrap wrappers.
351 Useful if your label contains HTML or a link. You must do your own
352 localization and filtering if you use a 'wrap_label' method.
353
354 =back
355
356 =head2 html_attributes callback
357
358 The form has an 'html_attributes' callback which can be used to customize,
359 localize, or modify the various attributes when used.
360 Types: element, wrapper, label, form_element, form_wrapper, checkbox_label
361
362 sub html_attributes {
363 my ( $self, $obj, $type, $attrs, $result ) = @_;
364 # obj is either form or field
365 $attrs->{class} = 'label' if $type eq 'label';
366 $attrs->{placeholder} = $self->_localize($attrs->{placeholder})
367 if exists $attrs->{placeholder};
368 return $attrs;
369 }
370
371 This callback is called in the methods that wrap the various '_attr' attributes,
372 i.e. element_attributes, label_attributes, wrapper_attributes,
373 form_element_attributes, form_wrapper_attributes.
374
375 =head2 Field tags
376
377 The 'tags' are settings and strings which may vary by the particular widget
378 that implements them. The best place to look for documentation on them
379 is in the field widget, field wrapper, and form widgets that you are using.
380 The 'tags' allow customizing rendering behavior on a per-field basis.
381 FormHandler has a number of flags/settings that it uses; you can add your
382 own for your custom rendering code.
383
384 wrapper_tag -- the tag to use in the wrapper, default 'div'
385 label_tag -- tag to use for label (default 'label')
386 label_after -- string to append to label, for example ': ' to append a colon
387
388 Tags can be used to switch the Simple
389 wrapper from divs to using paragraphs instead, or to add a colon in
390 label formatting:
391
392 has_field 'my_field' => (
393 tags => {wrapper_tag => 'p', label_after => ': ' } );
394
395 Most of the tags are implemented by the 'wrapper' widget, so see that
396 documentation for more details: L<HTML::FormHandler::Widget::Wrapper::Simple>,
397 L<HTML::FormHandler::Widget::Wrapper::Bootstrap>.
398
399 =head3 Tag types
400
401 The 'get_tag' method will check for these three types of tags and perform
402 the appropriate action.
403
404 =over 4
405
406 =item String
407
408 Standard, most common type of value for a tag.
409
410 has_field 'bar' => ( tags => { before_element => '<span>...</span>' } );
411
412 Some tags are true/false also:
413
414 has_field 'foo' => ( type => 'CheckBox',
415 tags => { no_wrapped_label => 1 } );
416
417 =item CodeRef
418
419 You can supply a coderef to a tag, and it will be executed as a method on
420 the field. This is useful for localization or other sorts of runtime
421 changes.
422
423 has_field 'bar' => ( tags => { before_element => \&bar_element } );
424 sub bar_element {
425 my $self = shift; # $self is the 'bar' field
426 return '<div>In a Sub</div>';
427 }
428
429 =item Block
430
431 You can supply a block by giving a string that consists of a '%' followed
432 by the block name:
433
434 has_block 'comment' => ( tag => 'a', content => 'This is a comment from a block',
435 class => ['comment' ] );
436 has_field 'foo' => ( tags => { before_element => '%comment' } );
437
438 =back
439
440 =head3 Tags and other settings for all fields
441
442 Tags can be set for all fields in the form by using a 'build_update_subfields'
443 sub, or 'widget_tags'. The 'update_subfields' hashref takes general-purpose
444 keys 'all', 'by_flag' (compound, repeatable, contains), and 'by_type'.
445 You can also set specific field attributes by using the field name as a key.
446 For example, if you don't want errors to be displayed
447 next to the fields, you need to set the 'no_errors' tag:
448
449 sub build_update_subfields {{
450 all => { tags => { no_errors => 1 }, wrapper_class => ['myapp'] },
451 by_type => { Text => { element_class => ['text'] } },
452 by_flag => { compound => { do_wrapper => 1 } },
453 foo => { label => 'My Foo' },
454 }}
455 -- or --
456 '+widget_tags' => ( default => sub { { no_errors => 1 } } );
457
458 The 'widget_tags' attribute only handles the 'tags' hashref, so if you also
459 want to set classes or attributes, then build_update_subfields is more useful.
460 You can also use 'build_update_subfields' in a custom compound field class.
461
462 If you have defaults that are set in 'build_update_subfields' in a base class,
463 in order to use hashrefs from both base and current classes, you will need to
464 merge the hashes:
465
466 use HTML::FormHandler::Merge ('merge');
467 sub build_update_subfields {
468 my $self = shift;
469 my $new = { all => { tags => { wrapper_tag => 'p' } } };
470 return merge( $new, $self->next::method(@_) );
471 }
472
473 In a role you would have to do the equivalent with an 'around' method modifier.
474
475 =head3 Repeatable field instances
476
477 The repeatable field instances are constructed internally, so it's trickier to set
478 things like wrapper tags. There are two ways to do it, using the 'init_contains'
479 attribute on the repeatable field, and using the 'update_subfields' builder:
480
481 has_field 'records' => ( type => 'Repeatable', num_when_empty => 2,
482 init_contains => { tags => { wrapper_tag => 'fieldset' } } );
483 -- or --
484 sub build_update_subfields { { by_flag => {
485 contains => { tags => { wrapper_tag => 'fieldset' }}}}}
486
487 The 'build_update_subfields' option is mainly useful if you have multiple
488 repeatable fields that you want to set, or if you want defaults in a base
489 class.
490
491 =head3 widget and widget_wrapper set to 'None'
492
493 If you want to implement the 'render' method in a custom field, you can set
494 'widget' to 'None' and no widget will be applied. Setting the 'widget_wrapper'
495 to 'None' will apply the 'None' wrapper, which simply returns the widget
496 rendering.
497
498 =head3 Error messages
499
500 The default is currently to display error messages next to the rendered fields,
501 if you're doing C<< $form->render >>. If you don't want messages next to fields,
502 you can set the 'no_errors' tag, as discussed in the section on 'Tags and other
503 settings...'.
504
505 Note that the 'None' widget wrapper, since it doesn't render anything except the
506 form element (input, select, etc), will not render errors next to the field.
507 Setting the 'do_wrapper' and 'do_label' flags to 0 will still render errors.
508
509 =head2 Blocks
510
511 When rendering, FormHandler loops through the sorted fields in the form and
512 executes the 'render' method on each field. Fields in FormHandler forms,
513 particularly those that interface with a database, are usually structured
514 in a way that matches the data structure. This doesn't always fit with
515 the way that you want to display the form.
516
517 'Blocks' provide an alternative way of structuring the display. A 'block'
518 is a fairly basic object that contains a 'render' method. The standard
519 block class, L<HTML::FormHandler::Widget::Block>, has Moose attributes to
520 set the HTML tag, the label, the classes, etc, plus a 'render_list'
521 which contains the names of a list of fields or other blocks to render.
522
523 Here is the definition of a fieldset block that contains two fields:
524
525 has_field 'foo';
526 has_field 'bar';
527 has_block 'first_fset' => ( tag => 'fieldset, label => 'Two Fields',
528 render_list => ['foo', 'bar'] );
529
530 The 'first_fset' block will render like this:
531
532 <fieldset><legend>Two Fields</legend>
533 <div>
534 <label>Foo</label>
535 <input type="text" name="foo" id="foo" value="" />
536 <div>
537 <div>
538 <label>Bar</label>
539 <input type="text" name="bar" id="bar" value="" />
540 <div>
541 </fieldset>
542
543 You can also provide a method to 'build' the block's render list
544
545 has_block 'first_fset' => ( tag => 'fieldset, label => 'Two Fields',
546 build_render_list_method => \&build_render_list_first_fset );
547 sub build_render_list_first_fset { ['foo', 'bar'] }
548
549 In order to actually get this block to be used when you render with
550 C<< $form->render >>, you need to supply a 'render_list' on the form
551 level:
552
553 sub build_render_list { ['first_fset', 'submit_btn'] }
554
555 You could also render it with C<< $form->block('first_fset')->render >>.
556
557 Blocks should be located in a widget name space, in a 'Block' directory,
558 or else the name should be prefixed with a '+'.
559
560 has '+widget_name_space' => ( default => sub { ['MyApp::Form::Widget'] };
561 has_block 'first' => ( type => 'MyBlock', ... );
562
563 The 'MyBlock' above will be found in 'MyApp::Form::Widget::Block::MyBlock'.
564
565 has_block 'intro' => ( type => '+MyApp::Form::Component::Intro' );
566
567 A block can inherit from L<HTML::FormHandler::Widget::Block>, but it doesn't
568 have to. At a minimum it must provide 'new' and 'render' methods. If no
569 'type' is specified, the block is created from the L<HTML::FormHandler::Widget::Block>
570 package.
571
572 The following package provides a functional block:
573
574 package MyApp::Component::Section;
575 sub new {
576 my ( $class, %args ) = @_;
577 return bless \%args, $class;
578 }
579 sub form {
580 my $self = shift;
581 return $self->{form};
582 }
583 sub render {
584 return
585 '<div class="intro">
586 <h3>Please enter the relevant details</h3>
587 </div>';
588 }
589 1;
590
591 When a form is rendered, it will either loop through all of the sorted_fields OR
592 loop through the fields and blocks listed in the 'render_list'. A render_list can
593 contain a mix of fields and blocks.
594
595 Note that you must be rendering with widgets to use block rendering.
596
597 =head2 Twitter Bootstrap 2.0 rendering
598
599 The main component of Bootstrap rendering is L<HTML::FormHandler::Widget::Wrapper::Bootstrap>.
600 It produces the standard Bootstrap-style HTML such as:
601
602 <div class="control-group">
603 <label class="control-label" for="input01">Text input</label>
604 <div class="controls">
605 <input type="text" class="input-xlarge" id="input01" name="input01" value="" />
606 </div>
607 </div>
608
609 These are the standard 'control' blocks for Bootstrap vertical and horizontal forms.
610 You can apply this wrapper to all of your fields by setting the widget_wrapper in the form:
611
612 has '+widget_wrapper' => ( default => 'Bootstrap' );
613
614 There is also a sample "theme": L<HTML::FormHandler::Widget::Theme::Bootstrap>. It sets
615 the widget_wrapper for you and provides a 'render_form_messages' method to render a
616 success/error messages section.
617
618 There are a couple of examples in the t/bootstrap directory of Bootstrap inline and search forms,
619 which don't use exactly the same kind of control HTML.
620
621 You can always copy the existing wrapper and add your own features, with settings provided
622 by the 'tags' hashref.
623
624 =head2 Rendering themes
625
626 Many of the flags and settings necessary for rendering can now be moved out into a role.
627 Whether you want to do that or not is a matter of style and preference. The advantage is
628 that it leaves the form class itself cleaner and easier to read. The disadvantage is
629 that your settings come from more different places.
630
631 Here's an example of a form rendering 'theme', taken from the t/bootstrap/basic.t test:
632
633 package MyApp::Form::Basic::Theme;
634 use Moose::Role;
635
636 # make a wrapper around the form
637 sub build_do_form_wrapper {1}
638 # set the class for the form wrapper
639 sub build_form_wrapper_class { ['span9'] }
640 # set the class for the form element
641 sub build_form_element_class { ['well'] }
642 # set various rendering tags
643 sub build_form_tags {
644 { wrapper_tag => 'div',
645 before => qq{<div class="row"><div class="span3"><p>With v2.0, we have
646 lighter and smarter defaults for form styles. No extra markup, just
647 form controls.</p></div>\n},
648 after => '</div>',
649 }
650 }
651
652 # the settings in 'build_update_subfields' are merged with the field
653 # definitions before they are constructed
654 sub build_update_subfields {{
655 # all fields have a label but no wrapper
656 all => { do_wrapper => 0, do_label => 1 },
657 # set the element class, a placeholder in element_attr
658 foo => { element_class => ['span3'],
659 element_attr => { placeholder => 'Type something…' },
660 tags => { after_element =>
661 qq{\n<span class="help-inline">Associated help text!</span>} } },
662 bar => { option_label => 'Check me out',
663 label_class => ['checkbox'], do_label => 0 },
664 submit_btn => { element_class => ['btn'] },
665 }}
666
667 Note that the value 'all' key in the update_subfields hashref will be merged
668 into the attributes used when building all of the fields.
669
670 =head2 Rendering fields
671
672 The default for most fields is a 'div' wrapper and a label. If you don't want
673 the wrapper, set C<< do_wrapper => 0 >>. If you don't want the label, set
674 C<< do_label => 0 >>.
675
676 Checkboxes are most complicated, in that the default
677 is to have two labels. The outer label, the one that's in the same place as the label
678 for other input elements, is set with C<< label => '...' >>. The inner label,
679 which is the equivalent of the C<< label => '...' >> in the options array used
680 for selects and checkbox groups, is set with C<< option_label => '...' >>.
681 There are a number of other 'tags' to control the presentation. See
682 L<HTML::FormHandler::Widget::Field::Checkbox> for more information, and
683 t/render/checkbox.t for examples.
684
685 Some fields by default do not render a label: Button, Submit, Reset, ButtonTag.
686 If you do want a label with these fields, you must set the 'do_label' flag to 1:
687
688 has_field 'foo' ( type => 'Button', do_label => 1 );
689
690 Select fields are also fairly complicated. They can be rendered with the
691 'Select', 'RadioGroup', and 'CheckboxGroup' widgets. Option groups are also
692 supported. See L<HTML::FormHandler::Field::Select>;
693
694 =head2 Rendering labels
695
696 A 'standard' label is built in the field if you don't supply one. The label
697 can be provided in the field definition:
698
699 has_field 'foo' => ( label => 'My Foo' );
700
701 You can also provide a method to 'build' the label:
702
703 has_field 'foo' => ( build_label_method => \&build_label );
704 sub build_label {
705 my $self = shift; # field method
706 return '...';
707 }
708
709 And a method to 'wrap' the label (used by the Simple and Bootstrap wrappers):
710
711 has_field 'foo' => ( label => 'My Foo', wrap_label_method => \&wrap_label );
712 sub wrap_label {
713 my ( $self, $label ) = @_;
714 # or: my $label = $self->label;
715 return qq{<a href="...">$label</a>};
716 }
717
718 This is particularly useful for creating labels that have links or other HTML.
719 The 'wrap_label_method' does no filtering or localization, so you must do that
720 yourself in the method if you need it.
721
722 =head2 Rendering filter
723
724 The base field class has a 'render_filter' attribute which is a coderef used to
725 clean the values used to fill in the form for Render::Simple and the Widgets,
726 and for some of the labels..
727 The default filter changes quote, ampersand, <, and > to the equivalent html
728 entities. If you wish to use some other sort of filtering, you can use the
729 'render_filter' method in your form, or set a coderef on individual field
730 objects. A 'render_filter' function in your form will be used by all fields.
731 Setting it for a field will just be for that field.
732
733 sub render_filter {
734 my $string = shift;
735 $string =~ s/my/MY/g; # perform some kind of transformation
736 return $string;
737 }
738 -- or --
739 has_field 'foo' => ( render_filter => sub { ... } );
740
741 The filter is called in Render::Simple and in the widgets with
742 C<< $self->html_filter( $fif ) >> or C<< $field->html_filter( $fif ) >>.
743
744 If you want to turn off the filter for a particular field, you can set it
745 to a sub that just returns the value:
746
747 has_field 'bar' => ( render_filter => sub { shift } );
748
749 If you want a label that is unfiltered, see 'wrap_label_method'.
750
751 =head1 Special rendering pseudo-fields
752
753 Also see L<HTML::FormHandler::Widget::Block>. Blocks may be a better
754 solution than pseudo-fields (i.e. fields that aren't actual form
755 elements).
756
757 Various 'tags' used for rendering can also be used for similar
758 purposes.
759
760 =head2 NonEditable
761
762 Like a Bootstrap 'non_editable' field. Displays the field's value
763 as a span.
764
765 has_field 'non_edit' => ( type => 'NonEditable', value => 'This is a Test' );
766
767 =head2 Display
768
769 L<HTML::FormHandler::Field::Display>
770
771 You can supply an HTML string to this field, to be displayed directly. There is no
772 'value' associated with this field; it's a field for rendering only. The HTML
773 string can be built with a form or field method.
774
775 Blocks or tags will often be a better solution.
776
777 =head1 AUTHOR
778
779 FormHandler Contributors - see HTML::FormHandler
780
781 =head1 COPYRIGHT AND LICENSE
782
783 This software is copyright (c) 2017 by Gerda Shank.
784
785 This is free software; you can redistribute it and/or modify it under
786 the same terms as the Perl 5 programming language system itself.
787
788 =cut