File Coverage

blib/lib/HTML/FormHandler/Field/AddElement.pm
Criterion Covered Total %
statement 22 22 100.0
branch 1 2 50.0
condition 2 5 40.0
subroutine 4 4 100.0
pod 0 1 0.0
total 29 34 85.2


line stmt bran cond sub pod time code
1             package HTML::FormHandler::Field::AddElement;
2             # ABSTRACT: Field to support repeatable javascript add
3             $HTML::FormHandler::Field::AddElement::VERSION = '0.40068';
4 1     1   883 use HTML::FormHandler::Moose;
  1         2  
  1         10  
5             extends 'HTML::FormHandler::Field::Display';
6 1     1   2876 use HTML::FormHandler::Render::Util ('process_attrs');
  1         6  
  1         12  
7              
8              
9             has 'repeatable' => ( is => 'rw', isa => 'Str', required => 1 );
10             has '+do_wrapper' => ( default => 1 );
11             has '+value' => ( default => 'Add Element' );
12              
13             sub build_render_method {
14             return sub {
15 1     1   3 my ( $self, $result ) = @_;
16 1   33     33 $result ||= $self->result;
17              
18 1         28 my $rep_field = $self->parent->field($self->repeatable);
19 1 50       7 die "Invalid repeatable name in field " . $self->name unless $rep_field;
20 1         54 my $value = $self->html_filter($self->_localize($self->value));
21 1         9 my $attrs = $self->element_attributes($result);
22 1         3 push @{$attrs->{class}}, ( 'add_element', 'btn' );
  1         5  
23 1         31 $attrs->{'data-rep-id'} = $rep_field->id;
24 1         28 $attrs->{id} = $self->id;
25 1         5 my $attr_str = process_attrs($attrs);
26 1   50     8 my $wrapper_tag = $self->get_tag('wrapper_tag') || 'div';
27 1         6 my $output = qq{<$wrapper_tag$attr_str>$value</$wrapper_tag>};
28 1         30 $output = $self->wrap_field($self->result, $output);
29 1         7 return $output;
30 1     1 0 40 };
31             }
32              
33             1;
34              
35             __END__
36              
37             =pod
38              
39             =encoding UTF-8
40              
41             =head1 NAME
42              
43             HTML::FormHandler::Field::AddElement - Field to support repeatable javascript add
44              
45             =head1 VERSION
46              
47             version 0.40068
48              
49             =head1 SYNOPSIS
50              
51             EXAMPLE field for rendering an AddElement field for
52             doing javascript additions of repeatable elements.
53              
54             You probably want to make your own.
55              
56             The main requirements are that the button have 1) the
57             'add_element' class, 2) a 'data-rep-id' attribute that
58             contains the id of the repeatable to which you want to
59             add an element.
60              
61             =head1 NAME
62              
63             HTML::FormHandler::Field::AddElement
64              
65             =head1 ATTRIBUTES
66              
67             has_field 'add_element' => ( type => 'AddElement', repeatable => 'foo',
68             value => 'Add another foo',
69             );
70              
71             =head2 repeatable
72              
73             Requires the name of a Repeatable sibling field.
74              
75             =head2 value
76              
77             The value of the button that's rendered, 'Add Element' by default.
78              
79             =head1 AUTHOR
80              
81             FormHandler Contributors - see HTML::FormHandler
82              
83             =head1 COPYRIGHT AND LICENSE
84              
85             This software is copyright (c) 2017 by Gerda Shank.
86              
87             This is free software; you can redistribute it and/or modify it under
88             the same terms as the Perl 5 programming language system itself.
89              
90             =cut