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.40067';
4 7     7   3720 use Moose::Role;
  7         15  
  7         41  
5 7     7   23780 use namespace::autoclean;
  7         11  
  7         81  
6 7     7   386 use HTML::FormHandler::Render::Util ('process_attrs');
  7         11  
  7         47  
7              
8             with 'HTML::FormHandler::Widget::Wrapper::Base';
9              
10              
11              
12             sub wrap_field {
13 55     55 0 80 my ( $self, $result, $rendered_widget ) = @_;
14              
15 55         62 my $output;
16             # is this a control group or a form action?
17 55 100 100     1402 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         256 my $attr = $self->wrapper_attributes($result);
21             # no 'control-group' class for Hidden fields, 'form-actions' for submit/reset
22 55 100       1337 my $div_class = $self->type eq 'Hidden' ? undef : $form_actions ? "form-actions" : "control-group";
    100          
23 55 100       119 unshift @{$attr->{class}}, $div_class if $div_class;
  54         150  
24 55         157 my $attr_str = process_attrs( $attr );
25             # wrapper is always a div
26 55 100       1543 if ( $self->do_wrapper ) {
27 49         209 $output .= $self->get_tag('before_wrapper');
28 49         101 $output .= qq{\n<div$attr_str>};
29             }
30             # render the label
31 55 100       1415 $output .= "\n" . $self->do_render_label($result, undef, ['control-label'] )
32             if $self->do_label;
33 55         160 $output .= $self->get_tag('before_element');
34             # the controls div for ... controls
35 55 100 100     1300 $output .= qq{\n<div class="controls">}
36             unless $form_actions || !$self->do_wrapper;
37             # yet another tag
38 55         109 $output .= $self->get_tag('before_element_inside_div');
39             # handle input-prepend and input-append
40 55 100 100     112 if( $self->get_tag('input_prepend') || $self->get_tag('input_append') ||
    100 100        
41             $self->get_tag('input_append_button') ) {
42 5         18 $rendered_widget = $self->do_prepend_append($rendered_widget);
43             }
44             elsif( lc $self->widget eq 'checkbox' ) {
45 8         31 $rendered_widget = $self->wrap_checkbox($result, $rendered_widget)
46             }
47              
48 55         137 $output .= "\n$rendered_widget";
49             # various 'help-inline' bits: errors, warnings
50 55 50       132 unless( $self->get_tag('no_errors') ) {
51             $output .= qq{\n<span class="help-inline">$_</span>}
52 55         1748 for $result->all_errors;
53 55         1807 $output .= qq{\n<span class="help-inline">$_</span>} for $result->all_warnings;
54             }
55             # extra after element stuff
56 55         115 $output .= $self->get_tag('after_element');
57             # close 'control' div
58 55 100 100     1275 $output .= '</div>' unless $form_actions || !$self->do_wrapper;
59             # close wrapper
60 55 100       1246 if ( $self->do_wrapper ) {
61 49         61 $output .= "\n</div>";
62 49         100 $output .= $self->get_tag('after_wrapper');
63             }
64 55         314 return "$output";
65             }
66              
67             sub do_prepend_append {
68 5     5 0 9 my ( $self, $rendered_widget ) = @_;
69              
70 5         5 my @class;
71 5 100       12 if( my $ip_tag = $self->get_tag('input_prepend' ) ) {
72 2         7 $rendered_widget = qq{<span class="add-on">$ip_tag</span>$rendered_widget};
73 2         4 push @class, 'input-prepend';
74             }
75 5 100       12 if ( my $ia_tag = $self->get_tag('input_append' ) ) {
76 2         7 $rendered_widget = qq{$rendered_widget<span class="add-on">$ia_tag</span>};
77 2         4 push @class, 'input-append';
78             }
79 5 100       14 if ( my $iab_tag = $self->get_tag('input_append_button') ) {
80 2 100       9 my @buttons = ref $iab_tag eq 'ARRAY' ? @$iab_tag : ($iab_tag);
81 2         4 foreach my $btn ( @buttons ) {
82 3         9 $rendered_widget = qq{$rendered_widget<button type="button" class="btn">$btn</button>};
83             }
84 2         4 push @class, 'input-append';
85             }
86 5         21 my $attr = process_attrs( { class => \@class } );
87 5         18 $rendered_widget =
88             qq{<div$attr>
89             $rendered_widget
90             </div>};
91 5         9 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.40067
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) 2016 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