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 140 141 142 143 144
|
package HTML::FormHandler::Result::Role;
# ABSTRACT: role with common code for form & field results
use Moose::Role;
has 'name' => ( isa => 'Str', is => 'rw', required => 1 );
# do we need 'accessor' ?
has 'parent' => ( is => 'rw', weak_ref => 1 );
has 'input' => (
is => 'ro',
clearer => '_clear_input',
writer => '_set_input',
predicate => 'has_input',
);
has '_results' => (
traits => ['Array'],
isa => 'ArrayRef[HTML::FormHandler::Field::Result]',
is => 'rw',
default => sub { [] },
handles => {
results => 'elements',
add_result => 'push',
num_results => 'count',
has_results => 'count',
clear_results => 'clear',
find_result_index => 'first_index',
set_result_at_index => 'set',
}
);
has 'error_results' => (
traits => ['Array'],
isa => 'ArrayRef', # for HFH::Result and HFH::Field::Result
is => 'rw',
default => sub { [] },
handles => {
has_error_results => 'count',
num_error_results => 'count',
clear_error_results => 'clear',
add_error_result => 'push',
all_error_results => 'elements',
}
);
has 'errors' => (
traits => ['Array'],
is => 'rw',
isa => 'ArrayRef[Str]',
default => sub { [] },
handles => {
all_errors => 'elements',
_push_errors => 'push',
num_errors => 'count',
has_errors => 'count',
clear_errors => 'clear',
}
);
has 'warnings' => (
traits => ['Array'],
is => 'rw',
isa => 'ArrayRef[Str]',
default => sub { [] },
handles => {
all_warnings => 'elements',
add_warning => 'push',
num_warnings => 'count',
has_warnings => 'count',
clear_warnings => 'clear',
}
);
sub validated { !$_[0]->has_error_results && $_[0]->has_input }
sub is_valid { shift->validated }
# this ought to be named 'result' for consistency,
# but the result objects are named 'result'.
# also providing 'field' method for compatibility
sub get_result {
my ( $self, $name, $die ) = @_;
my $index;
# if this is a full_name for a compound field
# walk through the fields to get to it
if ( $name =~ /\./ ) {
my @names = split /\./, $name;
my $result = $self;
foreach my $rname (@names) {
$result = $result->get_result($rname);
return unless $result
}
return $result;
}
else # not a compound name
{
for my $result ( $self->results ) {
return $result if ( $result->name eq $name );
}
}
return unless $die;
die "Field '$name' not found in '$self'";
}
sub field { shift->get_result(@_) }
use namespace::autoclean;
1;
__END__
=pod
=head1 NAME
HTML::FormHandler::Result::Role - role with common code for form & field results
=head1 VERSION
version 0.40013
=head1 SYNOPSIS
Role to hold common result attributes for L<HTML::FormHandler::Result>
and L<HTML::FormHandler::Result::Field>.
=head1 NAME
HTML::FormHandler::Result::Role - common code for form & field results
=head1 AUTHOR
FormHandler Contributors - see HTML::FormHandler
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2012 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
|