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