| 1 |
package HTML::FormHandler::Manual::Defaults; |
| 2 |
# ABSTRACT: form defaults documentation |
| 3 |
|
| 4 |
__END__ |
| 5 |
|
| 6 |
=pod |
| 7 |
|
| 8 |
=encoding UTF-8 |
| 9 |
|
| 10 |
=head1 NAME |
| 11 |
|
| 12 |
HTML::FormHandler::Manual::Defaults - form defaults documentation |
| 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 set defaults for your fields. |
| 23 |
|
| 24 |
=head1 Defaults |
| 25 |
|
| 26 |
Defaults for form fields come from a number of different places. The simplest |
| 27 |
way to set a field's default is on the field definition: |
| 28 |
|
| 29 |
has_field 'foo' => ( type => 'Text', default => 'my_foo' ); |
| 30 |
has_field 'select_many' => ( type => 'Multiple', default => [1, 2, 3] ); |
| 31 |
|
| 32 |
You can also set the default for a field with a method in the form with the name |
| 33 |
'default_<field_name>', where any periods in the field name are replaced |
| 34 |
with underscores. |
| 35 |
|
| 36 |
has_field 'foo'; |
| 37 |
sub default_foo { 'my_default' } |
| 38 |
|
| 39 |
Like other field attributes, the 'default' attribute can be modified on |
| 40 |
new with the 'field_list' attribute, or on 'process' with the 'update_field_list' |
| 41 |
parameter (or the shorthand form 'defaults'). |
| 42 |
|
| 43 |
my $form => MyApp::Form->new( field_list => { '+foo' => { default => 'my_foo' } } ); |
| 44 |
$form->process( update_field_list => { foo => { default => 'my_foo' } } ); |
| 45 |
$form->process( defaults => { foo => 'my_foo' }, params => $params ); |
| 46 |
|
| 47 |
For forms where you pass in an 'item' (usually a database row object), the |
| 48 |
values in that object will be used preferentially; if an accessor exists |
| 49 |
in the 'item' object, then the defaults won't be used. (If an accessor |
| 50 |
doesn't exist, the defaults *will* be used.) |
| 51 |
|
| 52 |
$form->process( item => $row, params => {} ); |
| 53 |
|
| 54 |
For the above call the 'default' on the field will not be used, which is |
| 55 |
usually what you want. |
| 56 |
|
| 57 |
When creating a new database record with your form, if you don't pass in an |
| 58 |
empty row, then the field defaults will be used, or you can provide defaults |
| 59 |
in an 'init_object'. |
| 60 |
|
| 61 |
note: the form class has 'item_class' set already. |
| 62 |
$form->process( schema => $schema, init_object => $obj ... ); |
| 63 |
|
| 64 |
If you provide an empty row object for 'create' type |
| 65 |
actions, however, you might want some defaults filled in. This can |
| 66 |
be done by filling the values into the row object or by turning on the form |
| 67 |
flag 'use_defaults_over_obj'. |
| 68 |
|
| 69 |
$form->process( item => $empty_row, use_defaults_over_obj => 1 ); |
| 70 |
|
| 71 |
If you always want new DBIC results to be ignored, you could set the flag in |
| 72 |
a base form method: |
| 73 |
|
| 74 |
sub set_active { |
| 75 |
my $self = shift; |
| 76 |
$self->next::method; |
| 77 |
if ( $self->item and ! $self->item->in_storage ) { |
| 78 |
$self->use_defaults_over_obj(1); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
You could also pass in another object or hashref in the 'init_object' attribute, |
| 83 |
and set the 'use_init_obj_over_item' flag: |
| 84 |
|
| 85 |
$form->process( item => $empty_row, init_object => $example, |
| 86 |
use_init_obj_over_item => 1 ); |
| 87 |
|
| 88 |
Note that the 'use_init_obj_over_item' and 'use_defaults_over_obj' flags |
| 89 |
are automatically cleared (if you're using persistent forms). |
| 90 |
|
| 91 |
For forms where some defaults come from a database row, and some defaults |
| 92 |
come from some other dynamic source (so that putting them into the field |
| 93 |
definitions doesn't make sense), you can use the |
| 94 |
'use_init_obj_when_no_accessor_in_item' flag to provide two different |
| 95 |
sets of defaults, one set in the 'item' (usually a db row) and one set in |
| 96 |
the init_obj. If the 'item' is undefined, the values in the init_object |
| 97 |
are used. |
| 98 |
|
| 99 |
in form: has '+use_init_obj_when_no_accessor_in_item' => ( default => 1 ); |
| 100 |
$form->process( item => $item, init_object => { foo => '...' }, .. ); |
| 101 |
|
| 102 |
There is a convenience method for setting 'defaults' on a number of fields at |
| 103 |
once, the form's 'defaults' attribute, which uses the same mechanism as |
| 104 |
'update_field_list' but only sets defaults. Note that this hashref is |
| 105 |
structured like the update_field_list with regard to field names, |
| 106 |
while the 'init_object' uses "structured" data: |
| 107 |
|
| 108 |
my $defaults = { |
| 109 |
model => 'standard', |
| 110 |
'opts.color' => 'Red', |
| 111 |
'opts.size' => 'Big', |
| 112 |
}; |
| 113 |
my $init_object => { |
| 114 |
model => 'standard', |
| 115 |
opts => { color => 'Red', size => 'Big' } |
| 116 |
}; |
| 117 |
|
| 118 |
$form->process( defaults => $defaults, ... ); |
| 119 |
$form->process( init_object => $init_object ... ); |
| 120 |
|
| 121 |
In addition, the 'defaults' actually changes the 'default' stored in |
| 122 |
the field definitions, while the init_object does not. |
| 123 |
|
| 124 |
There is also an alternative attribute in the fields, 'default_over_obj', but |
| 125 |
the new 'use_defaults_over_obj' and 'use_init_obj_over_item' flags, make it less |
| 126 |
necessary. Note that the 'default_over_obj' attribute only provides a default if |
| 127 |
an item/init_object and accessor exists. |
| 128 |
|
| 129 |
=head2 Defaults when processing params |
| 130 |
|
| 131 |
Normally when a form is posted, the params will contain all the values |
| 132 |
that are necessary to fill in the form. However, when a form is used in |
| 133 |
an API-like fashion, such as complex search forms, sometimes it is |
| 134 |
convenient to only provide particular params and let the others use |
| 135 |
defaults. However when the results are built from input, fields with |
| 136 |
no input are skipped unless the field has a value for 'input_without_param'. |
| 137 |
There is an additional form-level flag, 'use_fields_for_input_without_param' |
| 138 |
which will cause fields with no param entry to be built from the fields. |
| 139 |
This means that 'defaults' on the field will be used to provide a value |
| 140 |
and an input for the field. |
| 141 |
|
| 142 |
=head2 Query parameters for defaults |
| 143 |
|
| 144 |
You can use either the 'defaults' hashref or the 'init_object' to provide query |
| 145 |
parameter 'defaults'. They should not be provided in the 'params' hash, because |
| 146 |
then FormHandler will assume that the form has been posted and attempt to validate, |
| 147 |
which you don't want to do until the form has been submitted. Or you can use |
| 148 |
the 'posted' flag, to indicate whether or not to perform validation: |
| 149 |
|
| 150 |
$form->process( posted => ( $c->req->method eq 'POST' ), params => $c->req->params ); |
| 151 |
|
| 152 |
Note that in Catalyst, there are 'query_parameters' and 'body_parameters'. The |
| 153 |
'parameters' contains both 'query_parameters' and 'body_parameters'. |
| 154 |
|
| 155 |
=head1 AUTHOR |
| 156 |
|
| 157 |
FormHandler Contributors - see HTML::FormHandler |
| 158 |
|
| 159 |
=head1 COPYRIGHT AND LICENSE |
| 160 |
|
| 161 |
This software is copyright (c) 2017 by Gerda Shank. |
| 162 |
|
| 163 |
This is free software; you can redistribute it and/or modify it under |
| 164 |
the same terms as the Perl 5 programming language system itself. |
| 165 |
|
| 166 |
=cut |