PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/HTML/FormHandler/Widget/Wrapper/Simple.pm

http://github.com/gshank/html-formhandler
Perl | 113 lines | 75 code | 24 blank | 14 comment | 15 complexity | b060d4570ee6e39ceede20c00ff3450c MD5 | raw file
  1package HTML::FormHandler::Widget::Wrapper::Simple;
  2# ABSTRACT: simple field wrapper
  3
  4use Moose::Role;
  5use namespace::autoclean;
  6use HTML::FormHandler::Render::Util ('process_attrs');
  7
  8with 'HTML::FormHandler::Widget::Wrapper::Base';
  9
 10=head1 SYNOPSIS
 11
 12This is the default wrapper role. It will be installed if
 13no other wrapper is specified and widget_wrapper is not set to
 14'none'.
 15
 16Relevant field flags:
 17
 18   do_wrapper
 19   do_label
 20
 21If 'do_label' is set and not 'do_wrapper', only the label plus
 22the form element will be rendered.
 23
 24Supported 'tags', all set via the 'tags' hashref on the field:
 25
 26    wrapper_tag    -- the tag to use in the wrapper, default 'div'
 27
 28    label_tag      -- tag to use for label (default 'label')
 29    label_after    -- string to append to label, for example ': ' to append a colon
 30
 31    before_element -- string that goes right before the element
 32    after_element  -- string that goes right after the element
 33
 34    no_errors      -- don't issue error messages on the field
 35    error_class    -- class for error messages (default 'error_message')
 36    warning_class  -- class for warning messages (default 'warning_message' )
 37
 38    no_wrapped_label -- for checkboxes. Don't provide an inner wrapped label
 39                        (from Base wrapper)
 40
 41Example:
 42
 43    has_field 'foo' => ( tags => { wrapper_tag => 'span', no_errors => 1 } );
 44
 45=cut
 46
 47
 48sub wrap_field {
 49    my ( $self, $result, $rendered_widget ) = @_;
 50
 51    my $output;
 52    # get wrapper tag if set
 53    my $label_tag = $self->label_tag || '';
 54    my $wrapper_tag;
 55    if( $self->do_wrapper ) {
 56        $output .= $self->get_tag('before_wrapper');
 57        $wrapper_tag = $self->get_tag('wrapper_tag');
 58        # default wrapper tags
 59        $wrapper_tag ||= $self->has_flag('is_repeatable') ? 'fieldset' : 'div';
 60        # get attribute string
 61        my $attrs = process_attrs( $self->wrapper_attributes($result) );
 62        # write wrapper tag
 63        $output .= qq{\n<$wrapper_tag$attrs>};
 64        $label_tag = 'legend' if $wrapper_tag eq 'fieldset';
 65    }
 66    # write label; special processing for checkboxes
 67    $rendered_widget = $self->wrap_checkbox($result, $rendered_widget)
 68        if ( lc $self->widget eq 'checkbox' );
 69    $output .= "\n" . $self->do_render_label($result, $label_tag)
 70        if $self->do_label;
 71    # append 'before_element'
 72    $output .= $self->get_tag('before_element');
 73
 74    # start controls div
 75    if ( $self->get_tag('controls_div') ) {
 76        $output .= qq{\n<div class="controls">};
 77    }
 78    elsif ( $self->has_element_wrapper_class ) {
 79        my $ew_attr = $self->element_wrapper_attributes($result);
 80        my $element_wrapper_attrs =  process_attrs( $ew_attr );
 81        $output .= qq{\n<div$element_wrapper_attrs>};
 82    }
 83
 84    # the input element itself
 85    $output .= "\n$rendered_widget";
 86
 87    # close controls div
 88    if ( $self->get_tag('controls_div') || $self->has_element_wrapper_class ) {
 89        # end control div
 90        $output .= "\n</div>";
 91    }
 92
 93    # the 'after_element'
 94    $output .= $self->get_tag('after_element');
 95    # the error messages
 96    unless( $self->get_tag('no_errors') ) {
 97        my $error_class = $self->get_tag('error_class') || 'error_message';
 98        $output .= qq{\n<span class="$error_class">$_</span>}
 99            for $result->all_errors;
100        # warnings (incompletely implemented - only on field itself)
101        my $warning_class = $self->get_tag('warning_class') || 'warning_message';
102        $output .= qq{\n<span class="warning_message">$_</span>}
103            for $result->all_warnings;
104    }
105    if( $self->do_wrapper ) {
106        $output .= "\n</$wrapper_tag>";
107        $output .= $self->get_tag('after_wrapper');
108    }
109    return "$output";
110}
111
112
1131;