Commit | Line | Data |
0abc72ed |
1 | =head1 NAME |
2 | |
3 | Catalyst::Manual::Tutorial::09_AdvancedCRUD::09_FormHandler - Catalyst Tutorial - Chapter 9: Advanced CRUD - FormHandler |
4 | |
5 | |
6 | =head1 OVERVIEW |
7 | |
8 | This is B<Chapter 9 of 10> for the Catalyst tutorial. |
9 | |
10 | L<Tutorial Overview|Catalyst::Manual::Tutorial> |
11 | |
12 | =over 4 |
13 | |
14 | =item 1 |
15 | |
16 | L<Introduction|Catalyst::Manual::Tutorial::01_Intro> |
17 | |
18 | =item 2 |
19 | |
20 | L<Catalyst Basics|Catalyst::Manual::Tutorial::02_CatalystBasics> |
21 | |
22 | =item 3 |
23 | |
24 | L<More Catalyst Basics|Catalyst::Manual::Tutorial::03_MoreCatalystBasics> |
25 | |
26 | =item 4 |
27 | |
28 | L<Basic CRUD|Catalyst::Manual::Tutorial::04_BasicCRUD> |
29 | |
30 | =item 5 |
31 | |
32 | L<Authentication|Catalyst::Manual::Tutorial::05_Authentication> |
33 | |
34 | =item 6 |
35 | |
36 | L<Authorization|Catalyst::Manual::Tutorial::06_Authorization> |
37 | |
38 | =item 7 |
39 | |
40 | L<Debugging|Catalyst::Manual::Tutorial::07_Debugging> |
41 | |
42 | =item 8 |
43 | |
44 | L<Testing|Catalyst::Manual::Tutorial::08_Testing> |
45 | |
46 | =item 9 |
47 | |
48 | B<09_Advanced CRUD::09_FormHandler> |
49 | |
50 | =item 10 |
51 | |
52 | L<Appendices|Catalyst::Manual::Tutorial::10_Appendices> |
53 | |
54 | =back |
55 | |
56 | |
57 | =head1 DESCRIPTION |
58 | |
59 | This portion of the tutorial explores L<HTML::FormHandler|HTML::FormHandler> and |
60 | how it can be used to manage forms, perform validation of form input, |
61 | as well as save and restore data to/from the database. This was written |
62 | using HTML::FormHandler version 0.28001. |
63 | |
64 | See |
65 | L<Catalyst::Manual::Tutorial::09_AdvancedCRUD|Catalyst::Manual::Tutorial::09_AdvancedCRUD> |
66 | for additional form management options other than |
67 | L<HTML::FormHandler|HTML::FormHandler>. |
68 | |
69 | |
70 | =head1 Install HTML::FormHandler |
71 | |
72 | |
73 | Use the following command to install L<HTML::FormHandler::Model::DBIC> directly |
74 | from CPAN: |
75 | |
76 | sudo cpan HTML::FormHandler::Model::DBIC |
77 | |
78 | It will install L<HTML::FormHandler> as a prereq. |
79 | |
80 | |
81 | =head1 HTML::FormHandler FORM CREATION |
82 | |
83 | This section looks at how L<HTML::FormHandler|HTML::FormHandler> can be used to |
84 | add additional functionality to the manually created form from Chapter 4. |
85 | |
86 | |
87 | =head2 Using FormHandler in your controllers |
88 | |
89 | FormHandler doen't have a Catalyst base controller, because interfacing |
90 | to a form is only a couple of lines of code. |
91 | |
92 | =head2 Create a Book Form |
93 | |
94 | Create 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 | |
113 | At the top of the C<lib/MyApp/Controller/Books.pm> add: |
114 | |
115 | use MyApp::Form::Book; |
116 | |
117 | Add 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 | |
151 | These two methods could be combined at this point, but we'll use the 'form' |
152 | method later when we implement 'edit'. |
153 | |
154 | |
155 | =head2 Create a Template Page To Display The Form |
156 | |
157 | Open 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 | |
169 | Open C<root/src/books/list.tt2> in your editor and add the following to |
170 | the 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 | |
178 | This adds a new link to the bottom of the book list page that we can |
179 | use to easily launch our HTML::FormHandler-based form. |
180 | |
181 | |
182 | =head2 Test The HTML::FormHandler Create Form |
183 | |
184 | Press C<Ctrl-C> to kill the previous server instance (if it's still |
185 | running) and restart it: |
186 | |
187 | $ script/myapp_server.pl |
188 | |
189 | Login as C<test01> (password: mypass). Once at the Book List page, |
190 | click the new HTML::Formhandler "Create" link at the bottom to display the |
191 | form. Fill in the following values: |
192 | |
193 | Title = "Internetworking with TCP/IP Vol. II" |
194 | Rating = "4" |
195 | Author = "Comer" |
196 | |
197 | Click the Submit button, and you will be returned to the Book List page |
198 | with a "Book created" status message displayed. |
199 | |
200 | Note that because the 'Author' column is a Select list, only the authors |
201 | in the database can be entered. The 'ratings' field will only accept |
202 | integers. |
203 | |
204 | |
205 | =head2 Add Constraints |
206 | |
207 | Open C<lib/MyApp/Form/Book.pm> in your editor. |
208 | |
209 | Restrict the title size and make it required: |
210 | |
211 | has_field 'title' => ( minlength => 5, maxlength => 40, required => 1 ); |
212 | |
213 | Add range constraints to the 'rating' field: |
214 | |
215 | has_field 'rating' => ( type => 'Integer', range_start => 1, range_end => 5 ); |
216 | |
217 | The 'authors' relationship is a 'many-to-many' pseudo-relation, so this field |
218 | can be set to Multiple to allow the selection of multiple authors and make it |
219 | required: |
220 | |
221 | has_field 'authors' => ( type => 'Multiple', required => 1 ); |
222 | |
223 | Note: FormHandler automatically strips whitespace at the beginning or end of fields. |
224 | If 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 | |
229 | Press C<Ctrl-C> to kill the previous server instance (if it's still |
230 | running) and restart it: |
231 | |
232 | $ script/myapp_server.pl |
233 | |
234 | Make sure you are still logged in as C<test01> and try adding a book |
235 | with various errors: title less than 5 characters, non-numeric rating, a |
236 | rating of 0 or 6, etc. Also try selecting one, two, and zero authors. |
237 | |
238 | =head2 Create the 'edit' method |
239 | |
240 | Edit 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 | |
255 | Update 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 | |
268 | Press C<Ctrl-C> to kill the previous server instance (if it's still |
269 | running) and restart it: |
270 | |
271 | $ script/myapp_server.pl |
272 | |
273 | Make sure you are still logged in as C<test01> and go to the |
274 | L<http://localhost:3000/books/list> URL in your browser. Click the |
275 | "Edit" link next to "Internetworking with TCP/IP Vol. II", change the |
276 | rating to a 3, the "II" at end of the title to the number "2", add |
277 | Stevens as a co-author (control-click), and click Submit. You will then |
278 | be returned to the book list with a "Book edited" message at the top in |
279 | green. Experiment with other edits to various books. |
280 | |
281 | =head2 See additional documentation on FormHandler |
282 | |
283 | L<HTML::FormHandler::Manual> |
284 | |
285 | L<HTML::FormHandler> |
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 | |
295 | Gerda Shank, C<gshank@cpan.org> |
296 | |
297 | Copyright 2009, Gerda Shank, Perl Artistic License |