Debian Codegraph @

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

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