File Coverage

blib/lib/Catalyst/Plugin/FillInForm.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Catalyst::Plugin::FillInForm;
2              
3 1     1   25065 use strict;
  1         2  
  1         38  
4 1     1   825 use MRO::Compat;
  1         3484  
  1         29  
5 1     1   595 use HTML::FillInForm;
  0            
  0            
6              
7             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             sub finalize {
60             my $c = shift;
61             if ( $c->isa('Catalyst::Plugin::FormValidator') ) {
62             $c->fillform
63             if $c->form->has_missing
64             || $c->form->has_invalid
65             || $c->stash->{error};
66             }
67             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             sub fillform {
104             my $c = shift;
105             my $fdat = shift || $c->request->parameters;
106             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             return 1 unless ($c->response->{body});
111              
112             $c->response->output(
113             HTML::FillInForm->new->fill(
114             scalarref => \$c->response->{body},
115             fdat => $fdat,
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             1;