1HTML::FormHandler::FielUds:e:rReCpoenattraibbluet(e3d)PHeTrMlL:D:oFcourmmeHnatnadtlieorn::Field::Repeatable(3)
2
3
4
6 HTML::FormHandler::Field::Repeatable - repeatable (array) field
7
9 version 0.40068
10
12 In a form, for an array of hashrefs, equivalent to a 'has_many'
13 database relationship.
14
15 has_field 'addresses' => ( type => 'Repeatable' );
16 has_field 'addresses.address_id' => ( type => 'PrimaryKey' );
17 has_field 'addresses.street';
18 has_field 'addresses.city';
19 has_field 'addresses.state';
20
21 In a form, for an array of single fields (not directly equivalent to a
22 database relationship) use the 'contains' pseudo field name:
23
24 has_field 'tags' => ( type => 'Repeatable' );
25 has_field 'tags.contains' => ( type => 'Text',
26 apply => [ { check => ['perl', 'programming', 'linux', 'internet'],
27 message => 'Not a valid tag' } ]
28 );
29
30 or use 'contains' with single fields which are compound fields:
31
32 has_field 'addresses' => ( type => 'Repeatable' );
33 has_field 'addresses.contains' => ( type => '+MyAddress' );
34
35 If the MyAddress field contains fields 'address_id', 'street', 'city',
36 and 'state', then this syntax is functionally equivalent to the first
37 method where the fields are declared with dots ('addresses.city');
38
39 You can pass attributes to the 'contains' field by supplying an
40 'init_contains' hashref.
41
42 has_field 'addresses' => ( type => 'Repeatable,
43 init_contains => { wrapper_attr => { class => ['hfh', 'repinst'] } },
44 );
45
47 This class represents an array. It can either be an array of hashrefs
48 (compound fields) or an array of single fields.
49
50 The 'contains' keyword is used for elements that do not have names
51 because they are not hash elements.
52
53 This field node will build arrays of fields from the parameters or an
54 initial object, or empty fields for an empty form.
55
56 The name of the element fields will be an array index, starting with 0.
57 Therefore the first array element can be accessed with:
58
59 $form->field('tags')->field('0')
60 $form->field('addresses')->field('0')->field('city')
61
62 or using the shortcut form:
63
64 $form->field('tags.0')
65 $form->field('addresses.0.city')
66
67 The array of elements will be in "$form->field('addresses')->fields".
68 The subfields of the elements will be in a fields array in each
69 element.
70
71 foreach my $element ( $form->field('addresses')->fields )
72 {
73 foreach my $field ( $element->fields )
74 {
75 # do something
76 }
77 }
78
79 Every field that has a 'fields' array will also have an 'error_fields'
80 array containing references to the fields that contain errors.
81
82 Complications
83 When new elements are created by a Repeatable field in a database form
84 an attempt is made to re-load the Repeatable field from the database,
85 because otherwise the repeatable elements will not have primary keys.
86 Although this works, if you have included other fields in your
87 repeatable elements that do *not* come from the database, the
88 defaults/values must be able to be loaded in a way that works when the
89 form is initialized from the database item. This is only an issue if
90 you re-present the form after the database update succeeds.
91
93 index
94 This attribute contains the next index number available to create
95 an additional array element.
96
97 init_contains
98 When the Repeatable is repeated, this hashref will be used to
99 initialise each instance. It can contain any of the same attributes
100 you would normally give to "has_field" in HTML::FormHandler, except
101 "type", for obvious reasons.
102
103 has_field 'addresses' => (
104 type => 'Repeatable',
105 init_contains => {
106 wrapper_attr => {
107 rel => 'address'
108 },
109 wrapper_class => [ 'row' ]
110 }
111 );
112
113 Note that providing a "wrapper_class" to this hashref will override
114 the default "hfh-repinst" class on these instances.
115
116 num_when_empty
117 This attribute (default 1) indicates how many empty fields to
118 present in an empty form which hasn't been filled from parameters
119 or database rows.
120
121 num_extra
122 When the field results are built from an existing object (item or
123 init_object) an additional number of repeatable elements will be
124 created equal to this number. Default is 0.
125
126 add_extra
127 When a form is submitted and the field results are built from the
128 input parameters, it's not clear when or if an additional
129 repeatable element might be wanted. The method 'add_extra' will add
130 an empty repeatable element.
131
132 $form->process( params => {....} );
133 $form->field('my_repeatable')->add_extra(1);
134
135 This might be useful if the form is being re-presented to the user.
136
137 setup_for_js
138 setup_for_js => 1
139
140 Saves information in the form for javascript to use when adding
141 repeatable elements. If using the example javascript, you also
142 must set 'do_wrapper' in the Repeatable field and use the Bootstrap
143 widget wrapper (or wrap the repeatable elements in a 'controls' div
144 by setting tags => { controls_div => 1 }. See t/repeatable/js.t
145 for an example. See also HTML::FormHandler::Render::RepeatableJs
146 and HTML::FormHandler::Field::AddElement.
147
149 FormHandler Contributors - see HTML::FormHandler
150
152 This software is copyright (c) 2017 by Gerda Shank.
153
154 This is free software; you can redistribute it and/or modify it under
155 the same terms as the Perl 5 programming language system itself.
156
157
158
159perl v5.36.0 2022-0H7T-M2L2::FormHandler::Field::Repeatable(3)