| 1 |
package HTML::FormHandler::Manual::Intro; |
| 2 |
# ABSTRACT: introduction to using FormHandler |
| 3 |
|
| 4 |
__END__ |
| 5 |
|
| 6 |
=pod |
| 7 |
|
| 8 |
=encoding UTF-8 |
| 9 |
|
| 10 |
=head1 NAME |
| 11 |
|
| 12 |
HTML::FormHandler::Manual::Intro - introduction to using FormHandler |
| 13 |
|
| 14 |
=head1 VERSION |
| 15 |
|
| 16 |
version 0.410002 |
| 17 |
|
| 18 |
=head1 SYNOPSIS |
| 19 |
|
| 20 |
L<Manual Index|HTML::FormHandler::Manual> |
| 21 |
|
| 22 |
HTML::FormHandler is a form handling package that validates HTML form data |
| 23 |
and, for database forms, saves it to the database on validation. |
| 24 |
It has field classes that match various data types and HTML form elements, |
| 25 |
and rendering roles that can be used to render forms in many |
| 26 |
different ways, from hand-built forms to totally automated rendering. |
| 27 |
It can, of course, be used to validate data even if you are not interested |
| 28 |
in the rendering capabilities. |
| 29 |
|
| 30 |
A FormHandler 'form' is a Perl subclass of L<HTML::FormHandler> for non-database forms, |
| 31 |
or a subclass of L<HTML::FormHandler::Model::DBIC> for database forms, and in |
| 32 |
it you define your fields and validation routines. Because it's a Perl class |
| 33 |
written in Moose, you have a lot of flexibility and control. |
| 34 |
|
| 35 |
You can validate with Perl methods or Moose type constraints; you can use |
| 36 |
your own validation libraries. You can define your own field classes that |
| 37 |
perform specialized validation. |
| 38 |
|
| 39 |
When the form is validated, you can get the validated values back with |
| 40 |
C<< $form->value >>. |
| 41 |
|
| 42 |
A working example of a Catalyst app using FormHandler forms is available |
| 43 |
on github at L<https://github.com/gshank/formhandler-example>. |
| 44 |
|
| 45 |
=head1 Basics |
| 46 |
|
| 47 |
=head2 Create a form class |
| 48 |
|
| 49 |
The most common way of using FormHandler is to create a form package. You |
| 50 |
must 'use' "HTML::FormHandler::Moose" and 'extend' FormHandler: |
| 51 |
|
| 52 |
package MyApp::Form::Sample; |
| 53 |
use HTML::FormHandler::Moose; |
| 54 |
extends 'HTML::FormHandler'; |
| 55 |
|
| 56 |
Then you add some fields with 'has_field', and a field 'type' (the short |
| 57 |
name of the field package). (Fields with no type have type 'Text'.) |
| 58 |
|
| 59 |
has_field 'foo'; |
| 60 |
has_field 'bar' => ( type => 'Select' ); |
| 61 |
|
| 62 |
Basic field types are Text, Select, Checkbox, Submit, Hidden, Reset, |
| 63 |
TextArea, Password, Upload. See L<HTML::FormHandler::Manual::Fields> for |
| 64 |
more information. |
| 65 |
|
| 66 |
=head2 Or create a form class dynamically |
| 67 |
|
| 68 |
You can also create a form class 'dynamically', by creating a 'new' |
| 69 |
HTML::FormHandler object. Use a 'field_list' parameter to create the fields |
| 70 |
instead of 'has_field'. |
| 71 |
|
| 72 |
my $form = HTML::FormHandler->new( field_list => [ |
| 73 |
'username' => { type => 'Text' }, |
| 74 |
'selections' => { type => 'Select' }, |
| 75 |
] |
| 76 |
); |
| 77 |
|
| 78 |
Some features will not be available using this method (like the automatic |
| 79 |
use of 'validate_<field_name>' methods) and it's not as easy to test, |
| 80 |
of course. |
| 81 |
|
| 82 |
=head2 Process the form |
| 83 |
|
| 84 |
The form's 'process' method should be run on each request, passing in the |
| 85 |
request parameters: |
| 86 |
|
| 87 |
$form->process( params => $c->request->body_parameters, |
| 88 |
action => $action, |
| 89 |
); |
| 90 |
|
| 91 |
If the parameters are not empty, then validation will be performed. The |
| 92 |
corollary is that you should not pass in extra parameters when the form |
| 93 |
has not been posted. A special 'posted' flag can be used if |
| 94 |
the form consists entirely of fields like checkboxes that do not include |
| 95 |
names in params if unchecked, and also works to prevent validation from |
| 96 |
being performed if there are extra params: |
| 97 |
|
| 98 |
$form->process( posted => ( $c->req->method eq 'POST' ), |
| 99 |
params => $c->request->parameters, action => $action ); |
| 100 |
|
| 101 |
There is an alternative method for processing the form, which is sometimes |
| 102 |
preferred for persistent forms. It returns a 'result' object, and clears |
| 103 |
the form: |
| 104 |
|
| 105 |
my $result = $form->run( params => $c->request->body_parameters ); |
| 106 |
|
| 107 |
You can also set most other FormHandler attributes on the 'process' call., |
| 108 |
One useful feature is that you can activate or inactivate fields: |
| 109 |
|
| 110 |
$form->process( params => $params, active => ['field1', 'field2'] ); |
| 111 |
|
| 112 |
See also L<HTML::FormHandler>. |
| 113 |
|
| 114 |
=head2 Or process a database form |
| 115 |
|
| 116 |
A database form inherits from L<HTML::FormHandler::Model::DBIC> instead of |
| 117 |
L<HTML::FormHandler>. You must either pass in the DBIC row object or give |
| 118 |
FormHandler information to retrieve the row object. |
| 119 |
|
| 120 |
$form->process( item => $row, params => $params ); |
| 121 |
-- or -- |
| 122 |
$form->process( item_id => $id, schema => $schema, |
| 123 |
item_class => 'MyRow', params => $params ); |
| 124 |
|
| 125 |
'item_class' is often set in the form class. |
| 126 |
|
| 127 |
See also L<HTML::FormHandler::Manual::Database> and |
| 128 |
L<HTML::FormHandler::TraitFor::Model::DBIC>. |
| 129 |
|
| 130 |
=head2 After processing the form |
| 131 |
|
| 132 |
A database form will have saved the data or created a new row, so often no |
| 133 |
more processing is necessary. You can get the structured field values from |
| 134 |
C<< $form->value >>, and do whatever you want with them. |
| 135 |
|
| 136 |
If the validation succeeded, you may want to redirect: |
| 137 |
|
| 138 |
$form->process( params => $params ); |
| 139 |
return unless $form->validated |
| 140 |
$c->res->redirect( .... ); |
| 141 |
-- or -- |
| 142 |
return unless $form->process( params => params ); |
| 143 |
$c->res->redirect; |
| 144 |
|
| 145 |
=head2 Rendering the form |
| 146 |
|
| 147 |
At its simplest, all you need to do is C<< $form->render >> in a |
| 148 |
template. |
| 149 |
|
| 150 |
[% form.render %] |
| 151 |
|
| 152 |
The automatic rendering is powerful and flexible -- you can do almost |
| 153 |
anything with the right settings. Or you can render the form with a |
| 154 |
template. |
| 155 |
|
| 156 |
The form object will give you a hashref of values suitable for |
| 157 |
filling in the form with C<< $form->fif >>. |
| 158 |
|
| 159 |
By default FormHandler structures fields (and renders them) in a way |
| 160 |
that matches the database. If you want to organize the rendering output |
| 161 |
in different ways, you can use blocks to organize your fields. |
| 162 |
|
| 163 |
has_block 'fieldset1' => ( render_list => ['foo', 'bar'] ); |
| 164 |
|
| 165 |
For more rendering info, see L<HTML::FormHandler::Manual::Rendering>. |
| 166 |
|
| 167 |
=head2 Defaults for form fields |
| 168 |
|
| 169 |
The simplest way to provide defaults is by setting the default attribute |
| 170 |
in a field definition: |
| 171 |
|
| 172 |
has_field 'my_foo' => ( default => 'my_foo' ); |
| 173 |
|
| 174 |
The database row ('item') that is passed in will provide initial values |
| 175 |
for the form, of course. You can also provide default values with an |
| 176 |
'init_object', which acts kind of like a database object: |
| 177 |
|
| 178 |
$form->process( init_object => { foo => '...', bar => '...' } ); |
| 179 |
|
| 180 |
There are a number of other flags and methods for providing defaults. |
| 181 |
See L<HTML::FormHandler::Manual::Defaults>. |
| 182 |
|
| 183 |
=head2 Validation |
| 184 |
|
| 185 |
You can validate a field with a method in the form 'validate_<field_name>': |
| 186 |
|
| 187 |
has_field 'foo'; |
| 188 |
sub validate_foo { |
| 189 |
my ( $self, $field ) = @_; # self is the form |
| 190 |
unless( $field->value == .... ) { |
| 191 |
$field->add_error( .... ); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
You can provide a validation coderef that will be a field method: |
| 196 |
|
| 197 |
has_field 'foo' => ( validate_method => \&check_foo ); |
| 198 |
sub check_foo { |
| 199 |
my $self = shift; # self is field |
| 200 |
unless( $self->value == ... ) { |
| 201 |
$self->add_error( ... ); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
You can use 'apply' to use Moose types for validation, from L<HTML::FormHandler::Types> |
| 206 |
or another Moose type collection: |
| 207 |
|
| 208 |
use HTML::FormHandler::Types ('NotAllDigits'); |
| 209 |
... |
| 210 |
has_field 'my_field' => ( apply => [NotAllDigits] ); |
| 211 |
|
| 212 |
Or create validators with check: |
| 213 |
|
| 214 |
has_field 'quux' => ( |
| 215 |
apply => [ { check => qr/abc/, message => 'Not a valid quux' } ] ); |
| 216 |
|
| 217 |
You can also create custom fields with custom validation, or use an |
| 218 |
existing field that does the validation you need. |
| 219 |
|
| 220 |
See L<HTML::FormHandler::Manual::Validation> for more information on |
| 221 |
validation or L<HTML::FormHandler::Manual::Fields> for more information |
| 222 |
on fields. |
| 223 |
|
| 224 |
=head2 Organizing your form code |
| 225 |
|
| 226 |
You can use 'has_field' and 'has_block' in Moose roles: |
| 227 |
|
| 228 |
package MyApp::Form::Role::Address; |
| 229 |
use HTML::FormHandler::Moose::Role; |
| 230 |
|
| 231 |
has_field 'foo'; |
| 232 |
has_block 'bar'; |
| 233 |
|
| 234 |
Your forms can inherit from base classes that set common application |
| 235 |
defaults. You can override field definitions with '+'. |
| 236 |
|
| 237 |
You can create 'compound' fields and include them in a form: |
| 238 |
|
| 239 |
package MyApp::Form::Field::Complex; |
| 240 |
use HTML::FormHandler::Moose; |
| 241 |
extends 'HTML::FormHandler::Field::Compound'; |
| 242 |
has_field 'field1' => ( validate_method => \&validate_field1 ); |
| 243 |
has_field 'field2' => ( type => 'Select', |
| 244 |
options_method => \&options_field2 ); |
| 245 |
sub validate_field1 { ... } |
| 246 |
sub options_field2 { ... } |
| 247 |
... |
| 248 |
package MyApp::Form::Complex; |
| 249 |
use HTML::FormHandler::Moose; |
| 250 |
extends 'HTML::FormHandler'; |
| 251 |
has '+field_name_space' => ( default => 'MyApp::Form::Field' ); |
| 252 |
has_field 'compound1' => ( type => 'Complex' ); |
| 253 |
has_field 'compound2' => ( type => 'Complex' ); |
| 254 |
|
| 255 |
=head2 Testing |
| 256 |
|
| 257 |
It's much easier to write unit tests for FormHandler forms than for |
| 258 |
Catalyst controllers. The 't' directory of the downloaded distribution |
| 259 |
has lots of examples. See L<HTML::FormHandler::Manual::Testing> for more |
| 260 |
information. |
| 261 |
|
| 262 |
=head1 Localization |
| 263 |
|
| 264 |
FormHandler's built-in errors are added to the form fields with |
| 265 |
C<< $field->add_error >>, and to the form with C<< $form->add_form_error >>. |
| 266 |
These methods call a C<< $self->_localize >> method which is a coderef set from |
| 267 |
the field's default_localize sub, the field's 'localize_meth' attribute with |
| 268 |
C<< localize_meth => sub {} >>, or a form's sub localize_meth. The default localize |
| 269 |
uses Locale::Maketext. You can also use duck_type classes for localization. |
| 270 |
See the documentation in L<HTML::FormHandler::TraitFor::I18N> and the tests in xt/locale.t. |
| 271 |
|
| 272 |
If you wish to skip localization for a particular message (such as for system errors) |
| 273 |
you can use C<< $field->push_errors >> or C<< $form->push_form_errors >>. |
| 274 |
|
| 275 |
See also L<HTML::FormHandler::TraitFor::I18N>. |
| 276 |
|
| 277 |
=head1 Performance |
| 278 |
|
| 279 |
FormHandler makes heavy use of Moose, so almost all of FormHandler's profiled time |
| 280 |
will actually be in Moose methods, mostly constructing form and field attributes. |
| 281 |
Some people prefer to use a persistent form class (in a Moose attribute) in order |
| 282 |
to skip the form building step on each call. Other people don't like that solution |
| 283 |
because state will remain in the form until the next process call. The 'clear' |
| 284 |
method is called at the beginning of each 'process', but additional Moose attributes |
| 285 |
in the form, etc, will have to cleared by the programmer. |
| 286 |
|
| 287 |
If you are loading options from the database and you don't need to have them refreshed |
| 288 |
each time, you can set the 'do_not_reload' flag in the Select/Multiple field. |
| 289 |
If you're not using the field widget roles, you can set the 'no_widgets' flag. |
| 290 |
If you always use 'process' on each call (recommended) then you can set the |
| 291 |
'no_preload' flag in the form to skip building results in BUILD (new). |
| 292 |
|
| 293 |
=head1 AUTHOR |
| 294 |
|
| 295 |
FormHandler Contributors - see HTML::FormHandler |
| 296 |
|
| 297 |
=head1 COPYRIGHT AND LICENSE |
| 298 |
|
| 299 |
This software is copyright (c) 2017 by Gerda Shank. |
| 300 |
|
| 301 |
This is free software; you can redistribute it and/or modify it under |
| 302 |
the same terms as the Perl 5 programming language system itself. |
| 303 |
|
| 304 |
=cut |