X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FManual%2FTutorial%2FAdvancedCRUD%2FFormFu.pod;h=90d6ea5662c4b7269b0fd719906ed35198ccd755;hb=7c6892d94938b07b1ec201e2aef9695d2d35d3c5;hp=4e6dba6814999f98c89b68fe7681bf3e397fb56b;hpb=f279297ad42ed1dc1fc4e2289a4a1dc17acf91d7;p=catagits%2FCatalyst-Manual.git diff --git a/lib/Catalyst/Manual/Tutorial/AdvancedCRUD/FormFu.pod b/lib/Catalyst/Manual/Tutorial/AdvancedCRUD/FormFu.pod index 4e6dba6..90d6ea5 100644 --- a/lib/Catalyst/Manual/Tutorial/AdvancedCRUD/FormFu.pod +++ b/lib/Catalyst/Manual/Tutorial/AdvancedCRUD/FormFu.pod @@ -5,8 +5,6 @@ Catalyst::Manual::Tutorial::AdvancedCRUD::FormFu - Catalyst Tutorial - Part 9: Advanced CRUD - FormFu -NOTE: This part of the tutorial is in progress and will be ready soon. - =head1 OVERVIEW This is B for the Catalyst tutorial. @@ -62,7 +60,8 @@ L This portion of the tutorial explores L and how it can be used to manage forms, perform validation of form input, -as well as save and restore data to/from the database. +as well as save and restore data to/from the database. This was written +using HTML::FormFu version 0.03007. See L @@ -81,13 +80,33 @@ required by C: sudo apt-get install libtest-nowarnings-perl libdatetime-format-builder-perl \ libdatetime-format-strptime-perl libdatetime-locale-perl \ libhtml-tokeparser-simple-perl liblist-moreutils-perl \ - libregexp-copy-perl libregexp-common-perl libyaml-syck-perl libparams-util-perl + libregexp-copy-perl libregexp-common-perl libyaml-syck-perl libparams-util-perl \ + libcrypt-des-perl libcaptcha-recaptcha-perl libcrypt-cbc-perl \ + libreadonly-xs-perl libmoose-perl libregexp-assemble-perl + + ... + + sudo apt-get clean Then use the following command to install directly from CPAN the modules that aren't available as Ubuntu/Debian packages via C: - sudo cpan File::ShareDir Task::Weaken Config::Any HTML::FormFu \ - Catalyst::Controller::HTML::FormFu + sudo cpan File::ShareDir Task::Weaken Config::Any Test::Harness Test::Aggregate \ + boolean Test::MockTime DateTime::Format::Natural HTML::FormFu \ + Catalyst::Component::InstancePerContext Catalyst::Controller::HTML::FormFu \ + HTML::FormFu::Model::DBIC + + ... + + Is it OK to try to connect to the Internet? [yes] yes + + ... + + +B If you are following along with the Ubuntu LiveCD, you might +want to make sure you still have adequate free disk space in the root +partition with the C command. You can free up some space with +C. =head1 C FORM CREATION @@ -100,13 +119,13 @@ add additional functionality to the manually created form from Part 4. First, change your C to inherit from L -by changing the C line from the default of: +by changing the C line from the default of: - use base 'Catalyst::Controller'; + use parent 'Catalyst::Controller'; to use the FormFu base controller class: - use base 'Catalyst::Controller::HTML::FormFu'; + use parent 'Catalyst::Controller::HTML::FormFu'; =head2 Add Action to Display and Save the Form @@ -133,11 +152,11 @@ following method: # Create a new book my $book = $c->model('DB::Books')->new_result({}); # Save the form data for the book - $form->save_to_model($book); + $form->model->update($book); # Set a status message for the user $c->flash->{status_msg} = 'Book created'; # Return to the books list - $c->response->redirect($c->uri_for('list')); + $c->response->redirect($c->uri_for($self->action_for('list'))); $c->detach; } else { # Get the authors from the DB @@ -211,7 +230,7 @@ this document for a more foolproof config format. =head2 Update the CSS -Edit C and add the following lines to the bottom of +Edit C and add the following lines to the bottom of the file: input { @@ -241,7 +260,7 @@ Open C in your editor and enter the following: [%# Render the HTML::FormFu Form %] [% form %] -

Return to book list

+

Return to book list

=head2 Add Links for Create and Update via C @@ -251,7 +270,7 @@ the bottom of the existing file:

HTML::FormFu: - Create + Create

