| 1 |
package HTML::FormHandler::Field::AddElement; |
| 2 |
# ABSTRACT: Field to support repeatable javascript add |
| 3 |
$HTML::FormHandler::Field::AddElement::VERSION = '0.410002'; |
| 4 |
use HTML::FormHandler::Moose; |
| 5 |
extends 'HTML::FormHandler::Field::Display'; |
| 6 |
use HTML::FormHandler::Render::Util ('process_attrs'); |
| 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 |
my ( $self, $result ) = @_; |
| 16 |
$result ||= $self->result; |
| 17 |
|
| 18 |
my $rep_field = $self->parent->field($self->repeatable); |
| 19 |
die "Invalid repeatable name in field " . $self->name unless $rep_field; |
| 20 |
my $value = $self->html_filter($self->_localize($self->value)); |
| 21 |
my $attrs = $self->element_attributes($result); |
| 22 |
push @{$attrs->{class}}, ( 'add_element', 'btn' ); |
| 23 |
$attrs->{'data-rep-id'} = $rep_field->id; |
| 24 |
$attrs->{id} = $self->id; |
| 25 |
my $attr_str = process_attrs($attrs); |
| 26 |
my $wrapper_tag = $self->get_tag('wrapper_tag') || 'div'; |
| 27 |
my $output = qq{<$wrapper_tag$attr_str>$value</$wrapper_tag>}; |
| 28 |
$output = $self->wrap_field($self->result, $output); |
| 29 |
return $output; |
| 30 |
}; |
| 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.410002 |
| 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 |