Debian Codegraph @

lib/HTML/FormHandler/Manual/Catalyst.pod

raw
1 package HTML::FormHandler::Manual::Catalyst;
2 # ABSTRACT: using HFH forms in Catalyst
3
4 __END__
5
6 =pod
7
8 =encoding UTF-8
9
10 =head1 NAME
11
12 HTML::FormHandler::Manual::Catalyst - using HFH forms in Catalyst
13
14 =head1 VERSION
15
16 version 0.410002
17
18 =head1 SYNOPSIS
19
20 L<Manual Index|HTML::FormHandler::Manual>
21
22 This part of the FormHandler Manual describes the use of the L<HTML::FormHandler>
23 package in Catalyst controllers.
24
25 See the other FormHandler documentation at L<HTML::FormHandler::Manual>, or
26 the base class at L<HTML::FormHandler>.
27
28 =head1 DESCRIPTION
29
30 Although L<HTML::FormHandler> can be used in any Perl web application, module, or
31 script, one of its most common uses is in L<Catalyst> applications.
32
33 Using a form takes only a few lines of code, so it's not necessary to have
34 a L<Catalyst> base controller, although you could make a base controller for
35 FormHandler if you're doing more than the basics.
36
37 =head2 A Controller Example
38
39 The following example uses chained dispatching. The 'form' method is called
40 by both the create and edit actions.
41
42 package BookDB::Controller::Borrower;
43
44 use Moose;
45 BEGIN { extends 'Catalyst::Controller' }
46
47 use BookDB::Form::Borrower;
48
49 sub borrower_base : Chained PathPart('borrower') CaptureArgs(0) { }
50
51 sub list : Chained('borrower_base') PathPart('list') Args(0) {
52 my ( $self, $c ) = @_;
53 my $borrowers = [ $c->model('DB::Borrower')->all ];
54 my @columns = ( 'name', 'email' );
55 $c->stash( borrowers => $borrowers, columns => \@columns,
56 template => 'borrower/list.tt' );
57 }
58
59 sub add : Chained('borrower_base') PathPart('add') Args(0) {
60 my ( $self, $c ) = @_;
61 # Create the empty borrower row for the form
62 $c->stash( borrower => $c->model('DB::Borrower')->new_result({}) );
63 return $self->form($c);
64 }
65
66 sub item : Chained('borrower_base') PathPart('') CaptureArgs(1) {
67 my ( $self, $c, $borrower_id ) = @_;
68 $c->stash( borrower => $c->model('DB::Borrower')->find($borrower_id) );
69 }
70
71 sub edit : Chained('item') PathPart('edit') Args(0) {
72 my ( $self, $c ) = @_;
73 return $self->form($c);
74 }
75
76 sub form {
77 my ( $self, $c ) = @_;
78
79 my $form = BookDB::Form::Borrower->new;
80 $c->stash( form => $form, template => 'borrower/form.tt' );
81 return unless $form->process( item => $c->stash->{borrower},
82 params => $c->req->parameters );
83 $c->res->redirect( $c->uri_for($self->action_for('list')) );
84 }
85
86 sub delete : Chained('item') PathPart('delete') Args(0) {
87 my ( $self, $c ) = @_;
88
89 $c->stash->{borrower}->delete;
90 $c->res->redirect( $c->uri_for($c->action_for('list')) );
91 }
92
93 1;
94
95 =head2 Another way to set up your form
96
97 If you are setting the schema or other form attributes (such as the user_id,
98 or other attributes) on your form you could create a base controller that would set
99 these in the form on each call using L<Catalyst::Component::InstancePerContext>,
100 or set them in a base Chained method.
101
102 sub book_base : Chained PathPart('book') CaptureArgs(0) {
103 my ( $self, $c ) = @_;
104 my $form = MyApp::Form->new;
105 $form->schema( $c->model('DB')->schema );
106 $form->params( $c->req->parameters );
107 $form->user_id( $c->user->id );
108 $c->stash( form => $form );
109 }
110
111 Then you could just pass in the item_id when the form is processed.
112
113 return unless $c->stash->{form}->process( item_id => $id );
114
115 =head2 Putting a form in a Moose attribute
116
117 You can also put your form in a Moose attribute in the controller.
118
119 package MyApp::Controller::Book;
120 use Moose;
121 BEGIN { extends 'Catalyst::Controller'; }
122 use MyApp::Form::Book;
123 has 'edit_form' => ( isa => 'MyApp::Form::Book', is => 'rw',
124 lazy => 1, default => sub { MyApp::Form::Book->new } );
125
126 Then you can process the form in your actions with
127 C<< $self->edit_form->process( params => $c->req->body_parameters ); >> or
128 C<< my $result = $self->edit_form->run( params => $c->req->body_parameters ); >>.
129
130 =head2 Using HTML::FillInForm
131
132 If you want to use L<HTML::FillInForm> to fill in values instead of
133 doing it in directly in a template using either the field or the form 'fif'
134 methods, you can use L<Catalyst::View::FillInForm> on your view class:
135
136 package MyApp::View::TT;
137 use Moose;
138 with 'Catalyst::View::FillInForm';
139 ....
140 1;
141
142 and set the 'fif' hash in the 'fillinform' stash variable:
143
144 $self->form->process( ... );
145 $c->stash( fillinform => $self->form->fif );
146 return unless $form->validated;
147
148 When the 'fillinform' stash variable is set, HTML::FillInForm will automatically
149 be used by your view to fill in the form values. This can be very helpful
150 when you want to build your forms by hand, or when you have legacy forms that
151 you're just trying to hook up to FormHandler.
152
153 =head2 The Catalyst context
154
155 FormHandler has a 'ctx' attribute that can be used to set the Catalyst context (or
156 anything you want, really). But if you can avoid passing in the context, you should do so,
157 because you're mixing up your MVC and it makes it much more difficult to test your
158 forms. But if you need to do it, you can:
159
160 my $form = MyApp::Form->new( ctx => $c );
161
162 Usually you should prefer to add new attributes to your form:
163
164 package MyApp::Form;
165 use HTML::FormHandler::Moose;
166 extends 'HTML::FormHandler';
167
168 has 'user_id' => ( is => 'rw' );
169 has 'hostname' => ( is => 'rw' );
170 has 'captcha_store' => ( is => 'rw' );
171 ....
172 1;
173
174 Then just pass the attributes in on new:
175
176 my $form => MyApp::Form->new( user_id => $c->user->id, hostname => $c->req->host,
177 captcha_store => $c->{session}->{captcha} );
178
179 Or set them using accessors:
180
181 $form->user_id( $c->user->id );
182 $form->hostname( $c->req->host );
183 $form->captcha_store( $c->{session}->{captcha} );
184
185 Then you can access these attributes in your form validation methods:
186
187 sub validate_selection {
188 my ( $self, $field ) = @_;
189 if( $field->value eq 'something' && $self->hostname eq 'something_else' )
190 {
191 $field->add_error("some error message" );
192 }
193 }
194
195 =head1 AUTHOR
196
197 FormHandler Contributors - see HTML::FormHandler
198
199 =head1 COPYRIGHT AND LICENSE
200
201 This software is copyright (c) 2017 by Gerda Shank.
202
203 This is free software; you can redistribute it and/or modify it under
204 the same terms as the Perl 5 programming language system itself.
205
206 =cut