| 1 |
package HTML::FormHandler::Manual::Tutorial; |
| 2 |
# ABSTRACT: how to use FormHandler with Catalyst |
| 3 |
|
| 4 |
__END__ |
| 5 |
|
| 6 |
=pod |
| 7 |
|
| 8 |
=encoding UTF-8 |
| 9 |
|
| 10 |
=head1 NAME |
| 11 |
|
| 12 |
HTML::FormHandler::Manual::Tutorial - how to use FormHandler with Catalyst |
| 13 |
|
| 14 |
=head1 VERSION |
| 15 |
|
| 16 |
version 0.410002 |
| 17 |
|
| 18 |
=head1 SYNOPSIS |
| 19 |
|
| 20 |
L<Manual Index|HTML::FormHandler::Manual> |
| 21 |
|
| 22 |
A tutorial for beginners to L<HTML::FormHandler> |
| 23 |
|
| 24 |
=head1 Using HTML::FormHandler with Catalyst |
| 25 |
|
| 26 |
This tutorial demonstrates how you can use L<HTML::FormHandler> |
| 27 |
to manage forms, validate form input, and interface your forms with the database. |
| 28 |
|
| 29 |
=head1 Installation |
| 30 |
|
| 31 |
Use CPAN to install L<HTML::FormHandler> |
| 32 |
|
| 33 |
=head1 Use the Tutorial application |
| 34 |
|
| 35 |
We'll use the files that were created in the L<Catalyst::Manual::Tutorial>, in |
| 36 |
order to concentrate on just the bits where HTML::FormHandler is useful. |
| 37 |
You can download a tar file of the tutorial files from the Catalyst |
| 38 |
code repository. (See L<Catalyst::Manual::Tutorial::Intro>.) |
| 39 |
|
| 40 |
=head2 Create an HTML::FormHandler form |
| 41 |
|
| 42 |
Untar the tutorial and make a lib/MyApp/Form directory. In that directory |
| 43 |
create the file Book.pm. |
| 44 |
|
| 45 |
package MyApp::Form::Book; |
| 46 |
|
| 47 |
use utf8; # if using non-latin1 languages |
| 48 |
|
| 49 |
use HTML::FormHandler::Moose; |
| 50 |
extends 'HTML::FormHandler::Model::DBIC'; |
| 51 |
|
| 52 |
has '+item_class' => ( default => 'Book' ); |
| 53 |
has_field 'title' => ( type => 'Text' ); |
| 54 |
has_field 'rating' => ( type => 'Integer' ); |
| 55 |
has_field 'authors' => ( type => 'Multiple', label_column => 'last_name' ); |
| 56 |
has_field 'submit' => ( type => 'Submit', value => 'Submit' ); |
| 57 |
|
| 58 |
no HTML::FormHandler::Moose; |
| 59 |
1; |
| 60 |
|
| 61 |
This is your Form class. The form initializes the 'item_class' to the |
| 62 |
source name of your DBIx::Class result class. The form's fields are defined |
| 63 |
with the 'has_field' sugar, or in a 'field_list'. The names of the fields |
| 64 |
should match a column, relationship, or other accessor in your DBIx::Class |
| 65 |
result class. |
| 66 |
|
| 67 |
The basic fields have only a 'type', such as |
| 68 |
'Text', or 'Integer'. These types are actually the names of |
| 69 |
L<HTML::FormHandler::Field> classes. |
| 70 |
'Text' and 'Integer' are types that are provided by HTML::FormHandler, |
| 71 |
in L<HTML::FormHandler::Field::Text> and L<HTML::FormHandler::Field::Integer>. |
| 72 |
|
| 73 |
The 'Multiple' type will allow you to easily create a multiple select |
| 74 |
list from the 'authors' relationship. The 'label_column' attribute |
| 75 |
must be defined because the column in the 'authors' table which is used |
| 76 |
to create the select list does not have the default column name ('name'). |
| 77 |
|
| 78 |
The 'submit' field is necessary if you are going to use FormHandler to |
| 79 |
render your form. It wouldn't be necessary for hand-built templates or HTML. |
| 80 |
|
| 81 |
Eventually you will want to create your own field classes, but for |
| 82 |
this simple form the default types are adequate. |
| 83 |
|
| 84 |
=head2 Connect HTML::FormHandler to your controller |
| 85 |
|
| 86 |
Edit lib/MyApp/Controller/Books.pm. Add use Moose: |
| 87 |
|
| 88 |
use Moose; |
| 89 |
BEGIN { extends 'Catalyst::Controller' } |
| 90 |
use MyApp::Form::Book; |
| 91 |
|
| 92 |
Create an attribute to hold your form: |
| 93 |
|
| 94 |
has 'form' => ( isa => 'MyApp::Form::Book', is => 'rw', |
| 95 |
lazy => 1, default => sub { MyApp::Form::Book->new } ); |
| 96 |
|
| 97 |
=head2 Add Action to Display and Save the Form |
| 98 |
|
| 99 |
In C<lib/MyApp/Controller/Books.pm> add the following method: |
| 100 |
|
| 101 |
sub edit : Local { |
| 102 |
my ( $self, $c, $book_id ) = @_; |
| 103 |
|
| 104 |
$c->stash( template => 'books/edit.tt2', |
| 105 |
form => $self->form ); |
| 106 |
|
| 107 |
# Validate and insert/update database |
| 108 |
return unless $self->form->process( item_id => $book_id, |
| 109 |
params => $c->req->parameters, |
| 110 |
schema => $c->model('DB')->schema ); |
| 111 |
|
| 112 |
# Form validated, return to the books list |
| 113 |
$c->flash->{status_msg} = 'Book saved'; |
| 114 |
$c->res->redirect($c->uri_for('list')); |
| 115 |
} |
| 116 |
|
| 117 |
This will handle both creating new books, and updating old books. |
| 118 |
If $book_id is undefined, then HTML::FormHandler will create |
| 119 |
a new book from your form. If you pass in a DBIx::Class row |
| 120 |
object instead of a primary key, you don't need to specify the |
| 121 |
schema. |
| 122 |
|
| 123 |
=head2 Render the form |
| 124 |
|
| 125 |
Save a copy of C<root/src/books/edit.tt2> and create a new file that |
| 126 |
contains only: |
| 127 |
|
| 128 |
[% form.render %] |
| 129 |
|
| 130 |
=head2 Alternative hand-built Template for the form (optional) |
| 131 |
|
| 132 |
Although the automatic rendering works well, sometimes it's necessary |
| 133 |
to hand build HTML. This section contains an example of a Template Toolkit |
| 134 |
template that may be used to display a FormHandler form. |
| 135 |
|
| 136 |
In some cases, you might want to use the rendering for just the field |
| 137 |
and build custom divs or tables or whatever around it: |
| 138 |
|
| 139 |
<div class="mycustomclass"> |
| 140 |
[% form.render_field('book') %] |
| 141 |
</div> |
| 142 |
|
| 143 |
If you don't want to play with HTML at this point, you can skip ahead |
| 144 |
to the next section. |
| 145 |
|
| 146 |
You could also use TT macros to do pretty sophisticated |
| 147 |
template generation. But for now, we'll stick to a straightforward TT |
| 148 |
template: |
| 149 |
|
| 150 |
Delete the single statement in C<root/src/books/edit.tt2>, and enter |
| 151 |
or copy the following: |
| 152 |
|
| 153 |
[% META title = 'Book Form' %] |
| 154 |
|
| 155 |
[% FOR field IN form.error_fields %] |
| 156 |
[% FOR error IN field.errors %] |
| 157 |
<p><span class="error" id="error"> |
| 158 |
[% field.label _ ': ' _ error %] </span></p> |
| 159 |
[% END %] |
| 160 |
[% END %] |
| 161 |
|
| 162 |
<form name="[% form.name %]" |
| 163 |
action="[% c.uri_for('edit', form.item_id) %]" |
| 164 |
method="post"> |
| 165 |
<p> |
| 166 |
[% f = form.field('title') %] |
| 167 |
<label class="label" for="[% f.name %]">[% f.label %]:</label> |
| 168 |
<input type="text" name="[% f.name %]" id="[% f.name %]" value="[% f.fif %]"> |
| 169 |
</p> |
| 170 |
<p> |
| 171 |
[% f = form.field('rating') %] |
| 172 |
<label class="label" for="[% f.name %]">[% f.label %]:</label> |
| 173 |
<input type="text" name="[% f.name %]" id="[% f.name %]" %] value="[% f.fif %]"> |
| 174 |
</p> |
| 175 |
<p> |
| 176 |
[% f = form.field('authors') %] |
| 177 |
<label class="label" for="[% f.name %]">[% f.label %]:</label> |
| 178 |
<select name="[% f.name %]" multiple="multiple" size="[% f.size %]"> |
| 179 |
[% FOR option IN f.options %] |
| 180 |
<option value="[% option.value %]" |
| 181 |
[% FOREACH selval IN f.fif %] |
| 182 |
[% IF selval == option.value %]selected="selected"[% END %] |
| 183 |
[% END %]> |
| 184 |
[% option.label | html %]</option> |
| 185 |
[% END %] |
| 186 |
</select> |
| 187 |
</p> |
| 188 |
<input class="button" name="submit" type="submit" value="Submit" /> |
| 189 |
</form> |
| 190 |
|
| 191 |
<p><a href="[% c.uri_for('list') %]">Return to book list</a></p> |
| 192 |
|
| 193 |
=head2 Add links to access create and update actions |
| 194 |
|
| 195 |
Add a link to root/src/books/list.tt2 to allow you to edit |
| 196 |
an existing book, by changing the last <td> cell in the book |
| 197 |
list: |
| 198 |
|
| 199 |
<td> |
| 200 |
<a href="[% c.uri_for('delete', book.id) %]">Delete</a>| |
| 201 |
<a href="[% c.uri_for('edit', book.id) %]">Edit</a> |
| 202 |
</td> |
| 203 |
|
| 204 |
Change the link to create a book at the bottom of the file: |
| 205 |
|
| 206 |
<p> |
| 207 |
<a href="[% c.uri_for('edit') %]">Create book</a> |
| 208 |
</p> |
| 209 |
|
| 210 |
=head2 Test the L<HTML::FormHandler> Create Form |
| 211 |
|
| 212 |
Start up the server for MyApp: |
| 213 |
|
| 214 |
$ script/myapp_server.pl |
| 215 |
|
| 216 |
(You'll need to login with test01/mypass if you're using the packaged |
| 217 |
tutorial.) Click the new "Create book" link at the bottom to display |
| 218 |
the form. Fill in the fields and click submit. You should be |
| 219 |
returned to the Book List page with a "Book saved" message. |
| 220 |
|
| 221 |
Magic! A new book has been created and saved to the database |
| 222 |
with very little code in your controller. |
| 223 |
|
| 224 |
Click on the 'edit' links, and edit the existing books. Changes |
| 225 |
should be saved and displayed properly. Try to add an alphabetic |
| 226 |
character to the rating field. You should get an error message. |
| 227 |
|
| 228 |
=head2 Add additional attributes to your form's fields |
| 229 |
|
| 230 |
We'll add a couple of 'label' attributes to the fields: |
| 231 |
|
| 232 |
has_field 'title' => ( type => 'Text', label => 'Title of a Book' ); |
| 233 |
has_field 'rating' => ( type => 'Integer', label => 'Rating (1-5)' ); |
| 234 |
has_field 'authors' => ( type => 'Multiple', label_column => 'last_name' ); |
| 235 |
|
| 236 |
If you want a new attribute in your fields, it's very easy to add it to your |
| 237 |
custom Field classes. |
| 238 |
|
| 239 |
package MyApp::Form::Field::Extra; |
| 240 |
use Moose; |
| 241 |
extends 'HTML::FormHandler::Field'; |
| 242 |
|
| 243 |
has 'my_attribute' => ( isa => Str, is => 'ro' ); |
| 244 |
|
| 245 |
1; |
| 246 |
|
| 247 |
Now if your Field classes inherit from this, you can have a 'my_attribute' |
| 248 |
attribute for all your fields. Or use a Moose role instead of inheritance. |
| 249 |
|
| 250 |
You can also add attributes to the base FormHandler field class using Moose. |
| 251 |
This technique is described in L<HTML::FormHandler::Manual::Cookbook>. |
| 252 |
|
| 253 |
=head1 L<HTML::FormHandler> Validation |
| 254 |
|
| 255 |
Now we'll add more validation to ensure that users |
| 256 |
are entering correct data. |
| 257 |
|
| 258 |
Update the fields in the form file: |
| 259 |
|
| 260 |
has_field 'title' => ( type => 'Text', label => 'Title of a Book', |
| 261 |
required => 1, size => 40, minlength => 5 ); |
| 262 |
has_field 'rating' => ( type => 'Integer', label => 'Rating (1-5)', |
| 263 |
required => 1, messages => { required => 'You must rate the book' }, |
| 264 |
range_start => 1, range_end => 5 ); |
| 265 |
has_field 'authors' => ( type => 'Multiple', label_column => 'last_name', |
| 266 |
required => 1 ); |
| 267 |
|
| 268 |
We've made all the fields required. |
| 269 |
We added 'size' and 'minlength' attributes to the 'title' field. These |
| 270 |
are attributes of the 'Text' Field, which will use them to validate. |
| 271 |
We've added 'range_start' and 'range_end' attributes to the 'rating' field. |
| 272 |
Numbers entered in the form will be checked to make sure they fall within |
| 273 |
the defined range. (Another option would have been to use the 'IntRange' |
| 274 |
field type, which makes it easy to create a select list of numbers.) |
| 275 |
|
| 276 |
=head2 Add customized validation |
| 277 |
|
| 278 |
You can create a Field class for validation that will |
| 279 |
be performed on more than one field, but it is easy to perform |
| 280 |
custom validation on a per-field basis. |
| 281 |
|
| 282 |
This form doesn't really require any customized validation, |
| 283 |
so we'll add a silly field constraint. Add the following to the form: |
| 284 |
|
| 285 |
sub validate_title { |
| 286 |
my ( $self, $field ) = @_; |
| 287 |
$field->add_error("The word \'Rainbows\' is not allowed in titles") |
| 288 |
if ( $field->value =~ /Rainbows/ ); |
| 289 |
} |
| 290 |
|
| 291 |
You can also apply Moose constraints and transforms. Validation can also |
| 292 |
be performed in a form 'validate_<field_name' method, in a 'validate_model' |
| 293 |
routine, and in a custom field class. |
| 294 |
You can validate that the field is unique, or use a dependency list to make |
| 295 |
more fields required if one is updated. |
| 296 |
|
| 297 |
=head2 Check out the validation |
| 298 |
|
| 299 |
Restart the development server, login, and try adding books with |
| 300 |
various errors: title length less than 5 or more than 40, rating |
| 301 |
above 5, leaving out a particular field. Create a book with |
| 302 |
'Rainbows' in the title. |
| 303 |
|
| 304 |
You should get error messages for every error. |
| 305 |
|
| 306 |
=head1 AUTHOR |
| 307 |
|
| 308 |
FormHandler Contributors - see HTML::FormHandler |
| 309 |
|
| 310 |
=head1 COPYRIGHT AND LICENSE |
| 311 |
|
| 312 |
This software is copyright (c) 2017 by Gerda Shank. |
| 313 |
|
| 314 |
This is free software; you can redistribute it and/or modify it under |
| 315 |
the same terms as the Perl 5 programming language system itself. |
| 316 |
|
| 317 |
=cut |