This adds a new link to the bottom of the book list page that we can @@ -325,7 +344,7 @@ to match: max: 40 # Override the default of 'Invalid input' message: Length must be between 5 and 40 characters - + # Another text field for the numeric rating - type: Text name: rating @@ -333,18 +352,22 @@ to match: attributes: title: Enter a rating between 1 and 5 here # Use Filter to clean up the input data - filter: + # Could use 'NonNumeric' below, but since Filters apply *before* + # constraints, it would conflict with the 'Integer' constraint below. + # So let's skip this and just use the constraint. + #filter: # Remove everything except digits - - NonNumeric + #- NonNumeric # Add constraints to the field constraints: - - Required # Make sure it's a number - - Integer - message: "Digits only, please." - # Filters apply before constraints. - # If a user gives the rating "excellent", the NonNumeric filter would remove the entire string as it contains no digits. - # Remove the NonNumeric filter and let the Integer constraint handle the validation and error message. + - type: Integer + message: "Required. Digits only, please." + # Check the min & max values + - type: Range + min: 1 + max: 5 + message: "Must be between 1 and 5." # Add a select list for the author selection. Note that we will # dynamically fill in all the authors from the controller but we @@ -376,7 +399,8 @@ to match: constraints: # The user cannot leave any fields blank - Required - # If not all fields are required, move the Required constraint to the fields that are. + # If not all fields are required, move the Required constraint to the + # fields that are filter: # Remove whitespace at both ends - TrimEdges @@ -449,7 +473,7 @@ bottom: # Make sure we were able to get a book unless ($book) { $c->flash->{error_msg} = "Invalid book -- Cannot edit"; - $c->response->redirect($c->uri_for('list')); + $c->response->redirect($c->uri_for($self->action_for('list'))); $c->detach; } @@ -461,11 +485,11 @@ bottom: # is shorthand for "$form->submitted && !$form->has_errors" if ($form->submitted_and_valid) { # Save the form data for the book - $form->save_to_model($book); + $form->model->update($book); # Set a status message for the user $c->flash->{status_msg} = 'Book edited'; # Return to the books list - $c->response->redirect($c->uri_for('list')); + $c->response->redirect($c->uri_for($self->action_for('list'))); $c->detach; } else { # Get the authors from the DB @@ -480,7 +504,7 @@ bottom: # Add the authors to it $select->options(\@authors); # Populate the form with existing values from DB - $form->defaults_from_model($book); + $form->model->default_values($book); } # Set the template @@ -509,14 +533,14 @@ we set the error message and return to the book list. =item * If the form has been submitted and passes validation, we skip creating a -new book and just use C<$form-Esave_to_model> to update the existing +new book and just use C<$form-Emodel-Eupdate> to update the existing book. =item * If the form is being displayed for the first time (or has failed validation and it being redisplayed), we use - C<$form-Edefault_from_model> to populate the form with data from the + C<$form-Emodel-Edefault_values> to populate the form with data from the database. =back @@ -526,12 +550,18 @@ existing "Delete" link that allows us to edit/update each existing book. The last EtdE cell in the book list table should look like the following: + ... [% # Add a link to delete a book %] - Delete + Delete [% # Add a link to edit a book %] - Edit + Edit + ... + +B Only add two lines (the "Add a link to edit a book" comment +and the href for C). Make sure you add it below the +existing C link. =head2 Try Out the Edit/Update Feature @@ -549,6 +579,36 @@ Stevens as a co-author (control-click), and click Submit. You will then be returned to the book list with a "Book edited" message at the top in green. Experiment with other edits to various books. +=head2 More Things to Try + +You are now armed with enough knowledge to be dangerous. You can keep +tweaking the example application; some things you might want to do: + +=over 4 + +=item * + +Add an appropriate ACL to the new Edit function. + +=item * + +Cleanup the List page so that the Login link only displays when the user +isn't logged in and the Logout link only displays when a user is logged +in. + +=item * + +Add a more sensible policy for when and how users and admins can do +things in the CRUD cycle. + +=item * + +Support the CRUD cycle for authors. + +=back + +Or you can proceed to write your own application, which is probably the +real reason you worked through this Tutorial in the first place. =head2 Config::General Config for this tutorial @@ -611,8 +671,7 @@ Kennedy Clark, C Please report any errors, issues or suggestions to the author. The most recent version of the Catalyst Tutorial can be found at -L. +L. -Copyright 20066-2008, Kennedy Clark, under Creative Commons License -(L). - +Copyright 2006-2008, Kennedy Clark, under Creative Commons License +(L).