line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTML::FormHandler::Render::Hash; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
25022
|
use Moose::Role; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
with 'HTML::FormHandler::Render::Simple' => { |
6
|
|
|
|
|
|
|
excludes => [qw( |
7
|
|
|
|
|
|
|
render render_field_struct render_text |
8
|
|
|
|
|
|
|
render_password render_hidden render_select |
9
|
|
|
|
|
|
|
render_checkbox render_radio_group render_textarea |
10
|
|
|
|
|
|
|
render_compound render_submit |
11
|
|
|
|
|
|
|
)] |
12
|
|
|
|
|
|
|
}; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 NAME |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
HTML::FormHandler::Render::Hash - render a form to a raw hash |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 VERSION |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
Version 0.02 |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=cut |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 SYNOPSIS |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
To render a form as a hash, use this in a form: |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
package My::Form::User; |
31
|
|
|
|
|
|
|
with 'HTML::FormHandler::Render::Hash'; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
then, to render it to a template: |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
my $data = $form->render(); |
36
|
|
|
|
|
|
|
... |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=cut |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub render |
41
|
|
|
|
|
|
|
{ |
42
|
|
|
|
|
|
|
my $self = shift; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
my %output = ( |
45
|
|
|
|
|
|
|
field => [] |
46
|
|
|
|
|
|
|
); |
47
|
|
|
|
|
|
|
$output{action} = $self->action if $self->action; |
48
|
|
|
|
|
|
|
$output{name} = $self->name if $self->name; |
49
|
|
|
|
|
|
|
$output{method} = $self->http_method if $self->http_method; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
foreach my $field ($self->sorted_fields) { |
52
|
|
|
|
|
|
|
push @{ $output{field} }, $self->render_field($field); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
return \%output; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub render_field_struct |
58
|
|
|
|
|
|
|
{ |
59
|
|
|
|
|
|
|
my ($self, $field, $rendered_field, $class) = @_; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
my %output = ( |
62
|
|
|
|
|
|
|
id => $field->id, |
63
|
|
|
|
|
|
|
widget => $field->widget, |
64
|
|
|
|
|
|
|
label => $field->label, |
65
|
|
|
|
|
|
|
name => $field->html_name, |
66
|
|
|
|
|
|
|
%{ $rendered_field }, |
67
|
|
|
|
|
|
|
); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
my $l_type = defined $self->get_label_type( $field->widget ) |
70
|
|
|
|
|
|
|
? $self->get_label_type( $field->widget ) |
71
|
|
|
|
|
|
|
: ''; |
72
|
|
|
|
|
|
|
$class =~ s/^ class="//; |
73
|
|
|
|
|
|
|
$class =~ s/"$//; |
74
|
|
|
|
|
|
|
$output{class} = $class if $class; |
75
|
|
|
|
|
|
|
$output{label_type} = $l_type if $l_type; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
if ($field->has_errors) { |
78
|
|
|
|
|
|
|
$output{errors} = { error => [] }; |
79
|
|
|
|
|
|
|
push @{ $output{errors}{error} }, $_ for $field->errors; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
return \%output; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub render_text |
86
|
|
|
|
|
|
|
{ |
87
|
|
|
|
|
|
|
my ( $self, $field ) = @_; |
88
|
|
|
|
|
|
|
my %output = ( |
89
|
|
|
|
|
|
|
value => $field->fif |
90
|
|
|
|
|
|
|
); |
91
|
|
|
|
|
|
|
$output{size} = $field->size if $field->size; |
92
|
|
|
|
|
|
|
$output{maxlength} = $field->maxlength if $field->maxlength; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
return \%output; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub render_password |
98
|
|
|
|
|
|
|
{ |
99
|
|
|
|
|
|
|
my ( $self, $field ) = @_; |
100
|
|
|
|
|
|
|
return $self->render_text($field); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub render_hidden |
104
|
|
|
|
|
|
|
{ |
105
|
|
|
|
|
|
|
my ( $self, $field ) = @_; |
106
|
|
|
|
|
|
|
return { |
107
|
|
|
|
|
|
|
value => $field->fif |
108
|
|
|
|
|
|
|
}; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub render_select |
112
|
|
|
|
|
|
|
{ |
113
|
|
|
|
|
|
|
my ( $self, $field ) = @_; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
my %output = ( |
116
|
|
|
|
|
|
|
options => { option => [] } |
117
|
|
|
|
|
|
|
); |
118
|
|
|
|
|
|
|
$output{size} = $field->size if $field->size; |
119
|
|
|
|
|
|
|
$output{multiple} = $field->multiple == 1; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
my $index = 0; |
122
|
|
|
|
|
|
|
foreach my $opt ( $field->options ) |
123
|
|
|
|
|
|
|
{ |
124
|
|
|
|
|
|
|
my %option = ( |
125
|
|
|
|
|
|
|
id => $field->id . ".$index", |
126
|
|
|
|
|
|
|
value => $opt->{value}, |
127
|
|
|
|
|
|
|
label => $opt->{label}, |
128
|
|
|
|
|
|
|
); |
129
|
|
|
|
|
|
|
if ($field->fif) |
130
|
|
|
|
|
|
|
{ |
131
|
|
|
|
|
|
|
if ( $field->multiple == 1 ) |
132
|
|
|
|
|
|
|
{ |
133
|
|
|
|
|
|
|
my @fif; |
134
|
|
|
|
|
|
|
if( ref $field->fif ){ |
135
|
|
|
|
|
|
|
@fif = @{ $field->fif }; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
else{ |
138
|
|
|
|
|
|
|
@fif = ( $field->fif ); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
foreach my $optval ( @fif ) |
141
|
|
|
|
|
|
|
{ |
142
|
|
|
|
|
|
|
if ($optval == $opt->{value}) { |
143
|
|
|
|
|
|
|
$option{selected} = 1; |
144
|
|
|
|
|
|
|
last; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
else |
149
|
|
|
|
|
|
|
{ |
150
|
|
|
|
|
|
|
$option{selected} = 1 |
151
|
|
|
|
|
|
|
if $opt->{value} eq $field->fif; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
push @{ $output{options}{option} }, \%option; |
155
|
|
|
|
|
|
|
$index++; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
return \%output; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub render_checkbox |
161
|
|
|
|
|
|
|
{ |
162
|
|
|
|
|
|
|
my ( $self, $field ) = @_; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
my %output = ( |
165
|
|
|
|
|
|
|
value => $field->fif |
166
|
|
|
|
|
|
|
); |
167
|
|
|
|
|
|
|
$output{checkbox_value} = $field->checkbox_value if $field->checkbox_value; |
168
|
|
|
|
|
|
|
$output{checked} = 1 if $field->fif eq $field->checkbox_value; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
return \%output; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub render_radio_group |
175
|
|
|
|
|
|
|
{ |
176
|
|
|
|
|
|
|
my ( $self, $field ) = @_; |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
my %output = ( |
179
|
|
|
|
|
|
|
options => { option => [] }, |
180
|
|
|
|
|
|
|
value => $field->fif, |
181
|
|
|
|
|
|
|
); |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
my $index = 0; |
184
|
|
|
|
|
|
|
foreach my $opt ( $field->options ) |
185
|
|
|
|
|
|
|
{ |
186
|
|
|
|
|
|
|
my %option = ( |
187
|
|
|
|
|
|
|
id => $field->id . ".$index", |
188
|
|
|
|
|
|
|
value => $opt->{value}, |
189
|
|
|
|
|
|
|
label => $opt->{label}, |
190
|
|
|
|
|
|
|
); |
191
|
|
|
|
|
|
|
$option{checked} = 1 if $opt->{value} eq $field->fif; |
192
|
|
|
|
|
|
|
$index++; |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
return \%output; |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
sub render_textarea |
198
|
|
|
|
|
|
|
{ |
199
|
|
|
|
|
|
|
my ( $self, $field ) = @_; |
200
|
|
|
|
|
|
|
return { |
201
|
|
|
|
|
|
|
value => $field->fif || '', |
202
|
|
|
|
|
|
|
cols => $field->cols || 10, |
203
|
|
|
|
|
|
|
rows => $field->rows || 5, |
204
|
|
|
|
|
|
|
}; |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
sub render_compound |
208
|
|
|
|
|
|
|
{ |
209
|
|
|
|
|
|
|
my ( $self, $field ) = @_; |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
my %output = ( |
212
|
|
|
|
|
|
|
field => [] |
213
|
|
|
|
|
|
|
); |
214
|
|
|
|
|
|
|
foreach my $subfield ($field->sorted_fields) |
215
|
|
|
|
|
|
|
{ |
216
|
|
|
|
|
|
|
push @{ $output{field} }, $self->render_field($subfield); |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
return \%output; |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
sub render_submit |
222
|
|
|
|
|
|
|
{ |
223
|
|
|
|
|
|
|
my ( $self, $field ) = @_; |
224
|
|
|
|
|
|
|
return { |
225
|
|
|
|
|
|
|
value => $field->fif || $field->value || '', |
226
|
|
|
|
|
|
|
}; |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=head1 AUTHOR |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
Michael Nachbaur, C<< <mike at nachbaur.com> >> |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=head1 BUGS |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
Please report any bugs or feature requests to C<bug-html-formhandler-render-hash at rt.cpan.org>, or through |
236
|
|
|
|
|
|
|
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTML-FormHandler-Render-Hash>. I will be notified, and then you'll |
237
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=head1 SUPPORT |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
perldoc HTML::FormHandler::Render::Hash |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
You can also look for information at: |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=over 4 |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=HTML-FormHandler-Render-Hash> |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
L<http://annocpan.org/dist/HTML-FormHandler-Render-Hash> |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=item * CPAN Ratings |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
L<http://cpanratings.perl.org/d/HTML-FormHandler-Render-Hash> |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=item * Search CPAN |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
L<http://search.cpan.org/dist/HTML-FormHandler-Render-Hash/> |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=item * Source code access |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
L<http://github.com/NachoMan/HTML-FormHandler-Render-Hash/> |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=back |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
Copyright 2009 Michael Nachbaur. |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
280
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
281
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
=cut |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
1; # End of HTML::FormHandler::Render::Hash |