Debian Codegraph @

lib/HTML/FormHandler/Widget/Wrapper/Simple.pm

raw
1 package HTML::FormHandler::Widget::Wrapper::Simple;
2 # ABSTRACT: simple field wrapper
3 $HTML::FormHandler::Widget::Wrapper::Simple::VERSION = '0.410002';
4 use Moose::Role;
5 use HTML::Entities qw( encode_entities );
6 use namespace::autoclean;
7 use HTML::FormHandler::Render::Util ('process_attrs');
8
9 with 'HTML::FormHandler::Widget::Wrapper::Base';
10
11
12
13 sub wrap_field {
14 my ( $self, $result, $rendered_widget ) = @_;
15
16 my $output;
17 # get wrapper tag if set
18 my $label_tag = $self->label_tag || '';
19 my $wrapper_tag;
20 if( $self->do_wrapper ) {
21 $output .= $self->get_tag('before_wrapper');
22 $wrapper_tag = $self->get_tag('wrapper_tag');
23 # default wrapper tags
24 $wrapper_tag ||= $self->has_flag('is_repeatable') ? 'fieldset' : 'div';
25 # get attribute string
26 my $attrs = process_attrs( $self->wrapper_attributes($result) );
27 # write wrapper tag
28 $output .= qq{\n<$wrapper_tag$attrs>};
29 $label_tag = 'legend' if $wrapper_tag eq 'fieldset';
30 }
31 # write label; special processing for checkboxes
32 $rendered_widget = $self->wrap_checkbox($result, $rendered_widget)
33 if ( lc $self->widget eq 'checkbox' );
34 $output .= "\n" . $self->do_render_label($result, $label_tag)
35 if $self->do_label;
36 # append 'before_element'
37 $output .= $self->get_tag('before_element');
38
39 # start controls div
40 if ( $self->get_tag('controls_div') ) {
41 $output .= qq{\n<div class="controls">};
42 }
43 elsif ( $self->has_element_wrapper_class ) {
44 my $ew_attr = $self->element_wrapper_attributes($result);
45 my $element_wrapper_attrs = process_attrs( $ew_attr );
46 $output .= qq{\n<div$element_wrapper_attrs>};
47 }
48
49 # the input element itself
50 $output .= "\n$rendered_widget";
51
52 # close controls div
53 if ( $self->get_tag('controls_div') || $self->has_element_wrapper_class ) {
54 # end control div
55 $output .= "\n</div>";
56 }
57
58 # the 'after_element'
59 $output .= $self->get_tag('after_element');
60 # the error messages
61 unless( $self->get_tag('no_errors') ) {
62 my $error_class = $self->get_tag('error_class') || 'error_message';
63 $output .= qq{\n<span class="$error_class">$_</span>}
64 for map { encode_entities($_) } $result->all_errors;
65 # warnings (incompletely implemented - only on field itself)
66 my $warning_class = $self->get_tag('warning_class') || 'warning_message';
67 $output .= qq{\n<span class="warning_message">$_</span>}
68 for $result->all_warnings;
69 }
70 if( $self->do_wrapper ) {
71 $output .= "\n</$wrapper_tag>";
72 $output .= $self->get_tag('after_wrapper');
73 }
74 return "$output";
75 }
76
77
78 1;
79
80 __END__
81
82 =pod
83
84 =encoding UTF-8
85
86 =head1 NAME
87
88 HTML::FormHandler::Widget::Wrapper::Simple - simple field wrapper
89
90 =head1 VERSION
91
92 version 0.410002
93
94 =head1 SYNOPSIS
95
96 This is the default wrapper role. It will be installed if
97 no other wrapper is specified and widget_wrapper is not set to
98 'none'.
99
100 Relevant field flags:
101
102 do_wrapper
103 do_label
104
105 If 'do_label' is set and not 'do_wrapper', only the label plus
106 the form element will be rendered.
107
108 Supported 'tags', all set via the 'tags' hashref on the field:
109
110 wrapper_tag -- the tag to use in the wrapper, default 'div'
111
112 label_tag -- tag to use for label (default 'label')
113 label_after -- string to append to label, for example ': ' to append a colon
114
115 before_element -- string that goes right before the element
116 after_element -- string that goes right after the element
117
118 no_errors -- don't issue error messages on the field
119 error_class -- class for error messages (default 'error_message')
120 warning_class -- class for warning messages (default 'warning_message' )
121
122 no_wrapped_label -- for checkboxes. Don't provide an inner wrapped label
123 (from Base wrapper)
124
125 Example:
126
127 has_field 'foo' => ( tags => { wrapper_tag => 'span', no_errors => 1 } );
128
129 =head1 AUTHOR
130
131 FormHandler Contributors - see HTML::FormHandler
132
133 =head1 COPYRIGHT AND LICENSE
134
135 This software is copyright (c) 2017 by Gerda Shank.
136
137 This is free software; you can redistribute it and/or modify it under
138 the same terms as the Perl 5 programming language system itself.
139
140 =cut