| 1 |
package HTML::FormHandler::Manual::InflationDeflation; |
| 2 |
# ABSTRACT: inflation and deflation of field values |
| 3 |
|
| 4 |
__END__ |
| 5 |
|
| 6 |
=pod |
| 7 |
|
| 8 |
=encoding UTF-8 |
| 9 |
|
| 10 |
=head1 NAME |
| 11 |
|
| 12 |
HTML::FormHandler::Manual::InflationDeflation - inflation and deflation of field values |
| 13 |
|
| 14 |
=head1 VERSION |
| 15 |
|
| 16 |
version 0.410002 |
| 17 |
|
| 18 |
=head1 SYNOPSIS |
| 19 |
|
| 20 |
L<Manual Index|HTML::FormHandler::Manual> |
| 21 |
|
| 22 |
How to inflate and deflate field values. |
| 23 |
|
| 24 |
=head2 DESCRIPTION |
| 25 |
|
| 26 |
When working with the various ways that data can be transformed, in and out, |
| 27 |
the meaning of the terms 'inflate' and 'deflate' starts to feel kind of slippery. |
| 28 |
The one constant is that values presented in an HTML form must be in a string |
| 29 |
format, or presented with select elements or checkboxes. |
| 30 |
|
| 31 |
There are two general types of inflation/deflation provided by FormHandler. |
| 32 |
The first, 'standard' type inflates values in order to validate them, and deflates them |
| 33 |
in order to present them in string format via HTML. The other ('DB') type takes |
| 34 |
values provided by defaults (usually a DB row, or item, but could also be |
| 35 |
a field default or an init_object) and munges the values coming in and |
| 36 |
changes them back going out. |
| 37 |
|
| 38 |
=head2 Standard inflation/deflation |
| 39 |
|
| 40 |
The standard type of inflation/deflation is implemented by using some of the following |
| 41 |
options for inflation: |
| 42 |
|
| 43 |
inflate_method |
| 44 |
transform (using 'apply') |
| 45 |
|
| 46 |
..and the following options for deflation: |
| 47 |
|
| 48 |
deflate_method |
| 49 |
deflation (field attribute) |
| 50 |
|
| 51 |
When validation starts, the param input will be inflated by the inflate method, |
| 52 |
allowing validation to be performed on the inflated object. |
| 53 |
|
| 54 |
When the 'fif' fill-in-form value is returned for HTML generation, the deflation |
| 55 |
is used to flatten the object, usually into a string format. |
| 56 |
|
| 57 |
=head2 DB inflation/deflation |
| 58 |
|
| 59 |
The 'DB' type of inflation/deflation uses 'inflate_default_method' for inflation, |
| 60 |
and 'deflate_value_method' for deflation. Deflation could also be handled by changing |
| 61 |
the value in one of the various validation methods. |
| 62 |
|
| 63 |
This type of inflation/deflation is, logically, just a different way of providing |
| 64 |
data munging around the defaults (item/init_object/default) and 'value' output. |
| 65 |
The same effect could be achieved by performing a transformation outside of |
| 66 |
FormHandler - if you were handling the database updates yourself. Since the DBIC |
| 67 |
model enables automatic database updates, this kind of inflation/deflation |
| 68 |
makes that easier. |
| 69 |
|
| 70 |
One circumstance in which this type of inflation/deflation is useful is when |
| 71 |
there's a single field in the database row object which you want to expand into |
| 72 |
a compound field in the form. |
| 73 |
|
| 74 |
=head2 Attributes used in deflation/inflation |
| 75 |
|
| 76 |
=head3 Inflation methods |
| 77 |
|
| 78 |
The field 'input' comes from the params that are passed in from the submission |
| 79 |
of the form, so the input will always be in string format if it comes from an |
| 80 |
HTTP request. It's also possible to pass in params in other formats, of course. |
| 81 |
Or the params could be pre-processed before passing in to FormHandler. |
| 82 |
|
| 83 |
You should not normally be changing the 'input' attribute of a field. If you |
| 84 |
want the changed field value to be used when re-presenting the form, such as |
| 85 |
when you're adopting a standard format for the field, you should set |
| 86 |
C<< fif_from_value => 1 >>. |
| 87 |
|
| 88 |
There are three options for standard inflation, or transforming the field's |
| 89 |
'input' to the field's 'value': |
| 90 |
|
| 91 |
=over 4 |
| 92 |
|
| 93 |
=item inflate_method |
| 94 |
|
| 95 |
Provide a method on the field which inflates the field 'input' (from params): |
| 96 |
|
| 97 |
has_field 'futility' => ( inflate_method => \&inflate_field ); |
| 98 |
sub inflate_field { |
| 99 |
my ( $self, $value ) = @_; |
| 100 |
.... |
| 101 |
return $value; |
| 102 |
} |
| 103 |
|
| 104 |
=item transform |
| 105 |
|
| 106 |
In a sequence of 'apply' actions, changes the format of the 'value' that is |
| 107 |
being validated. This might be useful if there are some validations that work |
| 108 |
on one format of the value, and some that work on another. |
| 109 |
|
| 110 |
=item set the value in validation methods |
| 111 |
|
| 112 |
In a validate method, you can change the format of the value, with $field->value(...); |
| 113 |
|
| 114 |
=back |
| 115 |
|
| 116 |
=head3 Deflation methods |
| 117 |
|
| 118 |
=over 4 |
| 119 |
|
| 120 |
=item deflate_method |
| 121 |
|
| 122 |
Most general purpose deflation method. Provide a coderef which is a method |
| 123 |
on the field: |
| 124 |
|
| 125 |
has_field => 'foo' => ( deflate_method => \&deflate_foo ); |
| 126 |
sub deflate_foo { |
| 127 |
my ( $self, $value ) = @_; # $self is the 'foo' field |
| 128 |
<perform conversion> |
| 129 |
return $value; |
| 130 |
} |
| 131 |
|
| 132 |
=item deflation |
| 133 |
|
| 134 |
This is a coderef that performs deflation. |
| 135 |
|
| 136 |
has_field => 'bar' => ( deflation => sub { shift $value; ... return $value } ); |
| 137 |
|
| 138 |
=item set the value in validation methods |
| 139 |
|
| 140 |
Just like for inflation, you can change the value in a validation method; however, |
| 141 |
it won't be used for fill-in-form unless you set the 'fif_from_value' flag to true. |
| 142 |
|
| 143 |
=back |
| 144 |
|
| 145 |
=head3 fif_from_value |
| 146 |
|
| 147 |
Normally the fill-in-form value will be the param value that was submitted. |
| 148 |
If you want to change the format of the input when re-presenting the |
| 149 |
form, you can set 'fif_from_value'. |
| 150 |
|
| 151 |
=head3 deflate_to |
| 152 |
|
| 153 |
Earlier versions of FormHandler provided a 'deflate_to' attribute which |
| 154 |
allowed the deflation methods to be used for multiple, confusing purposes. This |
| 155 |
flag has been removed, since it made the process hard to understand and was |
| 156 |
mixing apples and oranges. The new inflation/deflation methods can handle |
| 157 |
all of the previous situations. |
| 158 |
|
| 159 |
=head1 AUTHOR |
| 160 |
|
| 161 |
FormHandler Contributors - see HTML::FormHandler |
| 162 |
|
| 163 |
=head1 COPYRIGHT AND LICENSE |
| 164 |
|
| 165 |
This software is copyright (c) 2017 by Gerda Shank. |
| 166 |
|
| 167 |
This is free software; you can redistribute it and/or modify it under |
| 168 |
the same terms as the Perl 5 programming language system itself. |
| 169 |
|
| 170 |
=cut |