File Coverage

blib/lib/HTML/FormHandler/Widget/Form/Simple.pm
Criterion Covered Total %
statement 87 91 95.6
branch 30 36 83.3
condition 19 33 57.5
subroutine 10 11 90.9
pod 0 8 0.0
total 146 179 81.5


line stmt bran cond sub pod time code
1             package HTML::FormHandler::Widget::Form::Simple;
2             # ABSTRACT: widget to render a form with divs
3             $HTML::FormHandler::Widget::Form::Simple::VERSION = '0.40068';
4 143     143   98319 use Moose::Role;
  143         425  
  143         1358  
5 143     143   763264 use HTML::FormHandler::Render::Util ('process_attrs');
  143         449  
  143         1340  
6              
7             with 'HTML::FormHandler::Widget::Form::Role::HTMLAttributes';
8              
9              
10             sub renderx {
11 1     1 0 1237 my ($self, %args) = @_;
12              
13 1 50       10 if ( keys %args > 0 ) {
14 1         9 while ( my ( $key, $value ) = each %args ) {
15 1 50       16 confess "invalid attribute '$key' passed to renderx"
16             unless $self->can($key);
17 1         55 $self->$key($value);
18             }
19             }
20 1         13 $self->render;
21             }
22              
23              
24             sub render {
25 68     68 0 11071 my ($self) = @_;
26              
27 68         205 my $result;
28             my $form;
29             # NOTE: do not use $self in this method; use $result or $form
30 68 100       1579 if ( $self->DOES('HTML::FormHandler::Result') ) {
31 3         45 $result = $self;
32 3         101 $form = $self->form;
33             }
34             else {
35 65         52783 $result = $self->result;
36 65         193 $form = $self;
37             }
38 68         569 my $output = $form->render_start($result);
39 68         537 $output .= $form->render_form_messages($result);
40              
41 68 100       2677 if ( $form->has_render_list ) {
42 7         21 foreach my $fb ( @{ $form->render_list } ) {
  7         233  
43             # it's a Field
44 19 100       676 if ( $form->field_in_index($fb) ) {
45             # find field result and use that
46 5         173 my $fld_result = $result->get_result($fb);
47             # if no result, then we shouldn't be rendering this field
48 5 50       26 next unless $fld_result;
49 5         33 $output .= $fld_result->render;
50             }
51             # it's a Block
52             else {
53             # always use form level result for blocks
54 14         436 my $block = $form->block($fb);
55 14 50       53 die "found no form field or block named '$fb'\n" unless $block;
56 14         82 $output .= $block->render($result);
57             }
58             }
59             }
60             else {
61 61         2405 foreach my $fld_result ( $result->results ) {
62 231         1187 $output .= $fld_result->render;
63             }
64             }
65              
66 67         920 $output .= $form->render_end($result);
67 67         555 return $output;
68             }
69              
70             sub render_start {
71 75     75 0 3862 my ( $self, $result ) = @_;
72 75   66     748 $result ||= $self->result;
73              
74 75         217 my $output = '';
75 75         581 $output = $self->get_tag('before');
76              
77 75   100     359 my $wtag = $self->get_tag('wrapper_tag') || 'fieldset';
78              
79             # render wrapper start if not fieldset
80 75 100       408 $output .= $self->render_wrapper_start($wtag, $result)
81             if $wtag ne 'fieldset';
82             # render form tag
83 75         655 my $attrs = process_attrs($self->attributes($result));
84 75         400 $output .= qq{<form$attrs>};
85             # render wrapper start if fieldset (not legal outside form tag)
86 75 100       819 $output .= $self->render_wrapper_start($wtag)
87             if $wtag eq 'fieldset';
88 75         367 $output .= $self->get_tag('after_start');
89              
90 75         276 return $output
91             }
92              
93             sub render_wrapper_start {
94 75     75 0 299 my ( $self, $wrapper_tag, $result ) = @_;
95 75 100       2781 return '' unless $self->do_form_wrapper;
96 11   66     252 $result ||= $self->result;
97 11         84 my $attrs = process_attrs($self->form_wrapper_attributes($result));
98 11         71 return qq{<$wrapper_tag$attrs>};
99             }
100              
101 0     0 0 0 sub render_form_errors { shift->render_form_messages(@_) }
102             sub render_form_messages {
103 63     63 0 221 my ( $self, $result ) = @_;
104 63   33     275 $result ||= $self->result;
105              
106 63 100       289 return '' if $self->get_tag('no_form_message_div');
107 61   50     268 my $messages_wrapper_class = $self->get_tag('messages_wrapper_class') || 'form_messages';
108 61         528 my $output = qq{\n<div class="$messages_wrapper_class">};
109 61   50     272 my $error_class = $self->get_tag('error_class') || 'error_message';
110 61 0 0     2106 if( $self->has_error_message && ( $result->has_errors || $result->has_form_errors ) ) {
      33        
111 0         0 my $msg = $self->error_message;
112 0         0 $msg = $self->_localize($msg);
113 0         0 $output .= qq{\n<span class="$error_class">$msg</span>};
114             }
115 61 100       2284 if ( $result->has_form_errors ) {
116             $output .= qq{\n<span class="$error_class">$_</span>}
117 3         102 for $result->all_form_errors;
118             }
119 61 100 100     2036 if( $self->has_success_message && $result->validated ) {
120 1         20 my $msg = $self->success_message;
121 1         13 $msg = $self->_localize($msg);
122 1   50     5 my $success_class = $self->get_tag('success_class') || 'success_message';
123 1         6 $output .= qq{\n<span class="$success_class">$msg</span>};
124             }
125 61 100 66     1978 if( $self->has_info_message && $self->info_message ) {
126 1         24 my $msg = $self->info_message;
127 1         6 $msg = $self->_localize($msg);
128 1   50     5 my $info_class = $self->get_tag('info_class') || 'info_message';
129 1         5 $output .= qq{\n<span class="$info_class">$msg</span>};
130             }
131 61         241 $output .= "\n</div>";
132 61         209 return $output;
133             }
134              
135             sub render_end {
136 70     70 0 234 my $self = shift;
137              
138 70         435 my $output = $self->get_tag('before_end');
139 70   100     371 my $wtag = $self->get_tag('wrapper_tag') || 'fieldset';
140 70 100       751 $output .= $self->render_wrapper_end($wtag) if $wtag eq 'fieldset';
141 70         227 $output .= "\n</form>";
142 70 100       370 $output .= $self->render_wrapper_end($wtag) if $wtag ne 'fieldset';
143 70         338 $output .= $self->get_tag('after');
144 70         226 $output .= "\n";
145 70         296 return $output;
146             }
147              
148             sub render_wrapper_end {
149 70     70 0 283 my ( $self, $wrapper_tag ) = @_;
150 70 100       2245 return '' unless $self->do_form_wrapper;
151 7         39 return qq{\n</$wrapper_tag>};
152             }
153 143     143   150399 use namespace::autoclean;
  143         459  
  143         1408  
