update chapter 9 formfu POD
[catagits/Catalyst-Manual.git] / lib / Catalyst / Manual / Tutorial / 09_AdvancedCRUD / 09_FormHandler.pod
CommitLineData
0abc72ed 1=head1 NAME
2
3Catalyst::Manual::Tutorial::09_AdvancedCRUD::09_FormHandler - Catalyst Tutorial - Chapter 9: Advanced CRUD - FormHandler
4
5
6=head1 OVERVIEW
7
8This is B<Chapter 9 of 10> for the Catalyst tutorial.
9
10L<Tutorial Overview|Catalyst::Manual::Tutorial>
11
12=over 4
13
14=item 1
15
16L<Introduction|Catalyst::Manual::Tutorial::01_Intro>
17
18=item 2
19
20L<Catalyst Basics|Catalyst::Manual::Tutorial::02_CatalystBasics>
21
22=item 3
23
24L<More Catalyst Basics|Catalyst::Manual::Tutorial::03_MoreCatalystBasics>
25
26=item 4
27
28L<Basic CRUD|Catalyst::Manual::Tutorial::04_BasicCRUD>
29
30=item 5
31
32L<Authentication|Catalyst::Manual::Tutorial::05_Authentication>
33
34=item 6
35
36L<Authorization|Catalyst::Manual::Tutorial::06_Authorization>
37
38=item 7
39
40L<Debugging|Catalyst::Manual::Tutorial::07_Debugging>
41
42=item 8
43
44L<Testing|Catalyst::Manual::Tutorial::08_Testing>
45
46=item 9
47
48B<09_Advanced CRUD::09_FormHandler>
49
50=item 10
51
52L<Appendices|Catalyst::Manual::Tutorial::10_Appendices>
53
54=back
55
56
57=head1 DESCRIPTION
58
59This portion of the tutorial explores L<HTML::FormHandler|HTML::FormHandler> and
60how it can be used to manage forms, perform validation of form input,
61as well as save and restore data to/from the database. This was written
62using HTML::FormHandler version 0.28001.
63
64See
65L<Catalyst::Manual::Tutorial::09_AdvancedCRUD|Catalyst::Manual::Tutorial::09_AdvancedCRUD>
66for additional form management options other than
67L<HTML::FormHandler|HTML::FormHandler>.
68
69
70=head1 Install HTML::FormHandler
71
72
73Use the following command to install L<HTML::FormHandler::Model::DBIC> directly
74from CPAN:
75
76 sudo cpan HTML::FormHandler::Model::DBIC
77
78It will install L<HTML::FormHandler> as a prereq.
79
80
81=head1 HTML::FormHandler FORM CREATION
82
83This section looks at how L<HTML::FormHandler|HTML::FormHandler> can be used to
84add additional functionality to the manually created form from Chapter 4.
85
86
87=head2 Using FormHandler in your controllers
88
89FormHandler doen't have a Catalyst base controller, because interfacing
90to a form is only a couple of lines of code.
91
92=head2 Create a Book Form
93
94Create the directory C<lib/MyApp/Form>. Create C<lib/MyApp/Form/Book.pm>:
95
96 package MyApp::Form::Book;
97 use HTML::FormHandler::Moose;
98 extends 'HTML::FormHandler::Model::DBIC';
99 use namespace::autoclean;
100
101 has '+item_class' => ( default =>'Books' );
102 has_field 'title';
103 has_field 'rating' => ( type => 'Integer' );
104 has_field 'authors' => ( type => 'Multiple', label_column => 'last_name' );
105 has_field 'submit' => ( type => 'Submit', value => 'Submit' );
106
107 __PACKAGE__->meta->make_immutable;
108 1;
109
110
111=head2 Add Action to Display and Save the Form
112
113At the top of the C<lib/MyApp/Controller/Books.pm> add:
114
115 use MyApp::Form::Book;
116
117Add the following methods:
118
119 =head2 create
120
121 Use HTML::FormHandler to create a new book
122
123 =cut
124
125 sub create : Chained('base') PathPart('create') Args(0) {
126 my ($self, $c ) = @_;
127
128 my $book = $c->model('DB::Book')->new_result({});
129 return $self->form($c, $book);
130 }
131
132 =head2 form
133
134 Process the FormHandler book form
135
136 =cut
137
138 sub form {
139 my ( $self, $c, $book ) = @_;
140
141 my $form = MyApp::Form::Book->new;
142 # Set the template
143 $c->stash( template => 'books/form.tt2', form => $form );
144 $form->process( item => $book, params => $c->req->params );
145 return unless $form->validated;
146 $c->flash( message => 'Book created' );
147 # Redirect the user back to the list page
148 $c->response->redirect($c->uri_for($self->action_for('list')));
149 }
150
151These two methods could be combined at this point, but we'll use the 'form'
152method later when we implement 'edit'.
153
154
155=head2 Create a Template Page To Display The Form
156
157Open C<root/src/books/form.tt2> in your editor and enter the following:
158
159 [% META title = 'Create/Update Book' %]
160
161 [%# Render the HTML::FormHandler Form %]
162 [% form.render %]
163
164 <p><a href="[% c.uri_for(c.controller.action_for('list')) %]">Return to book list</a></p>
165
166
167=head2 Add Link for Create
168
169Open C<root/src/books/list.tt2> in your editor and add the following to
170the bottom of the existing file:
171
172 ...
173 <p>
174 HTML::FormHandler:
175 <a href="[% c.uri_for(c.controller.action_for('create')) %]">Create</a>
176 </p>
177
178This adds a new link to the bottom of the book list page that we can
179use to easily launch our HTML::FormHandler-based form.
180
181
182=head2 Test The HTML::FormHandler Create Form
183
184Press C<Ctrl-C> to kill the previous server instance (if it's still
185running) and restart it:
186
187 $ script/myapp_server.pl
188
189Login as C<test01> (password: mypass). Once at the Book List page,
190click the new HTML::Formhandler "Create" link at the bottom to display the
191form. Fill in the following values:
192
193 Title = "Internetworking with TCP/IP Vol. II"
194 Rating = "4"
195 Author = "Comer"
196
197Click the Submit button, and you will be returned to the Book List page
198with a "Book created" status message displayed.
199
200Note that because the 'Author' column is a Select list, only the authors
201in the database can be entered. The 'ratings' field will only accept
202integers.
203
204
205=head2 Add Constraints
206
207Open C<lib/MyApp/Form/Book.pm> in your editor.
208
209Restrict the title size and make it required:
210
211 has_field 'title' => ( minlength => 5, maxlength => 40, required => 1 );
212
213Add range constraints to the 'rating' field:
214
215 has_field 'rating' => ( type => 'Integer', range_start => 1, range_end => 5 );
216
217The 'authors' relationship is a 'many-to-many' pseudo-relation, so this field
218can be set to Multiple to allow the selection of multiple authors and make it
219required:
220
221 has_field 'authors' => ( type => 'Multiple', required => 1 );
222
223Note: FormHandler automatically strips whitespace at the beginning or end of fields.
224If you want some other kind of stripping (or none) you can specify it explicitly.
225(see L<HTML::FormHandler::Manual>)
226
227=head2 Try Out the Updated Form
228
229Press C<Ctrl-C> to kill the previous server instance (if it's still
230running) and restart it:
231
232 $ script/myapp_server.pl
233
234Make sure you are still logged in as C<test01> and try adding a book
235with various errors: title less than 5 characters, non-numeric rating, a
236rating of 0 or 6, etc. Also try selecting one, two, and zero authors.
237
238=head2 Create the 'edit' method
239
240Edit C<lib/MyApp/Controller/Books.pm> and add the following method:
241
242
243 =head2 edit
244
245 Edit an existing book with FormHandler
246
247 =cut
248
249 sub edit : Chained('object') PathPart('edit') Args(0) {
250 my ( $self, $c ) = @_;
251
252 return $self->form($c, $c->stash->{object});
253 }
254
255Update the C<root/src/books/list.tt2>, adding an 'edit' link below the
256"Delete" link to use the FormHandler edit method:
257
258 <td>
259 [% # Add a link to delete a book %]
260 <a href="[% c.uri_for(c.controller.action_for('delete'), [book.id]) %]">Delete</a>
261 [% # Add a link to edit a book %]
262 <a href="[% c.uri_for(c.controller.action_for('edit'), [book.id]) %]">Edit</a>
263 </td>
264
265
266=head2 Try Out the Edit/Update Feature
267
268Press C<Ctrl-C> to kill the previous server instance (if it's still
269running) and restart it:
270
271 $ script/myapp_server.pl
272
273Make sure you are still logged in as C<test01> and go to the
274L<http://localhost:3000/books/list> URL in your browser. Click the
275"Edit" link next to "Internetworking with TCP/IP Vol. II", change the
276rating to a 3, the "II" at end of the title to the number "2", add
277Stevens as a co-author (control-click), and click Submit. You will then
278be returned to the book list with a "Book edited" message at the top in
279green. Experiment with other edits to various books.
280
281=head2 See additional documentation on FormHandler
282
f7f5632e 283L<HTML::FormHandler::Manual>
0abc72ed 284
f7f5632e 285L<HTML::FormHandler>
0abc72ed 286
287 #formhandler on irc.perl.org
288
289 mailing list: http://groups.google.com/group/formhandler
290
291 code: http://github.com/gshank/html-formhandler/tree/master
292
293=head1 AUTHOR
294
295Gerda Shank, C<gshank@cpan.org>
296
297Copyright 2009, Gerda Shank, Perl Artistic License