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.40067';
4 1     1   721 use HTML::FormHandler::Moose;
  1         2  
  1         8  
5             extends 'HTML::FormHandler::Field::Display';
6 1     1   1494 use HTML::FormHandler::Render::Util ('process_attrs');
  1         2  
  1         6  
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   2 my ( $self, $result ) = @_;
16 1   33     29 $result ||= $self->result;
17              
18 1         25 my $rep_field = $self->parent->field($self->repeatable);
19 1 50       3 die "Invalid repeatable name in field " . $self->name unless $rep_field;
20 1         40 my $value = $self->html_filter($self->_localize($self->value));
21 1         9 my $attrs = $self->element_attributes($result);
22 1         2 push @{$attrs->{class}}, ( 'add_element', 'btn' );
  1         4  
23 1         30 $attrs->{'data-rep-id'} = $rep_field->id;
24 1         27 $attrs->{id} = $self->id;
25 1         6 my $attr_str = process_attrs($attrs);
26 1   50     9 my $wrapper_tag = $self->get_tag('wrapper_tag') || 'div';
27 1         4 my $output = qq{<$wrapper_tag$attr_str>$value</$wrapper_tag>};
28 1         24 $output = $self->wrap_field($self->result, $output);
29 1         5 return $output;
30 1     1 0 37 };
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.40067
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) 2016 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