File Coverage

blib/lib/HTML/FormHandler/Widget/Wrapper/Bootstrap.pm
Criterion Covered Total %
statement 53 53 100.0
branch 31 32 96.8
condition 17 18 94.4
subroutine 5 5 100.0
pod 0 2 0.0
total 106 110 96.3


line stmt bran cond sub pod time code
1             package HTML::FormHandler::Widget::Wrapper::Bootstrap;
2             # ABSTRACT: Twitter Bootstrap 2.0 field wrapper
3             $HTML::FormHandler::Widget::Wrapper::Bootstrap::VERSION = '0.40068';
4 7     7   4411 use Moose::Role;
  7         22  
  7         91  
5 7     7   34943 use namespace::autoclean;
  7         21  
  7         52  
6 7     7   471 use HTML::FormHandler::Render::Util ('process_attrs');
  7         20  
  7         55  
7              
8             with 'HTML::FormHandler::Widget::Wrapper::Base';
9              
10              
11              
12             sub wrap_field {
13 55     55 0 160 my ( $self, $result, $rendered_widget ) = @_;
14              
15 55         102 my $output;
16             # is this a control group or a form action?
17 55 100 100     1486 my $form_actions = 1 if ( $self->name eq 'form_actions' || $self->type_attr eq 'submit'
      66        
18             || $self->type_attr eq 'reset' );
19             # create attribute string for wrapper
20 55         320 my $attr = $self->wrapper_attributes($result);
21             # no 'control-group' class for Hidden fields, 'form-actions' for submit/reset
22 55 100       1507 my $div_class = $self->type eq 'Hidden' ? undef : $form_actions ? "form-actions" : "control-group";
    100          
23 55 100       212 unshift @{$attr->{class}}, $div_class if $div_class;
  54         220  
24 55         197 my $attr_str = process_attrs( $attr );
25             # wrapper is always a div
26 55 100       1668 if ( $self->do_wrapper ) {
27 49         292 $output .= $self->get_tag('before_wrapper');
28 49         186 $output .= qq{\n<div$attr_str>};
29             }
30             # render the label
31 55 100       1567 $output .= "\n" . $self->do_render_label($result, undef, ['control-label'] )
32             if $self->do_label;
33 55         224 $output .= $self->get_tag('before_element');
34             # the controls div for ... controls
35 55 100 100     1407 $output .= qq{\n<div class="controls">}
36             unless $form_actions || !$self->do_wrapper;
37             # yet another tag
38 55         179 $output .= $self->get_tag('before_element_inside_div');
39             # handle input-prepend and input-append
40 55 100 100     184 if( $self->get_tag('input_prepend') || $self->get_tag('input_append') ||
    100 100        
41             $self->get_tag('input_append_button') ) {
42 5         17 $rendered_widget = $self->do_prepend_append($rendered_widget);
43             }
44             elsif( lc $self->widget eq 'checkbox' ) {
45 8         38 $rendered_widget = $self->wrap_checkbox($result, $rendered_widget)
46             }
47              
48 55         203 $output .= "\n$rendered_widget";
49             # various 'help-inline' bits: errors, warnings
50 55 50       183 unless( $self->get_tag('no_errors') ) {
51             $output .= qq{\n<span class="help-inline">$_</span>}
52 55         1902 for $result->all_errors;
53 55         1918 $output .= qq{\n<span class="help-inline">$_</span>} for $result->all_warnings;
54             }
55             # extra after element stuff
56 55         191 $output .= $self->get_tag('after_element');
57             # close 'control' div
58 55 100 100     1420 $output .= '</div>' unless $form_actions || !$self->do_wrapper;
59             # close wrapper
60 55 100       1418 if ( $self->do_wrapper ) {
61 49         120 $output .= "\n</div>";
62 49         147 $output .= $self->get_tag('after_wrapper');
63             }
64 55         404 return "$output";
65             }
66              
67             sub do_prepend_append {
68 5     5 0 12 my ( $self, $rendered_widget ) = @_;
69              
70 5         9 my @class;
71 5 100       13 if( my $ip_tag = $self->get_tag('input_prepend' ) ) {
72 2         8 $rendered_widget = qq{<span class="add-on">$ip_tag</span>$rendered_widget};
73 2         5 push @class, 'input-prepend';
74             }
75 5 100       15 if ( my $ia_tag = $self->get_tag('input_append' ) ) {
76 2         8 $rendered_widget = qq{$rendered_widget<span class="add-on">$ia_tag</span>};
77 2         5 push @class, 'input-append';
78             }
79 5 100       13 if ( my $iab_tag = $self->get_tag('input_append_button') ) {
80 2 100       11 my @buttons = ref $iab_tag eq 'ARRAY' ? @$iab_tag : ($iab_tag);
81 2         5 foreach my $btn ( @buttons ) {
82 3         12 $rendered_widget = qq{$rendered_widget<button type="button" class="btn">$btn</button>};
83             }
84 2         6 push @class, 'input-append';
85             }
86 5         27 my $attr = process_attrs( { class => \@class } );
87 5         19 $rendered_widget =
88             qq{<div$attr>
89             $rendered_widget
90             </div>};
91 5         15 return $rendered_widget;
92             }
93              
94             1;
95              
96             __END__
97              
98             =pod
99              
100             =encoding UTF-8
101              
102             =head1 NAME
103              
104             HTML::FormHandler::Widget::Wrapper::Bootstrap - Twitter Bootstrap 2.0 field wrapper
105              
106             =head1 VERSION
107              
108             version 0.40068
109              
110             =head1 SYNOPSIS
111              
112             Wrapper to implement Bootstrap 2.0 style form element rendering. This wrapper
113             does some very specific Bootstrap things, like wrap the form elements
114             in divs with non-changeable classes. It is not as flexible as the
115             'Simple' wrapper, but means that you don't have to specify those classes
116             in your form code.
117              
118             It wraps form elements with 'control-group' divs, and form 'actions' with
119             'form-actions' divs. It adds special additional wrappers for checkboxes and radio
120             buttons, with wrapped labels.
121              
122             =head1 DESCRIPTION
123              
124             Tags supported:
125              
126             label_no_filter -- don't html filter the label
127             label_after -- useful for putting a colon, or other trailing formatting
128             before_element -- insert tag before input, outside element's control div
129             before_element_inside_div -- insert tag before input element, inside control div
130             input_prepend -- for Bootstrap 'input-prepend' class
131             input_append -- for Bootstrap 'input-append' class
132             input_append_button -- 'input-append' with button instead of span
133             no_errors -- don't append error to field rendering
134             after_element -- insert tag after input element
135              
136             =head1 AUTHOR
137              
138             FormHandler Contributors - see HTML::FormHandler
139              
140             =head1 COPYRIGHT AND LICENSE
141              
142             This software is copyright (c) 2017 by Gerda Shank.
143              
144             This is free software; you can redistribute it and/or modify it under
145             the same terms as the Perl 5 programming language system itself.
146              
147             =cut