5 Catalyst::Manual::Tutorial::AdvancedCRUD::FormFu - Catalyst Tutorial - Part 9: Advanced CRUD - FormFu
10 This is B<Part 9 of 10> for the Catalyst tutorial.
12 L<Tutorial Overview|Catalyst::Manual::Tutorial>
18 L<Introduction|Catalyst::Manual::Tutorial::Intro>
22 L<Catalyst Basics|Catalyst::Manual::Tutorial::CatalystBasics>
26 L<More Catalyst Basics|Catalyst::Manual::Tutorial::MoreCatalystBasics>
30 L<Basic CRUD|Catalyst::Manual::Tutorial::BasicCRUD>
34 L<Authentication|Catalyst::Manual::Tutorial::Authentication>
38 L<Authorization|Catalyst::Manual::Tutorial::Authorization>
42 L<Debugging|Catalyst::Manual::Tutorial::Debugging>
46 L<Testing|Catalyst::Manual::Tutorial::Testing>
50 B<Advanced CRUD::FormFu>
54 L<Appendices|Catalyst::Manual::Tutorial::Appendices>
61 This portion of the tutorial explores L<HTML::FormFu|HTML::FormFu> and
62 how it can be used to manage forms, perform validation of form input,
63 as well as save and restore data to/from the database. This was written
64 using HTML::FormFu version 0.03007.
67 L<Catalyst::Manual::Tutorial::AdvancedCRUD|Catalyst::Manual::Tutorial::AdvancedCRUD>
68 for additional form management options other than
69 L<HTML::FormFu|HTML::FormFu>.
72 =head1 Install C<HTML::FormFu>
74 If you are following along in Debian 5, it turns out that some of the
75 modules we need are not yet available as Debian packages at the time
76 this was written. To install it with a combination of Debian packages
77 and traditional CPAN modules, first use C<aptitude> to install most of
80 we need to install the
81 L<HTML::FormFu|HTML::FormFu> package:
83 sudo aptitude -y install libhtml-formfu-perl libmoose-perl \
84 libregexp-assemble-perl libhtml-formfu-model-dbic-perl
90 Then use the following command to install directly from CPAN the modules
91 that aren't available as Debian packages:
93 sudo cpan Catalyst::Component::InstancePerContext Catalyst::Controller::HTML::FormFu
96 =head1 C<HTML::FormFu> FORM CREATION
98 This section looks at how L<HTML::FormFu|HTML::FormFu> can be used to
99 add additional functionality to the manually created form from Part 4.
102 =head2 Inherit From C<Catalyst::Controller::HTML::FormFu>
104 First, change your C<lib/MyApp/Controller/Books.pm> to inherit from
105 L<Catalyst::Controller::HTML::FormFu|Catalyst::Controller::HTML::FormFu>
106 by changing the C<use parent> line from the default of:
108 use parent 'Catalyst::Controller';
110 to use the FormFu base controller class:
112 use parent 'Catalyst::Controller::HTML::FormFu';
115 =head2 Add Action to Display and Save the Form
117 Open C<lib/MyApp/Controller/Books.pm> in your editor and add the
122 Use HTML::FormFu to create a new book
126 sub formfu_create :Chained('base) :PathPart('formfu_create') :Args(0) :FormConfig {
129 # Get the form that the :FormConfig attribute saved in the stash
130 my $form = $c->stash->{form};
132 # Check if the form has been submitted (vs. displaying the initial
133 # form) and if the data passed validation. "submitted_and_valid"
134 # is shorthand for "$form->submitted && !$form->has_errors"
135 if ($form->submitted_and_valid) {
137 my $book = $c->model('DB::Books')->new_result({});
138 # Save the form data for the book
139 $form->model->update($book);
140 # Set a status message for the user
141 $c->flash->{status_msg} = 'Book created';
142 # Return to the books list
143 $c->response->redirect($c->uri_for($self->action_for('list')));
146 # Get the authors from the DB
147 my @author_objs = $c->model("DB::Authors")->all();
148 # Create an array of arrayrefs where each arrayref is an author
150 foreach (sort {$a->last_name cmp $b->last_name} @author_objs) {
151 push(@authors, [$_->id, $_->last_name]);
153 # Get the select added by the config file
154 my $select = $form->get_element({type => 'Select'});
155 # Add the authors to it
156 $select->options(\@authors);
160 $c->stash->{template} = 'books/formfu_create.tt2';
164 =head2 Create a Form Config File
166 Although C<HTML::FormFu> supports any configuration file handled by
167 L<Config::Any|Config::Any>, most people tend to use YAML. First
168 create a directory to hold your form configuration files:
170 mkdir -p root/forms/books
172 Then create the file C<root/forms/books/formfu_create.yml> and enter the
176 # indicator is the field that is used to test for form submission
178 # Start listing the form elements
180 # The first element will be a text field for the title
184 # This is an optional 'mouse over' title pop-up
186 title: Enter a book title here
188 # Another text field for the numeric rating
193 title: Enter a rating between 1 and 5 here
195 # Add a drop-down list for the author selection. Note that we will
196 # dynamically fill in all the authors from the controller but we
197 # could manually set items in the drop-list by adding this YAML code:
199 # - [ '1', 'Bastien' ]
200 # - [ '2', 'Nasseh' ]
210 B<NOTE:> Copying and pasting YAML from perl documentation is sometimes
211 tricky. See the L<Config::General Config for this tutorial> section of
212 this document for a more foolproof config format.
215 =head2 Update the CSS
217 Edit C<root/static/css/main.css> and add the following lines to the bottom of
232 These changes will display form elements vertically. Note that the
233 existing definition of the C<.error> class is pulling the color scheme
234 settings from the C<root/lib/config/col> file that was created by the
235 TTSite helper. This allows control over the CSS color settings from a
239 =head2 Create a Template Page To Display The Form
241 Open C<root/src/books/formfu_create.tt2> in your editor and enter the following:
243 [% META title = 'Create/Update Book' %]
245 [%# Render the HTML::FormFu Form %]
248 <p><a href="[% c.uri_for(c.controller.action_for('list')) %]">Return to book list</a></p>
251 =head2 Add Links for Create and Update via C<HTML::FormFu>
253 Open C<root/src/books/list.tt2> in your editor and add the following to
254 the bottom of the existing file:
259 <a href="[% c.uri_for(c.controller.action_for('formfu_create')) %]">Create</a>
262 This adds a new link to the bottom of the book list page that we can
263 use to easily launch our HTML::FormFu-based form.
266 =head2 Test The <HTML::FormFu> Create Form
268 Press C<Ctrl-C> to kill the previous server instance (if it's still
269 running) and restart it:
271 $ script/myapp_server.pl
273 Login as C<test01> (password: mypass). Once at the Book List page,
274 click the new HTML::FormFu "Create" link at the bottom to display the
275 form. Fill in the following values: Title = "Internetworking with
276 TCP/IP Vol. II", Rating = "4", and Author = "Comer". Click Submit,
277 and you will be returned to the Book List page with a "Book created"
278 status message displayed.
280 Also note that this implementation allows you to can create books with
281 bogus information. Although we have constrained the authors with the
282 drop-down list (note that this isn't bulletproof because we still have
283 not prevented a user from "hacking" the form to specify other values),
284 there are no restrictions on items such as the length of the title (for
285 example, you can create a one-letter title) and value for the rating
286 (you can use any number you want, and even non-numeric values with
287 SQLite). The next section will address this concern.
289 B<Note:> Depending on the database you are using and how you established
290 the columns in your tables, the database could obviously provide various
291 levels of "type enforcement" on your data. The key point being made in
292 the previous paragraph is that the I<web application> itself is not
293 performing any validation.
296 =head1 C<HTML::FormFu> VALIDATION AND FILTERING
298 Although the use of L<HTML::FormFu|HTML::FormFu> in the previous section
299 did provide an automated mechanism to build the form, the real power of
300 this module stems from functionality that can automatically validate and
301 filter the user input. Validation uses constraints to be sure that
302 users input appropriate data (for example, that the email field of a
303 form contains a valid email address). Filtering can also be used to
304 remove extraneous whitespace from fields or to escape meta-characters in
308 =head2 Add Constraints
310 Open C<root/forms/books/formfu_create.yml> in your editor and update it
314 # indicator is the field that is used to test for form submission
316 # Start listing the form elements
318 # The first element will be a text field for the title
322 # This is an optional 'mouse over' title pop-up
324 title: Enter a book title here
325 # Add constraints for the field
327 # Force the length to be between 5 and 40 chars
331 # Override the default of 'Invalid input'
332 message: Length must be between 5 and 40 characters
334 # Another text field for the numeric rating
339 title: Enter a rating between 1 and 5 here
340 # Use Filter to clean up the input data
341 # Could use 'NonNumeric' below, but since Filters apply *before*
342 # constraints, it would conflict with the 'Integer' constraint below.
343 # So let's skip this and just use the constraint.
345 # Remove everything except digits
347 # Add constraints to the field
349 # Make sure it's a number
351 message: "Required. Digits only, please."
352 # Check the min & max values
356 message: "Must be between 1 and 5."
358 # Add a select list for the author selection. Note that we will
359 # dynamically fill in all the authors from the controller but we
360 # could manually set items in the select by adding this YAML code:
362 # - [ '1', 'Bastien' ]
363 # - [ '2', 'Nasseh' ]
367 # Convert the drop-down to a multi-select list
369 # Display 3 entries (user can scroll to see others)
371 # One could argue we don't need to do filters or constraints for
372 # a select list, but it's smart to do validation and sanity
373 # checks on this data in case a user "hacks" the input
374 # Add constraints to the field
376 # Make sure it's a number
384 # Global filters and constraints.
386 # The user cannot leave any fields blank
388 # If not all fields are required, move the Required constraint to the
391 # Remove whitespace at both ends
393 # Escape HTML characters for safety
396 B<NOTE:> Copying and pasting YAML from perl documentation is sometimes
397 tricky. See the L<Config::General Config for this tutorial> section of
398 this document for a more foolproof config format.
400 The main changes are:
406 The C<Select> element for C<authors> is changed from a single-select
407 drop-down to a multi-select list by adding configuration for the
408 C<multiple> and C<size> options in C<formfu_create.yml>.
412 Constraints are added to provide validation of the user input. See
413 L<HTML::FormFu::Constraint|HTML::FormFu::Constraint> for other
414 constraints that are available.
418 A variety of filters are run on every field to remove and escape
419 unwanted input. See L<HTML::FormFu::Filter|HTML::FormFu::Filter>
420 for more filter options.
425 =head2 Try Out the Updated Form
427 Press C<Ctrl-C> to kill the previous server instance (if it's still
428 running) and restart it:
430 $ script/myapp_server.pl
432 Make sure you are still logged in as C<test01> and try adding a book
433 with various errors: title less than 5 characters, non-numeric rating, a
434 rating of 0 or 6, etc. Also try selecting one, two, and zero authors.
435 When you click Submit, the HTML::FormFu C<constraint> items will
436 validate the logic and insert feedback as appropriate. Try adding blank
437 spaces at the front or the back of the title and note that it will be
441 =head1 CREATE AND UPDATE/EDIT ACTION
443 Let's expand the work done above to add an edit action. First, open
444 C<lib/MyApp/Controller/Books.pm> and add the following method to the
449 Use HTML::FormFu to update an existing book
453 sub formfu_edit :Chained('object') :PathPart('formfu_edit') :Args(0)
454 :FormConfig('books/formfu_create.yml') {
455 my ($self, $c, $id) = @_;
457 # Get the specified book
458 my $book = $c->model('DB::Books')->find($id);
460 # Make sure we were able to get a book
462 $c->flash->{error_msg} = "Invalid book -- Cannot edit";
463 $c->response->redirect($c->uri_for($self->action_for('list')));
467 # Get the form that the :FormConfig attribute saved in the stash
468 my $form = $c->stash->{form};
470 # Check if the form has been submitted (vs. displaying the initial
471 # form) and if the data passed validation. "submitted_and_valid"
472 # is shorthand for "$form->submitted && !$form->has_errors"
473 if ($form->submitted_and_valid) {
474 # Save the form data for the book
475 $form->model->update($book);
476 # Set a status message for the user
477 $c->flash->{status_msg} = 'Book edited';
478 # Return to the books list
479 $c->response->redirect($c->uri_for($self->action_for('list')));
482 # Get the authors from the DB
483 my @author_objs = $c->model("DB::Authors")->all();
484 # Create an array of arrayrefs where each arrayref is an author
486 foreach (sort {$a->last_name cmp $b->last_name} @author_bjs) {
487 push(@authors, [$_->id, $_->last_name]);
489 # Get the select added by the config file
490 my $select = $form->get_element({type => 'Select'});
491 # Add the authors to it
492 $select->options(\@authors);
493 # Populate the form with existing values from DB
494 $form->model->default_values($book);
498 $c->stash->{template} = 'books/formfu_create.tt2';
501 Most of this code should look familiar to what we used in the
502 C<formfu_create> method (in fact, we should probably centralize some of
503 the common code in separate methods). The main differences are:
509 We accept C<$id> as an argument via the URL.
513 We use C<$id> to look up the existing book from the database.
517 We make sure the C<$id> and book lookup returned a valid book. If not,
518 we set the error message and return to the book list.
522 If the form has been submitted and passes validation, we skip creating a
523 new book and just use C<$form-E<gt>model-E<gt>update> to update the existing
528 If the form is being displayed for the first time (or has failed
529 validation and it being redisplayed), we use
530 C<$form-E<gt>model-E<gt>default_values> to populate the form with data from the
535 Then, edit C<root/src/books/list.tt2> and add a new link below the
536 existing "Delete" link that allows us to edit/update each existing book.
537 The last E<lt>tdE<gt> cell in the book list table should look like the
542 [% # Add a link to delete a book %]
543 <a href="[% c.uri_for(c.controller.action_for('delete'), [book.id]) %]">Delete</a>
544 [% # Add a link to edit a book %]
545 <a href="[% c.uri_for(c.controller.action_for('formfu_edit'), [book.id]) %]">Edit</a>
549 B<Note:> Only add two lines (the "Add a link to edit a book" comment
550 and the href for C<formfu_edit>). Make sure you add it below the
551 existing C<delete> link.
554 =head2 Try Out the Edit/Update Feature
556 Press C<Ctrl-C> to kill the previous server instance (if it's still
557 running) and restart it:
559 $ script/myapp_server.pl
561 Make sure you are still logged in as C<test01> and go to the
562 L<http://localhost:3000/books/list> URL in your browser. Click the
563 "Edit" link next to "Internetworking with TCP/IP Vol. II", change the
564 rating to a 3, the "II" at end of the title to the number "2", add
565 Stevens as a co-author (control-click), and click Submit. You will then
566 be returned to the book list with a "Book edited" message at the top in
567 green. Experiment with other edits to various books.
569 =head2 More Things to Try
571 You are now armed with enough knowledge to be dangerous. You can keep
572 tweaking the example application; some things you might want to do:
578 Add an appropriate authorization check to the new Edit function.
582 Cleanup the List page so that the Login link only displays when the user
583 isn't logged in and the Logout link only displays when a user is logged
588 Add a more sensible policy for when and how users and admins can do
589 things in the CRUD cycle.
593 Support the CRUD cycle for authors.
597 Or you can proceed to write your own application, which is probably the
598 real reason you worked through this Tutorial in the first place.
600 =head2 Config::General Config for this tutorial
602 If you are having difficulty with YAML config above, please save the
603 below into the file C<formfu_create.conf> and delete the
604 C<formfu_create.yml> file. The below is in
605 L<Config::General|Config::General> format which follows the syntax of
614 message Length must be between 5 and 40 characters
622 title Enter a book title here
633 title Enter a rating between 1 and 5 here
657 Kennedy Clark, C<hkclark@gmail.com>
659 Please report any errors, issues or suggestions to the author. The
660 most recent version of the Catalyst Tutorial can be found at
661 L<http://dev.catalyst.perl.org/repos/Catalyst/Catalyst-Manual/5.70/trunk/lib/Catalyst/Manual/Tutorial/>.
663 Copyright 2006-2008, Kennedy Clark, under Creative Commons License
664 (L<http://creativecommons.org/licenses/by-sa/3.0/us/>).