blib/lib/Catalyst/Plugin/FillInForm.pm

filter: Statementstmt 95.0% 19 / 20 Branchbran 33.3% 2 / 6 Conditioncond 27.2% 3 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 62.0% 31 / 50 CC 8 SCAR 20.2
CC
8
Cov
65%
finalize
14.1
fillform
4.2
BEGIN
1.0
CRAP 7.5
↑ Summary
1 package Catalyst::Plugin::FillInForm;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use HTML::FillInForm;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 our $VERSION = '0.12';
8  
9 =head1 NAME
10  
11 Catalyst::Plugin::FillInForm - FillInForm for Catalyst
12  
13 =head1 SYNOPSIS
14  
15 use Catalyst 'FillInForm';
16 # that's it, if Catalyst::Plugin::FormValidator is being used
17  
18 # OR, manually:
19  
20 # in Controller/Root.pm; assume $c->stash->data is seeded elsewhere
21 sub end : Private {
22 my ( $self, $c ) = @_;
23 $c->forward('MyApp::View::TT') unless $c->res->output;
24 $c->fillform( $c->stash->data );
25 # ....
26  
27 =head1 DESCRIPTION
28  
29 Fill forms automatically, based on data from a previous HTML
30 form. Typically (but not necessarily) used in conjunction with
31 L<Catalyst::Plugin::FormValidator>. This module automatically
32 inserts data from a previous HTML form into HTML input fields,
33 textarea fields, radio buttons, checkboxes, and select
34 tags. It is an instance of L<HTML::FillInForm>, which itself
35 is a subclass of L<HTML::Parser>, which it uses to parse the
36 HTML and insert the values into the proper form tags.
37  
38 The usual application is after a user submits an HTML form
39 without filling out a required field, or with errors in fields
40 having specified constraints. FillInForm is used to
41 redisplay the HTML form with all the form elements containing
42 the submitted info. FillInForm can also be used to fill forms
43 with data from any source, e.g. directly from your database.
44  
45 =head2 EXTENDED METHODS
46  
47 =head3 finalize
48  
49 Will automatically fill in forms, based on the parameters in
50 C<$c-E<gt>req-E<gt>parameters>, if the last form has missing
51 or invalid fields, and if C<Catalyst::Plugin::FormValidator>
52 is being used. C<finalize> is called automatically by the
53 Catalyst Engine; the end user will not have to call it
54 directly. (In fact, it should never be called directly by the
55 end user.)
56  
57 =cut
58  
59 4 ▶sub finalize {
Subroutine finalize: called CC=5 SCAR=26.5
Pod documented
60 4 my $c = shift;
61 4 ▶ if ( $c->isa('Catalyst::Plugin::FormValidator') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->isa("Catalyst::Plugin::FormValidator")) 0 4 4
62 $c->fillform
63 if $c->form->has_missing
64 || $c->form->has_invalid
65 0 ▶ || $c->stash->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $c->form->has_missing or $c->form->has_invalid or $c->stash->{"error"} 0 0 0
Condition0%0/6
$c->form->has_missing or $c->form->has_invalid or $c->stash->{"error"}
l!l&&r!l&&!r
000
$c->form->has_missing or $c->form->has_invalid
l!l&&r!l&&!r
000
MC/DC0%0/3
$c->form->has_missing or $c->form->has_invalid or $c->stash->{"error"}
$c->form->has_missing $c->form->has_invalid $c->stash->{"error"}
Truth table0%0/4
A or B or C
A= $c->form->has_missingB= $c->form->has_invalidC= $c->stash->{"error"}
ABCresult
1XX1
01X1
0011
0000
66 }
67 4 return $c->maybe::next::method(@_);
68 }
69  
70 =head2 METHODS
71  
72 =head3 fillform
73  
74 Fill a form, based on request parameters (the default) or any
75 other specified data hash. You would call this manually if
76 you're getting your data from some source other than the
77 parameters (e.g. if you're seeding an edit form with the
78 results of a database query), or if you're using some other
79 validation system than C<Catalyst::Plugin::FormValidator>.
80  
81 $c->fillform; # defaults to $c->req->parameters
82  
83 # OR
84  
85 $c->fillform( \%data_hash );
86  
87 C<fillform> must be called after an HTML template has been
88 rendered. A typical way of using it is to place it immediately
89 after your C<forward> call to your view class, which might be
90 in a built-in C<end> action in your application class.
91  
92 You can also hand in a hashref of additional params for
93 HTML::FillInForm->fill() if you like. Explicitly providing a
94 \%data_hash is mandatory for this use case.
95  
96 $c->fillform( $c->req->parameters, {
97 ignore_fields => [ 'pagesrc', 'pagedst' ],
98 fill_password => 0,
99 } );
100  
101 =cut
102  
103 4 ▶sub fillform {
Subroutine fillform: called CC=4 SCAR=14.5
Pod documented
104 4 my $c = shift;
105 4 ▶ my $fdat = shift || $c->request->parameters;
Condition66%2/3
shift() || $c->request->parameters
l!l&&r!l&&!r
310
MC/DC0%0/2
shift() || $c->request->parameters
shift() $c->request->parameters
Truth table66%2/3
A || B
A= shift()B= $c->request->parameters
ABresult
1X1
011
000
106 4 my $additional_params = shift;
107  
108 # For whatever reason your response body is empty. So this fillform() will
109 # accomplish nothing. Skip HTML::FillInForm to avoid annoying warnings downstream.
110 4 ▶ return 1 unless ($c->response->{body});
Branch: 1 branch
BranchTrueFalseTotal
unless $c->response->{"body"} 0 4 4
111  
112 $c->response->output(
113 HTML::FillInForm->new->fill(
114 scalarref => \$c->response->{body},
115 4 ▶ fdat => $fdat,
Condition50%1/2
'HTML::FillInForm'->new->fill('scalarref', \$c->response->{'body'}, 'fdat', $fdat, %$additional_params) || ''
l!l
40
MC/DC0%0/1
'HTML::FillInForm'->new->fill('scalarref', \$c->response->{'body'}, 'fdat', $fdat, %$additional_params) || ''
'HTML::FillInForm'->new->fill('scalarref', \$c->response->{'body'}, 'fdat', $fdat, %$additional_params)
Truth table50%1/2
A
A= 'HTML::FillInForm'->new->fill('scalarref', \$c->response->{'body'}, 'fdat', $fdat, %$additional_params)
Aresult
11
00
116 %$additional_params,
117 ) || ''
118 );
119 }
120  
121 =head1 NOTES
122  
123 This class does not play well with Catalyst's ActionClass('RenderView')
124 so you may want to check your C<end> method (in Controller/Root.pm or perhaps
125 MyApp.pm). If it looks like this:
126  
127 sub end : ActionClass('RenderView') {}
128  
129 Then you'll need to change it to something like this:
130  
131 sub end : Private {
132 my ($self, $c) = @_;
133 $c->forward('render');
134 $c->fillform($c->req->params) unless $c->res->output;
135 }
136  
137 sub render : ActionClass('RenderView') { }
138  
139 =head1 SEE ALSO
140  
141 L<Catalyst>, L<Catalyst::Plugin::FormValidator>, L<HTML::FillInForm>, L<Catalyst::Action::RenderView>.
142  
143 =head1 AUTHOR
144  
145 Sebastian Riedel, C<sri@cpan.org>
146 Marcus Ramberg, C<mramberg@cpan.org>
147 Jesse Sheidlower, C<jester@panix.com>
148 Jay Hannah, C<jay@jays.net>
149  
150 =head1 COPYRIGHT
151  
152 This program is free software, you can redistribute it and/or modify it under
153 the same terms as Perl itself.
154  
155 =cut
156  
157 5 1;
↑ Summary