| 1 |
package HTML::FormHandler::Manual::Database; |
| 2 |
# ABSTRACT: FormHandler use recipes |
| 3 |
|
| 4 |
__END__ |
| 5 |
|
| 6 |
=pod |
| 7 |
|
| 8 |
=encoding UTF-8 |
| 9 |
|
| 10 |
=head1 NAME |
| 11 |
|
| 12 |
HTML::FormHandler::Manual::Database - FormHandler use recipes |
| 13 |
|
| 14 |
=head1 VERSION |
| 15 |
|
| 16 |
version 0.410002 |
| 17 |
|
| 18 |
=head1 SYNOPSIS |
| 19 |
|
| 20 |
L<Manual Index|HTML::FormHandler::Manual> |
| 21 |
|
| 22 |
Information on interfacing FormHandler forms and fields with a database. |
| 23 |
Also see L<HTML::FormHandler::TraitFor::Model::DBIC>. |
| 24 |
|
| 25 |
=head1 Form Models |
| 26 |
|
| 27 |
For a database form, use a model base class that interfaces with the |
| 28 |
database, such as L<HTML::FormHandler::Model::DBIC>, which needs to |
| 29 |
be installed as a separate package. There's also a sample 'object' model |
| 30 |
in L<HTML::FormHandler::Model::Object>, which will update a simple object. |
| 31 |
|
| 32 |
When using a database model, form field values for the row are retrieved from |
| 33 |
the database using the field 'accessor' attributes (defaults to field name) |
| 34 |
as database class accessors. |
| 35 |
|
| 36 |
FormHandler will use relationships to populate single and multiple |
| 37 |
selection lists, and validate input. A 'single' relationship is processed |
| 38 |
by L<HTML::FormHandler::Field::Compound>. A 'has_many' relationship is |
| 39 |
processed by L<HTML::FormHandler::Field::Repeatable>. |
| 40 |
|
| 41 |
Do not use database row method names, such as 'delete', as field names in |
| 42 |
a database form. |
| 43 |
|
| 44 |
You can pass in either the primary key or a row object to the form. If a |
| 45 |
primary key (item_id) is passed in, you must also provide the schema. |
| 46 |
The model will use the item_class (DBIC source name) to fetch the row from the |
| 47 |
database. If you pass in a row object (item), the schema, item_class, and |
| 48 |
item_id will be set from the row. |
| 49 |
|
| 50 |
Executing C<< $form->process( item => $row, params => $params ); >> will validate |
| 51 |
the parameters and then update or create the database row object. |
| 52 |
|
| 53 |
=head1 Fields that map to database relationships |
| 54 |
|
| 55 |
=head2 Select |
| 56 |
|
| 57 |
A select field will automatically retrieve a select list from the database, |
| 58 |
if the proper column names are provided. Single selects handle 'belongs_to' |
| 59 |
relationships, where the related table is used to construct a selection list |
| 60 |
from the database. |
| 61 |
|
| 62 |
See also L<HTML::FormHandler::Field::Select> and 'lookup_options' in |
| 63 |
L<HTML::FormHandler::TraitFor::Model::DBIC>. |
| 64 |
|
| 65 |
=head2 Multiple Select |
| 66 |
|
| 67 |
A multiple select is either a 'Select' with multiple => 1 set, or a field |
| 68 |
of the 'Multiple' type. The name of a Multiple select which pulls options |
| 69 |
from the database automatically should be the name of the 'many_to_many' |
| 70 |
relationship. The 'value' of the field is derived from the 'has_many' |
| 71 |
part of the relationship. |
| 72 |
|
| 73 |
The primary key is used for the 'id' of the select. The 'label' column of |
| 74 |
the select is assumed to be 'name'. If the label column has a different |
| 75 |
name, it must be specified with 'label_column'. |
| 76 |
|
| 77 |
Pertinent attributes: |
| 78 |
|
| 79 |
label_column |
| 80 |
active_column |
| 81 |
sort_column |
| 82 |
|
| 83 |
See also L<HTML::FormHandler::Field::Select> and |
| 84 |
L<HTML::FormHandler::Model::DBIC>. |
| 85 |
|
| 86 |
=head2 Compound fields |
| 87 |
|
| 88 |
A compound field represents a single relationship to another table. |
| 89 |
Although most compound relations can be handled without providing a |
| 90 |
primary key, in some circumstances you may need to provide a |
| 91 |
PrimaryKey field, or add extra values in update_model. |
| 92 |
|
| 93 |
See also L<HTML::FormHandler::Field::Compound>. |
| 94 |
|
| 95 |
The default for compound fields is that if all subfields are empty, |
| 96 |
the value of the compound field is set to undef (null). For some types |
| 97 |
of relations, you may want to set the 'not_nullable' flag to force |
| 98 |
the field to contain all subfields anyway, such as when the related |
| 99 |
rows are not deleted when empty. See test t/compound/empty.t for a |
| 100 |
demonstration of the difference in output. |
| 101 |
|
| 102 |
=head2 Repeatable fields |
| 103 |
|
| 104 |
The 'Repeatable' field type allows you to update arrays of columns from related tables |
| 105 |
easily. You will need to provide a 'PrimaryKey' hidden field in the |
| 106 |
compound field contained in the Repeatable. |
| 107 |
|
| 108 |
has_field 'addresses' => ( type => 'Repeatable' ); |
| 109 |
has_field 'addresses.address_id' => ( type => 'PrimaryKey' ); |
| 110 |
has_field 'addresses.street'; |
| 111 |
has_field 'addresses.city'; |
| 112 |
has_field 'addresses.state'; |
| 113 |
|
| 114 |
There are some complications with creating Repeatable elements (with |
| 115 |
the PrimaryKey field set to undef) in a database and re-presenting the form. |
| 116 |
See L<HTML::FormHandler::Field::Repeatable> for more info. |
| 117 |
|
| 118 |
=head1 Flags |
| 119 |
|
| 120 |
=head2 writeonly |
| 121 |
|
| 122 |
Do not read the value from the 'item' when populating the form. |
| 123 |
|
| 124 |
=head2 noupdate |
| 125 |
|
| 126 |
Do not update the database with this field, i.e. do not include it in |
| 127 |
C<< $form->value >>. |
| 128 |
|
| 129 |
=head1 Form generator |
| 130 |
|
| 131 |
A DBIC form generator is installed with the L<HTML::FormHandler::Model::DBIC> |
| 132 |
package. See L<HTML::FormHandler::Generator::DBIC>. |
| 133 |
|
| 134 |
There's also a role, L<HTML::FormHandler::TraitFor::DBICFields>, that allows |
| 135 |
simple form fields to be auto-generated from a DBIC result class. |
| 136 |
|
| 137 |
my $form = HTML::FormHandler::Model::DBIC->new_with_traits( |
| 138 |
traits => ['HTML::FormHandler::TraitFor::DBICFields'], |
| 139 |
includes => ['title', 'author' ], |
| 140 |
field_list => [ 'submit' => { type => 'Submit', value => 'Save', order => 99 } ], |
| 141 |
item => $book ); |
| 142 |
|
| 143 |
=head1 AUTHOR |
| 144 |
|
| 145 |
FormHandler Contributors - see HTML::FormHandler |
| 146 |
|
| 147 |
=head1 COPYRIGHT AND LICENSE |
| 148 |
|
| 149 |
This software is copyright (c) 2017 by Gerda Shank. |
| 150 |
|
| 151 |
This is free software; you can redistribute it and/or modify it under |
| 152 |
the same terms as the Perl 5 programming language system itself. |
| 153 |
|
| 154 |
=cut |