File Coverage

blib/lib/HTML/FormHandler/Result.pm
Criterion Covered Total %
statement 21 21 100.0
branch 4 4 100.0
condition 3 3 100.0
subroutine 7 7 100.0
pod 0 5 0.0
total 35 40 87.5


line stmt bran cond sub pod time code
1             package HTML::FormHandler::Result;
2             # ABSTRACT: form result object
3             $HTML::FormHandler::Result::VERSION = '0.40067';
4 141     141   1464 use Moose;
  141         171  
  141         798  
5             # following is to allow the form to return an empty
6             # hashref when value is undefined, without messing
7             # with the way 'value' works for fields
8             with 'HTML::FormHandler::Result::Role';
9             with 'HTML::FormHandler::Traits';
10              
11              
12             has 'form' => (
13             isa => 'HTML::FormHandler',
14             is => 'ro',
15             weak_ref => 1,
16             # handles => ['render' ]
17             );
18              
19             has '_value' => (
20             is => 'ro',
21             writer => '_set_value',
22             reader => '_get_value',
23             clearer => '_clear_value',
24             predicate => 'has_value',
25             );
26              
27 84 100   84 0 3274 sub value { shift->_get_value || {} }
28              
29             has 'form_errors' => (
30             traits => ['Array'],
31             is => 'rw',
32             isa => 'ArrayRef[Str]',
33             default => sub { [] },
34             handles => {
35             all_form_errors => 'elements',
36             push_form_errors => 'push',
37             num_form_errors => 'count',
38             has_form_errors => 'count',
39             clear_form_errors => 'clear',
40             }
41             );
42              
43             sub form_and_field_errors {
44 1     1 0 2 my $self = shift;
45 1         40 my @field_errors = map { $_->all_errors } $self->all_error_results;
  1         32  
46 1         36 my @form_errors = $self->all_form_errors;
47 1         3 return (@form_errors, @field_errors);
48             }
49              
50 1071 100 100 1071 0 39812 sub validated { !$_[0]->has_error_results && $_[0]->has_input && !$_[0]->has_form_errors }
51              
52             has 'ran_validation' => ( is => 'rw', isa => 'Bool', default => 0 );
53              
54             sub fif {
55 1     1 0 2 my $self = shift;
56 1         34 $self->form->fields_fif($self);
57             }
58              
59             sub peek {
60 2     2 0 12 my $self = shift;
61 2         54 my $string = "Form Result " . $self->name . "\n";
62 2         3 my $indent = ' ';
63 2         70 foreach my $res ( $self->results ) {
64 6         18 $string .= $res->peek( $indent );
65             }
66 2         7 return $string;
67             }
68              
69             __PACKAGE__->meta->make_immutable;
70 141     141   599398 use namespace::autoclean;
  141         13198  
  141         1408  
71             1;
72              
73             __END__
74              
75             =pod
76              
77             =encoding UTF-8
78              
79             =head1 NAME
80              
81             HTML::FormHandler::Result - form result object
82              
83             =head1 VERSION
84              
85             version 0.40067
86              
87             =head1 SYNOPSIS
88              
89             This is the Result object that maps to the Form.
90              
91             my $result = $self->form->run( $params );
92             my $result2 = $self->form->run( $other_params );
93              
94             my $value = $result->field('title')->value;
95             my $fif = $result->fif;
96             my $field_fid = $result->field('title')->fif;
97              
98             =head2 DESCRIPTION
99              
100             Although not experimental, the 'results' have not been exercised as much
101             as the other parts of the code. If there is missing functionality or
102             things that don't work, please ask or report bugs.
103              
104             The original FormHandler 'process' method, when used with persistent forms,
105             leaves behind state data for a particular execution of 'process'. This is
106             not optimal or clean from an architectural point of view.
107             The intention with the 'result' object is to separate dynamic data from static.
108             The 'form' object is treated as a kind of result factory, which will spit out
109             results and leave the form in a consistent state.
110              
111             In the current state of implementation, the result object can be used to render
112             a form:
113              
114             $result->render;
115              
116             However there are still open questions about how much of the form/field
117             should be forwarded to the result. At this point, the number of forwarded
118             methods is minimal. Mechanisms to make this more customizable are being
119             considered.
120              
121             Dynamic select lists are not supported yet. Static select lists
122             (that are the same for every form execution) should work fine, but lists
123             that are different depending on some field value will not.
124              
125             Most of this object is implemented in L<HTML::FormHandler::Result::Role>,
126             because it is shared with L<HTML::FormHandler::Field::Result>.
127              
128             =head1 AUTHOR
129              
130             FormHandler Contributors - see HTML::FormHandler
131              
132             =head1 COPYRIGHT AND LICENSE
133              
134             This software is copyright (c) 2016 by Gerda Shank.
135              
136             This is free software; you can redistribute it and/or modify it under
137             the same terms as the Perl 5 programming language system itself.
138              
139             =cut