Debian Codegraph @

lib/HTML/FormHandler/Result/Role.pm

raw
1 package HTML::FormHandler::Result::Role;
2 # ABSTRACT: role with common code for form & field results
3 $HTML::FormHandler::Result::Role::VERSION = '0.410002';
4 use Moose::Role;
5
6
7 has 'name' => ( isa => 'Str', is => 'rw', required => 1 );
8
9 # do we need 'accessor' ?
10 has 'parent' => ( is => 'rw', weak_ref => 1 );
11
12 has 'input' => (
13 is => 'ro',
14 clearer => '_clear_input',
15 writer => '_set_input',
16 predicate => 'has_input',
17 );
18
19 has '_results' => (
20 traits => ['Array'],
21 isa => 'ArrayRef[HTML::FormHandler::Field::Result]',
22 is => 'rw',
23 default => sub { [] },
24 handles => {
25 results => 'elements',
26 add_result => 'push',
27 num_results => 'count',
28 has_results => 'count',
29 clear_results => 'clear',
30 find_result_index => 'first_index',
31 set_result_at_index => 'set',
32 _pop_result => 'pop',
33 }
34 );
35
36 has 'error_results' => (
37 traits => ['Array'],
38 isa => 'ArrayRef', # for HFH::Result and HFH::Field::Result
39 is => 'rw',
40 default => sub { [] },
41 handles => {
42 has_error_results => 'count',
43 num_error_results => 'count',
44 clear_error_results => 'clear',
45 add_error_result => 'push',
46 all_error_results => 'elements',
47 }
48 );
49
50 sub errors_by_id {
51 my $self = shift;
52 my %errors;
53 $errors{$_->field_def->id} = [$_->all_errors] for $self->all_error_results;
54 return \%errors;
55 }
56
57 sub errors_by_name {
58 my $self = shift;
59 my %errors;
60 $errors{$_->field_def->html_name} = [$_->all_errors] for $self->all_error_results;
61 return \%errors;
62 }
63
64 has 'errors' => (
65 traits => ['Array'],
66 is => 'rw',
67 isa => 'ArrayRef[Str]',
68 default => sub { [] },
69 handles => {
70 all_errors => 'elements',
71 _push_errors => 'push',
72 num_errors => 'count',
73 has_errors => 'count',
74 clear_errors => 'clear',
75 }
76 );
77
78 has 'warnings' => (
79 traits => ['Array'],
80 is => 'rw',
81 isa => 'ArrayRef[Str]',
82 default => sub { [] },
83 handles => {
84 all_warnings => 'elements',
85 add_warning => 'push',
86 num_warnings => 'count',
87 has_warnings => 'count',
88 clear_warnings => 'clear',
89 }
90 );
91
92 sub validated { !$_[0]->has_error_results && $_[0]->has_input }
93 sub is_valid { shift->validated }
94
95 # this ought to be named 'result' for consistency,
96 # but the result objects are named 'result'.
97 # also providing 'field' method for compatibility
98 sub get_result {
99 my ( $self, $name, $die ) = @_;
100
101 my $index;
102 # if this is a full_name for a compound field
103 # walk through the fields to get to it
104 if ( $name =~ /\./ ) {
105 my @names = split /\./, $name;
106 my $result = $self;
107 foreach my $rname (@names) {
108 $result = $result->get_result($rname);
109 return unless $result
110 }
111 return $result;
112 }
113 else # not a compound name
114 {
115 for my $result ( $self->results ) {
116 return $result if ( $result->name eq $name );
117 }
118 }
119 return unless $die;
120 die "Field '$name' not found in '$self'";
121 }
122 sub field { shift->get_result(@_) }
123
124 use namespace::autoclean;
125 1;
126
127 __END__
128
129 =pod
130
131 =encoding UTF-8
132
133 =head1 NAME
134
135 HTML::FormHandler::Result::Role - role with common code for form & field results
136
137 =head1 VERSION
138
139 version 0.410002
140
141 =head1 SYNOPSIS
142
143 Role to hold common result attributes for L<HTML::FormHandler::Result>
144 and L<HTML::FormHandler::Result::Field>.
145
146 =head1 NAME
147
148 HTML::FormHandler::Result::Role - common code for form & field results
149
150 =head1 AUTHOR
151
152 FormHandler Contributors - see HTML::FormHandler
153
154 =head1 COPYRIGHT AND LICENSE
155
156 This software is copyright (c) 2017 by Gerda Shank.
157
158 This is free software; you can redistribute it and/or modify it under
159 the same terms as the Perl 5 programming language system itself.
160
161 =cut