| 1 |
package HTML::FormHandler::Render::Simple; |
| 2 |
# ABSTRACT: simple rendering role |
| 3 |
$HTML::FormHandler::Render::Simple::VERSION = '0.410002'; |
| 4 |
use Moose::Role; |
| 5 |
|
| 6 |
requires( 'sorted_fields', 'field' ); |
| 7 |
|
| 8 |
use HTML::FormHandler::Render::Util ('process_attrs', 'ucc_widget'); |
| 9 |
use HTML::Entities qw( encode_entities ); |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
sub render { |
| 14 |
my $self = shift; |
| 15 |
my $output = $self->render_start; |
| 16 |
|
| 17 |
$output .= $self->render_form_errors; |
| 18 |
|
| 19 |
foreach my $field ( $self->sorted_fields ) { |
| 20 |
$output .= $self->render_field($field); |
| 21 |
} |
| 22 |
|
| 23 |
$output .= $self->render_end; |
| 24 |
return $output; |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
sub render_form_errors { |
| 29 |
my $self = shift; |
| 30 |
|
| 31 |
return '' unless $self->has_form_errors; |
| 32 |
my $output = "\n<div class=\"form_errors\">"; |
| 33 |
$output .= qq{\n<span class="error_message">$_</span>} |
| 34 |
for map { encode_entities($_) } $self->all_form_errors; |
| 35 |
$output .= "\n</div>"; |
| 36 |
return $output; |
| 37 |
} |
| 38 |
|
| 39 |
sub render_field { |
| 40 |
my ( $self, $field ) = @_; |
| 41 |
|
| 42 |
if ( ref( \$field ) eq 'SCALAR' ) { |
| 43 |
$field = $self->field($field); |
| 44 |
} |
| 45 |
die "must pass field to render_field" |
| 46 |
unless ( defined $field && $field->isa('HTML::FormHandler::Field') ); |
| 47 |
# widgets should be in camel case, since they are Perl package names |
| 48 |
my $widget = ucc_widget($field->widget); |
| 49 |
return '' if $widget eq 'no_render'; |
| 50 |
my $rendered_field; |
| 51 |
my $form_render = 'render_' . $widget; |
| 52 |
if ( $self->can($form_render) ) { |
| 53 |
$rendered_field = $self->$form_render($field); |
| 54 |
} |
| 55 |
elsif ( $field->can('render') ) { |
| 56 |
$rendered_field = $field->render; |
| 57 |
} |
| 58 |
else { |
| 59 |
die "No widget method found for '$widget' in H::F::Render::Simple"; |
| 60 |
} |
| 61 |
return $self->wrap_field( $field, $rendered_field ); |
| 62 |
} |
| 63 |
|
| 64 |
sub wrap_field { |
| 65 |
my ( $self, $field, $rendered_field ) = @_; |
| 66 |
|
| 67 |
return "\n$rendered_field" if $field->uwrapper eq 'none'; |
| 68 |
return "\n$rendered_field" if ! $field->do_wrapper; |
| 69 |
|
| 70 |
my $output = "\n"; |
| 71 |
|
| 72 |
my $wrapper_tag = $field->get_tag('wrapper_tag'); |
| 73 |
$wrapper_tag ||= $field->has_flag('is_repeatable') ? 'fieldset' : 'div'; |
| 74 |
my $attrs = process_attrs($field->wrapper_attributes); |
| 75 |
|
| 76 |
$output .= qq{<$wrapper_tag$attrs>}; |
| 77 |
if( $wrapper_tag eq 'fieldset' ) { |
| 78 |
$output .= '<legend>' . $field->loc_label . '</legend>'; |
| 79 |
} |
| 80 |
elsif ( ! $field->get_tag('label_none') && $field->do_label && length( $field->label ) > 0 ) { |
| 81 |
$output .= "\n" . $self->render_label($field); |
| 82 |
} |
| 83 |
|
| 84 |
$output .= "\n$rendered_field"; |
| 85 |
$output .= qq{\n<span class="error_message">$_</span>} |
| 86 |
for map { encode_entities($_) } $field->all_errors; |
| 87 |
|
| 88 |
$output .= "\n</$wrapper_tag>"; |
| 89 |
|
| 90 |
return "$output"; |
| 91 |
} |
| 92 |
|
| 93 |
sub render_text { |
| 94 |
my ( $self, $field ) = @_; |
| 95 |
my $output = '<input type="' . $field->input_type . '" name="'; |
| 96 |
$output .= $field->html_name . '"'; |
| 97 |
$output .= ' id="' . $field->id . '"'; |
| 98 |
$output .= ' size="' . $field->size . '"' if $field->size; |
| 99 |
$output .= ' maxlength="' . $field->maxlength . '"' if $field->maxlength; |
| 100 |
$output .= ' value="' . $field->html_filter($field->fif) . '"'; |
| 101 |
$output .= process_attrs($field->element_attributes); |
| 102 |
$output .= ' />'; |
| 103 |
return $output; |
| 104 |
} |
| 105 |
|
| 106 |
sub render_password { |
| 107 |
my ( $self, $field ) = @_; |
| 108 |
my $output = '<input type="password" name="'; |
| 109 |
$output .= $field->html_name . '"'; |
| 110 |
$output .= ' id="' . $field->id . '"'; |
| 111 |
$output .= ' size="' . $field->size . '"' if $field->size; |
| 112 |
$output .= ' maxlength="' . $field->maxlength . '"' if $field->maxlength; |
| 113 |
$output .= ' value="' . $field->html_filter($field->fif) . '"'; |
| 114 |
$output .= process_attrs($field->element_attributes); |
| 115 |
$output .= ' />'; |
| 116 |
return $output; |
| 117 |
} |
| 118 |
|
| 119 |
sub render_hidden { |
| 120 |
my ( $self, $field ) = @_; |
| 121 |
my $output = '<input type="hidden" name="'; |
| 122 |
$output .= $field->html_name . '"'; |
| 123 |
$output .= ' id="' . $field->id . '"'; |
| 124 |
$output .= ' value="' . $field->html_filter($field->fif) . '"'; |
| 125 |
$output .= process_attrs($field->element_attributes); |
| 126 |
$output .= ' />'; |
| 127 |
return $output; |
| 128 |
} |
| 129 |
|
| 130 |
sub render_select { |
| 131 |
my ( $self, $field ) = @_; |
| 132 |
|
| 133 |
my $multiple = $field->multiple; |
| 134 |
my $id = $field->id; |
| 135 |
my $output = '<select name="' . $field->html_name . '"'; |
| 136 |
$output .= qq{ id="$id"}; |
| 137 |
$output .= ' multiple="multiple"' if $multiple == 1; |
| 138 |
$output .= ' size="' . $field->size . '"' if $field->size; |
| 139 |
my $html_attributes = process_attrs($field->element_attributes); |
| 140 |
$output .= $html_attributes; |
| 141 |
$output .= '>'; |
| 142 |
my $index = 0; |
| 143 |
if( defined $field->empty_select ) { |
| 144 |
$output .= '<option value="">' . $field->_localize($field->empty_select) . '</option>'; |
| 145 |
} |
| 146 |
my $fif = $field->fif; |
| 147 |
my %fif_lookup; |
| 148 |
@fif_lookup{@$fif} = () if $multiple; |
| 149 |
foreach my $option ( @{ $field->{options} } ) { |
| 150 |
my $value = $option->{value}; |
| 151 |
$output .= '<option value="' |
| 152 |
. $field->html_filter($value) |
| 153 |
. qq{" id="$id.$index"}; |
| 154 |
if( defined $option->{disabled} && $option->{disabled} ) { |
| 155 |
$output .= ' disabled="disabled"'; |
| 156 |
} |
| 157 |
if ( defined $fif ) { |
| 158 |
if ( $multiple && exists $fif_lookup{$value} ) { |
| 159 |
$output .= ' selected="selected"'; |
| 160 |
} |
| 161 |
elsif ( $fif eq $value ) { |
| 162 |
$output .= ' selected="selected"'; |
| 163 |
} |
| 164 |
} |
| 165 |
$output .= $html_attributes; |
| 166 |
my $label = $option->{label}; |
| 167 |
$label = $field->_localize($label) if $field->localize_labels; |
| 168 |
$output .= '>' . ( $field->html_filter($label) ) . '</option>'; |
| 169 |
$index++; |
| 170 |
} |
| 171 |
$output .= '</select>'; |
| 172 |
return $output; |
| 173 |
} |
| 174 |
|
| 175 |
sub render_checkbox { |
| 176 |
my ( $self, $field ) = @_; |
| 177 |
|
| 178 |
my $output = '<input type="checkbox" name="' . $field->html_name . '"'; |
| 179 |
$output .= ' id="' . $field->id . '"'; |
| 180 |
$output .= ' value="' . $field->html_filter($field->checkbox_value) . '"'; |
| 181 |
$output .= ' checked="checked"' if $field->fif eq $field->checkbox_value; |
| 182 |
$output .= process_attrs($field->element_attributes); |
| 183 |
$output .= ' />'; |
| 184 |
return $output; |
| 185 |
} |
| 186 |
|
| 187 |
sub render_radio_group { |
| 188 |
my ( $self, $field ) = @_; |
| 189 |
|
| 190 |
my $output = " <br />"; |
| 191 |
my $index = 0; |
| 192 |
foreach my $option ( @{ $field->options } ) { |
| 193 |
my $id = $field->id . ".$index"; |
| 194 |
$output .= qq{<label for="$id"><input type="radio" value="} . $field->html_filter($option->{value}) . '"'; |
| 195 |
$output .= ' name="' . $field->html_name . '" id="' . "$id\""; |
| 196 |
$output .= ' checked="checked"' if $option->{value} eq $field->fif; |
| 197 |
$output .= ' />'; |
| 198 |
$output .= $field->html_filter($option->{label}) . '</label><br />'; |
| 199 |
$index++; |
| 200 |
} |
| 201 |
return $output; |
| 202 |
} |
| 203 |
|
| 204 |
sub render_textarea { |
| 205 |
my ( $self, $field ) = @_; |
| 206 |
my $fif = $field->fif || ''; |
| 207 |
my $id = $field->id; |
| 208 |
my $cols = $field->cols || 10; |
| 209 |
my $rows = $field->rows || 5; |
| 210 |
my $name = $field->html_name; |
| 211 |
|
| 212 |
my $output = |
| 213 |
qq(<textarea name="$name" id="$id" ) |
| 214 |
. process_attrs($field->element_attributes) |
| 215 |
. qq(rows="$rows" cols="$cols">) |
| 216 |
. $field->html_filter($fif) |
| 217 |
. q(</textarea>); |
| 218 |
|
| 219 |
return $output; |
| 220 |
} |
| 221 |
|
| 222 |
sub render_upload { |
| 223 |
my ( $self, $field ) = @_; |
| 224 |
|
| 225 |
my $output; |
| 226 |
$output = '<input type="file" name="'; |
| 227 |
$output .= $field->html_name . '"'; |
| 228 |
$output .= ' id="' . $field->id . '"'; |
| 229 |
$output .= process_attrs($field->element_attributes); |
| 230 |
$output .= ' />'; |
| 231 |
return $output; |
| 232 |
} |
| 233 |
|
| 234 |
sub render_label { |
| 235 |
my ( $self, $field ) = @_; |
| 236 |
|
| 237 |
my $attrs = process_attrs( $field->label_attributes ); |
| 238 |
my $label = $field->html_filter($field->loc_label); |
| 239 |
$label .= $field->get_tag('label_after') |
| 240 |
if( $field->tag_exists('label_after') ); |
| 241 |
my $label_tag = $field->tag_exists('label_tag') ? $field->get_tag('label_tag') : 'label'; |
| 242 |
return qq{<$label_tag$attrs for="} . $field->id . qq{">$label</$label_tag>}; |
| 243 |
} |
| 244 |
|
| 245 |
sub render_compound { |
| 246 |
my ( $self, $field ) = @_; |
| 247 |
|
| 248 |
my $output = ''; |
| 249 |
foreach my $subfield ( $field->sorted_fields ) { |
| 250 |
$output .= $self->render_field($subfield); |
| 251 |
} |
| 252 |
return $output; |
| 253 |
} |
| 254 |
|
| 255 |
sub render_submit { |
| 256 |
my ( $self, $field ) = @_; |
| 257 |
|
| 258 |
my $output = '<input type="submit" name="'; |
| 259 |
$output .= $field->html_name . '"'; |
| 260 |
$output .= ' id="' . $field->id . '"'; |
| 261 |
$output .= process_attrs($field->element_attributes); |
| 262 |
$output .= ' value="' . $field->html_filter($field->_localize($field->value)) . '" />'; |
| 263 |
return $output; |
| 264 |
} |
| 265 |
|
| 266 |
sub render_reset { |
| 267 |
my ( $self, $field ) = @_; |
| 268 |
|
| 269 |
my $output = '<input type="reset" name="'; |
| 270 |
$output .= $field->html_name . '"'; |
| 271 |
$output .= ' id="' . $field->id . '"'; |
| 272 |
$output .= process_attrs($field->element_attributes); |
| 273 |
$output .= ' value="' . $field->html_filter($field->value) . '" />'; |
| 274 |
return $output; |
| 275 |
} |
| 276 |
|
| 277 |
sub render_captcha { |
| 278 |
my ( $self, $field ) = @_; |
| 279 |
|
| 280 |
my $output = '<img src="' . $self->captcha_image_url . '"/>'; |
| 281 |
$output .= '<input id="' . $field->id . '" name="'; |
| 282 |
$output .= $field->html_name . '"/>'; |
| 283 |
return $output; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
use namespace::autoclean; |
| 288 |
1; |
| 289 |
|
| 290 |
__END__ |
| 291 |
|
| 292 |
=pod |
| 293 |
|
| 294 |
=encoding UTF-8 |
| 295 |
|
| 296 |
=head1 NAME |
| 297 |
|
| 298 |
HTML::FormHandler::Render::Simple - simple rendering role |
| 299 |
|
| 300 |
=head1 VERSION |
| 301 |
|
| 302 |
version 0.410002 |
| 303 |
|
| 304 |
=head1 SYNOPSIS |
| 305 |
|
| 306 |
This is a Moose role that is an example of a simple rendering |
| 307 |
routine for L<HTML::FormHandler>. It's here as an example of |
| 308 |
how to write a custom renderer in one package, if you prefer |
| 309 |
that to using the widgets. It won't always be updated with |
| 310 |
improvements by default, because it was becoming a lot of work to update |
| 311 |
the rendering in multiple places. |
| 312 |
|
| 313 |
For a 'MyApp::Form::Renderer' which you've created and modified, |
| 314 |
in your Form class: |
| 315 |
|
| 316 |
package MyApp::Form::Silly; |
| 317 |
use Moose; |
| 318 |
extends 'HTML::FormHandler::Model::DBIC'; |
| 319 |
with 'MyApp::Form::Renderers'; |
| 320 |
|
| 321 |
In a template: |
| 322 |
|
| 323 |
[% form.render %] |
| 324 |
|
| 325 |
The widgets are rendered with C<< $field->render >>; rendering |
| 326 |
routines from a class like this use C<< $form->render_field('field_name') >> |
| 327 |
to render individual fields: |
| 328 |
|
| 329 |
[% form.render_field( 'title' ) %] |
| 330 |
|
| 331 |
=head1 DESCRIPTION |
| 332 |
|
| 333 |
This role provides HTML output routines for the 'widget' types |
| 334 |
defined in the provided FormHandler fields. Each 'widget' name |
| 335 |
has a 'widget_$name' method here. |
| 336 |
|
| 337 |
These widget routines output strings with HTML suitable for displaying |
| 338 |
form fields. |
| 339 |
|
| 340 |
The widget for a particular field can be defined in the form. You can |
| 341 |
create additional widget routines in your form for custom widgets. |
| 342 |
|
| 343 |
The fill-in-form values ('fif') are cleaned with the 'render_filter' |
| 344 |
method of the base field class. You can change the filter to suit |
| 345 |
your own needs: see L<HTML::FormHandler::Manual::Rendering> |
| 346 |
|
| 347 |
=head2 render |
| 348 |
|
| 349 |
To render all the fields in a form in sorted order (using |
| 350 |
'sorted_fields' method). |
| 351 |
|
| 352 |
=head2 render_start, render_end |
| 353 |
|
| 354 |
These use the methods in L<HTML::FormHandler::Widget::Form::Simple> now. If |
| 355 |
you want to customize them, copy them to your own package. |
| 356 |
|
| 357 |
Will render the beginning and ending <form> tags and fieldsets. Allows for easy |
| 358 |
splitting up of the form if you want to hand-render some of the fields. |
| 359 |
|
| 360 |
[% form.render_start %] |
| 361 |
[% form.render_field('title') %] |
| 362 |
<insert specially rendered field> |
| 363 |
[% form.render_field('some_field') %] |
| 364 |
[% form.render_end %] |
| 365 |
|
| 366 |
=head2 render_field |
| 367 |
|
| 368 |
Render a field passing in a field object or a field name |
| 369 |
|
| 370 |
$form->render_field( $field ) |
| 371 |
$form->render_field( 'title' ) |
| 372 |
|
| 373 |
=head2 render_text |
| 374 |
|
| 375 |
Output an HTML string for a text widget |
| 376 |
|
| 377 |
=head2 render_password |
| 378 |
|
| 379 |
Output an HTML string for a password widget |
| 380 |
|
| 381 |
=head2 render_hidden |
| 382 |
|
| 383 |
Output an HTML string for a hidden input widget |
| 384 |
|
| 385 |
=head2 render_select |
| 386 |
|
| 387 |
Output an HTML string for a 'select' widget, single or multiple |
| 388 |
|
| 389 |
=head2 render_checkbox |
| 390 |
|
| 391 |
Output an HTML string for a 'checkbox' widget |
| 392 |
|
| 393 |
=head2 render_radio_group |
| 394 |
|
| 395 |
Output an HTML string for a 'radio_group' selection widget. |
| 396 |
This widget should be for a field that inherits from 'Select', |
| 397 |
since it requires the existence of an 'options' array. |
| 398 |
|
| 399 |
=head2 render_textarea |
| 400 |
|
| 401 |
Output an HTML string for a textarea widget |
| 402 |
|
| 403 |
=head2 render_compound |
| 404 |
|
| 405 |
Renders field with 'compound' widget |
| 406 |
|
| 407 |
=head2 render_submit |
| 408 |
|
| 409 |
Renders field with 'submit' widget |
| 410 |
|
| 411 |
=head1 AUTHOR |
| 412 |
|
| 413 |
FormHandler Contributors - see HTML::FormHandler |
| 414 |
|
| 415 |
=head1 COPYRIGHT AND LICENSE |
| 416 |
|
| 417 |
This software is copyright (c) 2017 by Gerda Shank. |
| 418 |
|
| 419 |
This is free software; you can redistribute it and/or modify it under |
| 420 |
the same terms as the Perl 5 programming language system itself. |
| 421 |
|
| 422 |
=cut |