154             1;
155              
156             __END__
157              
158             =pod
159              
160             =encoding UTF-8
161              
162             =head1 NAME
163              
164             HTML::FormHandler::Widget::Form::Simple - widget to render a form with divs
165              
166             =head1 VERSION
167              
168             version 0.40068
169              
170             =head1 SYNOPSIS
171              
172             Role to apply to form objects to allow rendering. This rendering
173             role is applied to HTML::FormHandler by default. It supports block
174             rendering. (L<HTML::FormHandler::Blocks>, L<HTML::FormHandler::Widget::Block>)
175              
176             Relevant flags:
177              
178             do_form_wrapper - put a wrapper around the form
179              
180             If the wrapper_tag is a 'fieldset' (default if not specified) the
181             wrapper goes inside the form tags (because it's not valid to put it
182             outside of them). If the wrapper_tag is something else, it will go
183             around the form tags. If you're doing one kind of wrapper and want
184             another one, you can achieve that result by using the 'before'/'after'
185             tags or the 'after_start'/'before_end' tags.
186              
187             Supported tags:
188              
189             wrapper_tag -- tag for form wrapper; default 'fieldset'
190             before
191             after
192             after_start
193             before_end
194              
195             messages_wrapper_class -- default 'form_messages'
196             error_class -- default 'error_message'
197             error_message -- message to issue when form contains errors
198             success_class -- default 'success_message'
199             success_message -- message to issue when form was submitted successfully
200              
201             =head1 AUTHOR
202              
203             FormHandler Contributors - see HTML::FormHandler
204              
205             =head1 COPYRIGHT AND LICENSE
206              
207             This software is copyright (c) 2017 by Gerda Shank.
208              
209             This is free software; you can redistribute it and/or modify it under
210             the same terms as the Perl 5 programming language system itself.
211              
212             =cut