1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
package HTML::FormHandler::Widget::Wrapper::Simple;
# ABSTRACT: simple field wrapper
use Moose::Role;
use namespace::autoclean;
use HTML::FormHandler::Render::Util ('process_attrs');
with 'HTML::FormHandler::Widget::Wrapper::Base';
sub wrap_field {
my ( $self, $result, $rendered_widget ) = @_;
my $output;
# get wrapper tag if set
my $label_tag = $self->label_tag || '';
my $wrapper_tag;
if( $self->do_wrapper ) {
$output .= $self->get_tag('before_wrapper');
$wrapper_tag = $self->get_tag('wrapper_tag');
# default wrapper tags
$wrapper_tag ||= $self->has_flag('is_repeatable') ? 'fieldset' : 'div';
# get attribute string
my $attrs = process_attrs( $self->wrapper_attributes($result) );
# write wrapper tag
$output .= qq{\n<$wrapper_tag$attrs>};
$label_tag = 'legend' if $wrapper_tag eq 'fieldset';
}
# write label; special processing for checkboxes
$rendered_widget = $self->wrap_checkbox($result, $rendered_widget)
if ( lc $self->widget eq 'checkbox' );
$output .= "\n" . $self->do_render_label($result, $label_tag)
if $self->do_label;
# append 'before_element'
$output .= $self->get_tag('before_element');
# start controls div
if ( $self->get_tag('controls_div') ) {
$output .= qq{\n<div class="controls">};
}
elsif ( $self->has_element_wrapper_class ) {
my $ew_attr = $self->element_wrapper_attributes($result);
my $element_wrapper_attrs = process_attrs( $ew_attr );
$output .= qq{\n<div$element_wrapper_attrs>};
}
# the input element itself
$output .= "\n$rendered_widget";
# close controls div
if ( $self->get_tag('controls_div') || $self->has_element_wrapper_class ) {
# end control div
$output .= "\n</div>";
}
# the 'after_element'
$output .= $self->get_tag('after_element');
# the error messages
unless( $self->get_tag('no_errors') ) {
my $error_class = $self->get_tag('error_class') || 'error_message';
$output .= qq{\n<span class="$error_class">$_</span>}
for $result->all_errors;
# warnings (incompletely implemented - only on field itself)
my $warning_class = $self->get_tag('warning_class') || 'warning_message';
$output .= qq{\n<span class="warning_message">$_</span>}
for $result->all_warnings;
}
if( $self->do_wrapper ) {
$output .= "\n</$wrapper_tag>";
$output .= $self->get_tag('after_wrapper');
}
return "$output";
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
HTML::FormHandler::Widget::Wrapper::Simple - simple field wrapper
=head1 VERSION
version 0.40057
=head1 SYNOPSIS
This is the default wrapper role. It will be installed if
no other wrapper is specified and widget_wrapper is not set to
'none'.
Relevant field flags:
do_wrapper
do_label
If 'do_label' is set and not 'do_wrapper', only the label plus
the form element will be rendered.
Supported 'tags', all set via the 'tags' hashref on the field:
wrapper_tag -- the tag to use in the wrapper, default 'div'
label_tag -- tag to use for label (default 'label')
label_after -- string to append to label, for example ': ' to append a colon
before_element -- string that goes right before the element
after_element -- string that goes right after the element
no_errors -- don't issue error messages on the field
error_class -- class for error messages (default 'error_message')
warning_class -- class for warning messages (default 'warning_message' )
no_wrapped_label -- for checkboxes. Don't provide an inner wrapped label
(from Base wrapper)
Example:
has_field 'foo' => ( tags => { wrapper_tag => 'span', no_errors => 1 } );
=head1 AUTHOR
FormHandler Contributors - see HTML::FormHandler
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Gerda Shank.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|