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.40067';
4 141     141   73487 use Moose::Role;
  141         282  
  141         1100  
5 141     141   489735 use HTML::FormHandler::Render::Util ('process_attrs');
  141         257  
  141         1083  
6              
7             with 'HTML::FormHandler::Widget::Form::Role::HTMLAttributes';
8              
9              
10             sub renderx {
11 1     1 0 803 my ($self, %args) = @_;
12              
13 1 50       5 if ( keys %args > 0 ) {
14 1         5 while ( my ( $key, $value ) = each %args ) {
15 1 50       9 confess "invalid attribute '$key' passed to renderx"
16             unless $self->can($key);
17 1         35 $self->$key($value);
18             }
19             }
20 1         7 $self->render;
21             }
22              
23              
24             sub render {
25 67     67 0 10195 my ($self) = @_;
26              
27 67         132 my $result;
28             my $form;
29             # NOTE: do not use $self in this method; use $result or $form
30 67 100       1421 if ( $self->DOES('HTML::FormHandler::Result') ) {
31 3         33 $result = $self;
32 3         80 $form = $self->form;
33             }
34             else {
35 64         36767 $result = $self->result;
36 64         117 $form = $self;
37             }
38 67         450 my $output = $form->render_start($result);
39 67         388 $output .= $form->render_form_messages($result);
40              
41 67 100       2341 if ( $form->has_render_list ) {
42 6         9 foreach my $fb ( @{ $form->render_list } ) {
  6         189  
43             # it's a Field
44 16 100       492 if ( $form->field_in_index($fb) ) {
45             # find field result and use that
46 4         24 my $fld_result = $result->get_result($fb);
47             # if no result, then we shouldn't be rendering this field
48 4 50       9 next unless $fld_result;
49 4         87 $output .= $fld_result->render;
50             }
51             # it's a Block
52             else {
53             # always use form level result for blocks
54 12         362 my $block = $form->block($fb);
55 12 50       36 die "found no form field or block named '$fb'\n" unless $block;
56 12         49 $output .= $block->render($result);
57             }
58             }
59             }
60             else {
61 61         2196 foreach my $fld_result ( $result->results ) {
62 231         788 $output .= $fld_result->render;
63             }
64             }
65              
66 66         653 $output .= $form->render_end($result);
67 66         422 return $output;
68             }
69              
70             sub render_start {
71 74     74 0 2713 my ( $self, $result ) = @_;
72 74   66     591 $result ||= $self->result;
73              
74 74         158 my $output = '';
75 74         448 $output = $self->get_tag('before');
76              
77 74   100     233 my $wtag = $self->get_tag('wrapper_tag') || 'fieldset';
78              
79             # render wrapper start if not fieldset
80 74 100       288 $output .= $self->render_wrapper_start($wtag, $result)
81             if $wtag ne 'fieldset';
82             # render form tag
83 74         512 my $attrs = process_attrs($self->attributes($result));
84 74         270 $output .= qq{<form$attrs>};
85             # render wrapper start if fieldset (not legal outside form tag)
86 74 100       584 $output .= $self->render_wrapper_start($wtag)
87             if $wtag eq 'fieldset';
88 74         237 $output .= $self->get_tag('after_start');
89              
90 74         187 return $output
91             }
92              
93             sub render_wrapper_start {
94 74     74 0 148 my ( $self, $wrapper_tag, $result ) = @_;
95 74 100       2241 return '' unless $self->do_form_wrapper;
96 11   66     184 $result ||= $self->result;
97 11         68 my $attrs = process_attrs($self->form_wrapper_attributes($result));
98 11         49 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 62     62 0 114 my ( $self, $result ) = @_;
104 62   33     190 $result ||= $self->result;
105              
106 62 100       203 return '' if $self->get_tag('no_form_message_div');
107 60   50     177 my $messages_wrapper_class = $self->get_tag('messages_wrapper_class') || 'form_messages';
108 60         159 my $output = qq{\n<div class="$messages_wrapper_class">};
109 60   50     188 my $error_class = $self->get_tag('error_class') || 'error_message';
110 60 0 0     1844 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 60 100       2093 if ( $result->has_form_errors ) {
116             $output .= qq{\n<span class="$error_class">$_</span>}
117 3         96 for $result->all_form_errors;
118             }
119 60 100 100     1850 if( $self->has_success_message && $result->validated ) {
120 1         18 my $msg = $self->success_message;
121 1         12 $msg = $self->_localize($msg);
122 1   50     5 my $success_class = $self->get_tag('success_class') || 'success_message';
123 1         5 $output .= qq{\n<span class="$success_class">$msg</span>};
124             }
125 60 100 66     1802 if( $self->has_info_message && $self->info_message ) {
126 1         22 my $msg = $self->info_message;
127 1         6 $msg = $self->_localize($msg);
128 1   50     4 my $info_class = $self->get_tag('info_class') || 'info_message';
129 1         4 $output .= qq{\n<span class="$info_class">$msg</span>};
130             }
131 60         155 $output .= "\n</div>";
132 60         167 return $output;
133             }
134              
135             sub render_end {
136 69     69 0 135 my $self = shift;
137              
138 69         305 my $output = $self->get_tag('before_end');
139 69   100     222 my $wtag = $self->get_tag('wrapper_tag') || 'fieldset';
140 69 100       559 $output .= $self->render_wrapper_end($wtag) if $wtag eq 'fieldset';
141 69         139 $output .= "\n</form>";
142 69 100       225 $output .= $self->render_wrapper_end($wtag) if $wtag ne 'fieldset';
143 69         223 $output .= $self->get_tag('after');
144 69         141 $output .= "\n";
145 69         176 return $output;
146             }
147              
148             sub render_wrapper_end {
149 69     69 0 154 my ( $self, $wrapper_tag ) = @_;
150 69 100       1843 return '' unless $self->do_form_wrapper;
151 7         28 return qq{\n</$wrapper_tag>};
152             }
153 141     141   126432 use namespace::autoclean;
  141         235  
  141         980  
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.40067
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) 2016 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