Valiant/HTML/FormBuilder.pm

filter: Statementstmt 89.5% 488 / 545 Branchbran 70.7% 310 / 438 Conditioncond 60.4% 122 / 202 MC/DCmcdc 34.5% 39 / 113 Subroutinesub 85.5% 59 / 69 Podpod 57.1% 24 / 42 totaltotal 73.9% 1042 / 1409 CC 302 SCAR 30.9
CC
302
Cov
78%
fields_for
56.5
form_has_errors
56.0
collection_checkbox
43.0
CRAP 22.0
« Valiant/Validator/Numericality.pm ↑ Summary Valiant/Validates.pm »
1 package Valiant::HTML::FormBuilder;
2  
3 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Module::Runtime ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Valiant::I18N;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 with 'Valiant::Naming';
9  
10 # Non public helper methods
11  
12 sub set_unless_defined {
13 261 ▶ my ($key, $options, $value) = @_;
Subroutine set_unless_defined: called CC=3 SCAR=11.0
Pod undocumented
14 261 ▶ return if exists($options->{$key}) && defined($options->{$key});
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{$key} and defined $options->{$key} 77 184 261
Condition66%2/3
exists $options->{$key} and defined $options->{$key}
!ll&&!rl&&r
184077
MC/DC50%1/2
exists $options->{$key} and defined $options->{$key}
exists $options->{$key} defined $options->{$key}
Truth table66%2/3
A and B
A= exists $options->{$key}B= defined $options->{$key}
ABresult
0X0
100
111
15 184 $options->{$key} = $value;
16 }
17  
18 2 has model => ( is => 'ro', required => 1);
19 2 has name => ( is => 'ro', required => 1 );
20 0 ▶has options => ( is => 'ro', required => 1, default => sub { +{} } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
21 2 has index => ( is => 'ro', required => 0, predicate => 'has_index' );
22 2 has namespace => ( is => 'ro', required => 0, predicate => 'has_namespace' );
23 2 has tag_helpers => (
24 is => 'ro',
25 required => 1,
26 handles => [qw/view/],
27 builder => '_build_tag_helpers',
28 lazy => 1,
29 );
30  
31 sub _build_tag_helpers {
32 1 ▶ my ($self) = @_;
Subroutine _build_tag_helpers: called CC=2 SCAR=7.0
33 1 my %args = ();
34 $args{view} = exists($self->options->{view}) ?
35 $self->options->{view} :
36 1 ▶ Module::Runtime::use_module('Valiant::HTML::Util::View')->new;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->options->{'view'} ? : 0 1 1
37 1 return Module::Runtime::use_module('Valiant::HTML::Util::Form')->new(%args);
38 }
39  
40 44 ▶has _theme => ( is => 'rw', required => 1, init_arg=>'theme', default => sub { +{} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
41 44 ▶has _nested_child_index => (is=>'rw', init_arg=>undef, required=>1, default=>sub { +{} });
Subroutine __ANON__: called CC=1 SCAR=0.0
42  
43 around BUILDARGS => sub {
44 44 ▶ my ($orig, $class, @args) = @_;
Subroutine __ANON__: called CC=4 SCAR=14.0
45 44 my $options = $class->$orig(@args);
46  
47 44 ▶ $options->{index} = $options->{child_index} if !exists($options->{index}) && exists($options->{child_index});
Branch: 1 branch
BranchTrueFalseTotal
if not exists $options->{"index"} and exists $options->{"child_index"} 0 44 44
Condition66%2/3
not exists $options->{"index"} and exists $options->{"child_index"}
!ll&&!rl&&r
11330
MC/DC0%0/2
not exists $options->{"index"} and exists $options->{"child_index"}
not exists $options->{"index"} exists $options->{"child_index"}
Truth table66%2/3
not(A) and B
A= not exists $options->{"index"}B= exists $options->{"child_index"}
ABresult
0X0
100
111
48 44 ▶ $options->{options} = exists($options->{options}) ? +{%$options, %{$options->{options}}} : $options;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'options'} ? : 43 1 44
49  
50 44 return $options;
51 2 };
52  
53 0 ▶sub DEFAULT_MODEL_ERROR_MSG_ON_FIELD_ERRORS { return 'Your form has errors' }
Subroutine DEFAULT_MODEL_ERROR_MSG_ON_FIELD_ERRORS: not called CC=1 SCAR=6.9
Pod undocumented
54 0 ▶sub DEFAULT_MODEL_ERROR_TAG_ON_FIELD_ERRORS { return 'invalid_form' }
Subroutine DEFAULT_MODEL_ERROR_TAG_ON_FIELD_ERRORS: not called CC=1 SCAR=6.9
Pod undocumented
55 3 ▶sub DEFAULT_COLLECTION_CHECKBOX_BUILDER { return 'Valiant::HTML::FormBuilder::Checkbox' }
Subroutine DEFAULT_COLLECTION_CHECKBOX_BUILDER: called CC=1 SCAR=0.0
Pod undocumented
56 3 ▶sub DEFAULT_COLLECTION_RADIO_BUTTON_BUILDER { return 'Valiant::HTML::FormBuilder::RadioButton' }
Subroutine DEFAULT_COLLECTION_RADIO_BUTTON_BUILDER: called CC=1 SCAR=0.0
Pod undocumented
57  
58 0 ▶sub id { return shift->options->{id} }
Subroutine id: not called CC=1 SCAR=6.9
Pod documented
59  
60 sub theme {
61 211 ▶ my ($self) = @_;
Subroutine theme: called CC=2 SCAR=6.9
Pod undocumented
62 211 ▶ my $default_theme = $self->can('default_theme') ? $self->default_theme : +{};
Branch: 1 branch
BranchTrueFalseTotal
$self->can('default_theme') ? : 67 144 211
63 211 return +{ %$default_theme, %{$self->_theme} };
64 }
65  
66 sub nested_child_index {
67 15 ▶ my ($self, $attribute) = @_;
Subroutine nested_child_index: called CC=2 SCAR=6.9
Pod undocumented
68 15 ▶ if(exists($self->_nested_child_index->{$attribute})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->_nested_child_index->{$attribute}) { } 13 2 15
69 13 return ++$self->_nested_child_index->{$attribute};
70 } else {
71 2 return $self->_nested_child_index->{$attribute} = 0
72 }
73 }
74  
75 sub tag_id_for_attribute {
76 169 ▶ my ($self, $attribute, @extra) = @_;
Subroutine tag_id_for_attribute: called CC=3 SCAR=11.0
Pod undocumented
77 169 my $opts = +{};
78 169 ▶ $opts->{namespace} = $self->namespace if $self->has_namespace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_namespace 1 168 169
79 169 ▶ $opts->{index} = $self->index if $self->has_index;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_index 29 140 169
80  
81 169 return $self->tag_helpers->field_id($self->name, $attribute, $opts, @extra);
82 }
83  
84 # $self->tag_name_for_attribute($attribute, +{ multiple=>1 });
85 sub tag_name_for_attribute {
86 115 ▶ my ($self, $attribute, $opts, @extra) = @_;
Subroutine tag_name_for_attribute: called CC=4 SCAR=13.9
Pod undocumented
87 115 ▶ $opts = +{} unless defined $opts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts 113 2 115
88 115 ▶ $opts->{namespace} = $self->namespace if $self->has_namespace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_namespace 1 114 115
89 115 ▶ $opts->{index} = $self->index if $self->has_index;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_index 20 95 115
90  
91 115 return $self->tag_helpers->field_name($self->name, $attribute, $opts, @extra);
92 }
93  
94 sub tag_value_for_attribute {
95 134 ▶ my ($self, $attribute) = @_;
Subroutine tag_value_for_attribute: called CC=1 SCAR=0.0
Pod undocumented
96 134 return $self->tag_helpers->field_value($self->model, $attribute);
97 }
98  
99 sub human_name_for_attribute {
100 32 ▶ my ($self, $attribute) = @_;
Subroutine human_name_for_attribute: called CC=2 SCAR=7.2
Pod undocumented
101 32 ▶ return $self->model->can('human_attribute_name') ?
Branch: 1 branch
BranchTrueFalseTotal
$self->model->can('human_attribute_name') ? : 32 0 32
102 $self->model->human_attribute_name($attribute) :
103 $self->tag_helpers->_humanize($attribute);
104 }
105  
106 sub attribute_has_errors {
107 152 ▶ my ($self, $attribute) = @_;
Subroutine attribute_has_errors: called CC=3 SCAR=11.0
Pod undocumented
108 152 ▶ return $self->model->can('errors') && $self->model->errors->where($attribute) ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$self->model->can('errors') && $self->model->errors->where($attribute) ? : 87 65 152
Condition100%3/3
$self->model->can("errors") and $self->model->errors->where($attribute)
!ll&&!rl&&r
85787
MC/DC100%2/2
$self->model->can("errors") and $self->model->errors->where($attribute)
$self->model->can("errors") $self->model->errors->where($attribute)
Truth table100%3/3
A and B
A= $self->model->can("errors")B= $self->model->errors->where($attribute)
ABresult
0X0
100
111
109 }
110  
111 # $fb->has_errors()
112 # $fb->model_errors($content)
113 # $fb->error_for_form(\&template)
114  
115 sub form_has_errors {
116 0 ▶ my ($self) = shift;
Subroutine form_has_errors: not called CC=7 SCAR=40.3
Pod documented
117 0 ▶ return '' unless $self->model->has_errors # There is at least one field with an error
Branch: 1 branch
BranchTrueFalseTotal
unless $self->model->has_errors or $self->_get_model_errors 0 0 0
Condition0%0/3
$self->model->has_errors or $self->_get_model_errors
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->model->has_errors or $self->_get_model_errors
$self->model->has_errors $self->_get_model_errors
Truth table0%0/3
A or B
A= $self->model->has_errorsB= $self->_get_model_errors
ABresult
1X1
011
000
118 || $self->_get_model_errors; # There is at least one error on the model itself
119  
120 0 ▶ my $options = ref($_[0]) eq 'HASH' ? shift : +{};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
121 0 ▶ my $content = @_ ? shift : _t('invalid_form');
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
122  
123 0 ▶ return $content->($self->view, $self, $self->model) if ref($content) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $content eq "CODE" 0 0 0
124  
125 0 $options = $self->merge_theme_field_opts('model_errors', undef, $options);
126 0 ▶ $content = $self->model->i18n->translate(
Branch: 1 branch
BranchTrueFalseTotal
if $self->model->i18n->is_i18n_tag($content) 0 0 0
127 $content,
128 scope=>'valiant.html.errors.messages',
129 default=>[ _t("errors.messages.invalid_form"), _t("messages.invalid_form") ],
130 ) if $self->model->i18n->is_i18n_tag($content);
131  
132 0 return $self->tag_helpers->tags->div($options, $content);
133 }
134  
135 sub _default_form_has_errors_content {
136 0 ▶ my ($self) = shift;
Subroutine _default_form_has_errors_content: not called CC=1 SCAR=6.9
137 0 my $message = $self->_generate_default_model_error;
138 return sub {
139 0 ▶ my ($view, $self) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 0 return $self->tag_helpers->div($message);
141 }
142 0 }
143  
144 # Public methods for HTML generation
145  
146 # $fb->model_errors()
147 # $fb->model_errors(\%options)
148 # $fb->model_errors(\%options, \&template)
149 # $fb->model_errors(\&template)
150  
151 sub model_errors {
152 7 ▶ my ($self) = shift;
Subroutine model_errors: called CC=12 SCAR=25.0
Pod documented
153 7 my ($options, $content) = (+{}, undef);
154  
155 7 ▶ while(my $arg = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift()) 6 7 13
156 6 ▶ $options = $arg if (ref($arg)||'') eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg || "") eq "HASH" 5 1 6
Condition50%1/2
ref $arg || ''
l!l
60
MC/DC0%0/1
ref $arg || ''
ref $arg
Truth table50%1/2
A
A= ref $arg
Aresult
11
00
157 6 ▶ $content = $arg if (ref($arg)||'') eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg || "") eq "CODE" 1 5 6
Condition50%1/2
ref $arg || ''
l!l
60
MC/DC0%0/1
ref $arg || ''
ref $arg
Truth table50%1/2
A
A= ref $arg
Aresult
11
00
158 }
159 7 $options = $self->merge_theme_field_opts('model_errors', undef, $options);
160  
161 7 my @errors = $self->_get_model_errors;
162 7 my $show_message_on_field_errors = delete $options->{show_message_on_field_errors};
163  
164 7 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if ($self->model->has_errors and $show_message_on_field_errors) 2 5 7
Condition66%2/3
$self->model->has_errors and $show_message_on_field_errors
!ll&&!rl&&r
052
MC/DC50%1/2
$self->model->has_errors and $show_message_on_field_errors
$self->model->has_errors $show_message_on_field_errors
Truth table66%2/3
A and B
A= $self->model->has_errorsB= $show_message_on_field_errors
ABresult
0X0
100
111
165 $self->model->has_errors && # We have errors
166 # !scalar(@errors) && # but no model errorsS_VIEW
167 ($show_message_on_field_errors) # And a default model error
168 ) {
169 2 unshift @errors, $self->_generate_default_model_error($show_message_on_field_errors);
170 }
171 7 ▶ return '' unless @errors;
Branch: 1 branch
BranchTrueFalseTotal
unless @errors 0 7 7
172  
173 7 ▶ my $max_errors = exists($options->{max_errors}) ? delete($options->{max_errors}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'max_errors'} ? : 2 5 7
174 7 ▶ @errors = @errors[0..($max_errors-1)] if($max_errors);
Branch: 1 branch
BranchTrueFalseTotal
if $max_errors 2 5 7
175 7 ▶ $content = $self->_default_model_errors_content($options) unless defined($content);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 6 1 7
176  
177 7 my $error_content = $content->(@errors);
178 7 return $error_content;
179 }
180  
181 sub _get_model_errors {
182 7 ▶ my ($self) = @_;
Subroutine _get_model_errors: called CC=1 SCAR=0.0
183 7 return my @errors = $self->model->errors->model_messages;
184 }
185  
186 sub _generate_default_model_error {
187 2 ▶ my ($self, $tag) = @_;
Subroutine _generate_default_model_error: called CC=4 SCAR=13.9
188 2 ▶ $tag = _t('invalid_form') if $tag eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq 1 1 1 2
189 2 ▶ return $tag unless ref $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tag 1 1 2
190 1 ▶ return $self->DEFAULT_MODEL_ERROR_MSG_ON_FIELD_ERRORS unless $self->model->can('i18n');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->model->can("i18n") 0 1 1
191 1 return $self->model->i18n->translate(
192 $tag,
193 scope=>'valiant.html.errors.messages',
194 default=>[ _t("errors.messages.$tag"), _t("messages.$tag") ],
195 );
196 }
197  
198 sub _default_model_errors_content {
199 6 ▶ my ($self, $options) = @_;
Subroutine _default_model_errors_content: called CC=1 SCAR=0.0
200 return sub {
201 6 ▶ my (@errors) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.0
202 6 ▶ if( scalar(@errors) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @errors == 1) { } 2 4 6
203 2 return $self->tag_helpers->content_tag('div', $errors[0], $options);
204 0 return $self->tag_helpers->content_tag('div', $errors[0], $options);
205 } else {
206 4 ▶ return $self->tag_helpers->content_tag('ol', $options, sub { map { $self->tag_helpers->content_tag('li', $_) } @errors });
Subroutine __ANON__: called CC=1 SCAR=0.0
207 }
208 }
209 6 }
210  
211 sub merge_theme {
212 0 ▶ my ($self, %theme) = @_;
Subroutine merge_theme: not called CC=1 SCAR=6.9
Pod undocumented
213 0 $self->_theme(+{ %{$self->_theme}, %theme });
214 }
215  
216 sub merge_theme_field_opts {
217 211 ▶ my ($self, $tag_type, $attribute, $existing) = @_;
Subroutine merge_theme_field_opts: called CC=4 SCAR=16.8
Pod undocumented
218 211 my $theme = $self->theme;
219  
220 211 ▶ if(exists $theme->{$tag_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $theme->{$tag_type}) 0 211 211
221 0 $existing = +{ %{$theme->{$tag_type}}, %$existing };
222 }
223 211 ▶ if($attribute && exists $theme->{attributes}{$attribute}{$tag_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute and exists $theme->{"attributes"}{$attribute}{$tag_type}) 0 211 211
Condition66%2/3
$attribute and exists $theme->{"attributes"}{$attribute}{$tag_type}
!ll&&!rl&&r
421690
MC/DC0%0/2
$attribute and exists $theme->{"attributes"}{$attribute}{$tag_type}
$attribute exists $theme->{"attributes"}{$attribute}{$tag_type}
Truth table66%2/3
A and B
A= $attributeB= exists $theme->{"attributes"}{$attribute}{$tag_type}
ABresult
0X0
100
111
224 0 $existing = +{ %{$theme->{attributes}{$attribute}{$tag_type}}, %$existing };
225 }
226 211 return $existing;
227 }
228  
229 # $fb->label($attribute)
230 # $fb->label($attribute, \%options)
231 # $fb->label($attribute, $content)
232 # $fb->label($attribute, \%options, $content)
233 # $fb->label($attribute, \&content); sub content { my ($translated_attribute) = @_; ... }
234 # $fb->label($attribute, \%options, \&content); sub content { my ( $translated_attribute) = @_; ... }
235  
236 sub label {
237 32 ▶ my ($self, $attribute) = (shift, shift);
Subroutine label: called CC=10 SCAR=23.0
Pod documented
238 32 my ($options, $content) = (+{}, (my $translated_attribute = $self->human_name_for_attribute($attribute)));
239 32 ▶ while(my $arg = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift()) 43 32 75
240 43 ▶ $options = $arg if (ref($arg)||'') eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg || "") eq "HASH" 21 22 43
Condition100%2/2
ref $arg || ''
l!l
2320
MC/DC100%1/1
ref $arg || ''
ref $arg
Truth table100%2/2
A
A= ref $arg
Aresult
11
00
241 43 ▶ $content = $arg if (ref($arg)||'') eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg || "") eq "CODE" 2 41 43
Condition100%2/2
ref $arg || ''
l!l
2320
MC/DC100%1/1
ref $arg || ''
ref $arg
Truth table100%2/2
A
A= ref $arg
Aresult
11
00
242 43 ▶ $content = $arg if (ref(\$arg)||'') eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if (ref \$arg || "") eq "SCALAR" 20 23 43
Condition50%1/2
ref \$arg || ''
l!l
430
MC/DC0%0/1
ref \$arg || ''
ref \$arg
Truth table50%1/2
A
A= ref \$arg
Aresult
11
00
243 }
244  
245 32 set_unless_defined(for => $options, $self->tag_id_for_attribute($attribute));
246  
247 32 $options = $self->merge_theme_field_opts(label=>$attribute, $options);
248  
249 32 ▶ if((ref($content)||'') eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $content || '') eq 'CODE') { } 2 30 32
Condition100%2/2
ref $content || ''
l!l
230
MC/DC100%1/1
ref $content || ''
ref $content
Truth table100%2/2
A
A= ref $content
Aresult
11
00
250 2 ▶ return $self->tag_helpers->label_tag($attribute, $self->process_options($attribute, $options), sub { $content->($translated_attribute) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
251 } else {
252 30 return $self->tag_helpers->label_tag($attribute, $content, $self->process_options($attribute, $options));
253 }
254 }
255  
256 # $fb->errors_for($attribute)
257 # $fb->errors_for($attribute, \%options)
258 # $fb->errors_for($attribute, \%options, \&template)
259 # $fb->errors_for($attribute, \&template)
260  
261 sub errors_for {
262 17 ▶ my ($self, $attribute) = (shift, shift);
Subroutine errors_for: called CC=11 SCAR=24.1
Pod documented
263 17 my ($options, $content) = (+{}, undef);
264 17 ▶ while(my $arg = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift()) 5 17 22
265 5 ▶ $options = $arg if (ref($arg)||'') eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg || "") eq "HASH" 3 2 5
Condition50%1/2
ref $arg || ''
l!l
50
MC/DC0%0/1
ref $arg || ''
ref $arg
Truth table50%1/2
A
A= ref $arg
Aresult
11
00
266 5 ▶ $content = $arg if (ref($arg)||'') eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg || "") eq "CODE" 2 3 5
Condition50%1/2
ref $arg || ''
l!l
50
MC/DC0%0/1
ref $arg || ''
ref $arg
Truth table50%1/2
A
A= ref $arg
Aresult
11
00
267 }
268 17 $options = $self->merge_theme_field_opts(errors_for=>$attribute, $options);
269  
270 17 ▶ die "Can't display errors on a model that doesn't support the errors method" unless $self->model->can('errors');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->model->can("errors") 0 17 17
271  
272 17 my @errors = $self->model->errors->full_messages_for($attribute);
273 17 ▶ return '' unless @errors;
Branch: 1 branch
BranchTrueFalseTotal
unless @errors 2 15 17
274
275 15 ▶ my $max_errors = exists($options->{max_errors}) ? delete($options->{max_errors}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'max_errors'} ? : 2 13 15
276 15 ▶ @errors = @errors[0..($max_errors-1)] if($max_errors);
Branch: 1 branch
BranchTrueFalseTotal
if $max_errors 2 13 15
277 15 ▶ $content = $self->_default_errors_for_content($self->process_options($attribute, $options)) unless defined($content);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 13 2 15
278  
279 15 return $content->(@errors);
280 }
281  
282 sub _default_errors_for_content {
283 13 ▶ my ($self, $options) = @_;
Subroutine _default_errors_for_content: called CC=1 SCAR=0.0
284 return sub {
285 13 ▶ my (@errors) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
286 13 ▶ if( scalar(@errors) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @errors == 1) { } 11 2 13
287 11 return $self->tag_helpers->content_tag('div', $errors[0], $options);
288 } else {
289 2 ▶ return $self->tag_helpers->content_tag('ol', $options, sub { map { $self->tag_helpers->content_tag('li', $_) } @errors });
Subroutine __ANON__: called CC=1 SCAR=0.0
290 }
291 }
292 13 }
293  
294 sub process_options {
295 152 ▶ my ($self, $attribute, $options) = @_;
Subroutine process_options: called CC=8 SCAR=24.5
Pod undocumented
296  
297 152 ▶ if( ($self->attribute_has_errors($attribute)) && (my $errors_attrs = delete $options->{errors_attrs})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->attribute_has_errors($attribute) and my $errors_attrs = delete $options->{"errors_attrs"}) 1 151 152
Condition100%3/3
$self->attribute_has_errors($attribute) and my $errors_attrs = delete $options->{"errors_attrs"}
!ll&&!rl&&r
65861
MC/DC100%2/2
$self->attribute_has_errors($attribute) and my $errors_attrs = delete $options->{"errors_attrs"}
$self->attribute_has_errors($attribute) my $errors_attrs = delete $options->{"errors_attrs"}
Truth table100%3/3
A and B
A= $self->attribute_has_errors($attribute)B= my $errors_attrs = delete $options->{"errors_attrs"}
ABresult
0X0
100
111
298 1 foreach my $key(keys %$errors_attrs) {
299 1 ▶ if(exists $options->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{$key}) { } 1 0 1
300 1 ▶ if( ($key eq 'data') || ($key eq 'aria') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'data' or $key eq 'aria') { } 0 1 1
elsif ($key eq 'class') { } 1 0 1
Condition33%1/3
$key eq "data" or $key eq "aria"
l!l&&r!l&&!r
001
MC/DC0%0/2
$key eq "data" or $key eq "aria"
$key eq "data" $key eq "aria"
Truth table33%1/3
A or B
A= $key eq "data"B= $key eq "aria"
ABresult
1X1
011
000
301 0 $options->{$key} = +{ %{$options->{$key}}, %{$errors_attrs->{$key}} };
302 } elsif($key eq 'class') {
303 1 $options->{$key} .= " $errors_attrs->{$key}";
304 } else {
305 0 $options->{$key} .= $errors_attrs->{$key};
306 }
307 } else {
308 0 $options->{$key} = $errors_attrs->{$key};
309 }
310 }
311 }
312 152 return $options;
313 }
314  
315 # $fb->input($attribute, \%options)
316 # $fb->input($attribute)
317  
318 sub input {
319 70 ▶ my ($self, $attribute, $options) = (shift, shift, (@_ ? shift : +{}));
Subroutine input: called CC=9 SCAR=22.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 42 28 70
320 70 ▶ $options = $self->merge_theme_field_opts($options->{type} || 'input', $attribute, $options);
Condition100%2/2
$options->{'type'} || 'input'
l!l
3832
MC/DC100%1/1
$options->{'type'} || 'input'
$options->{'type'}
Truth table100%2/2
A
A= $options->{'type'}
Aresult
11
00
321 70 ▶ my $errors_classes = exists($options->{errors_classes}) ? delete($options->{errors_classes}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'errors_classes'} ? : 8 62 70
322 70 ▶ my $model = $self->model->can('to_model') ? $self->model->to_model : $self->model;
Branch: 1 branch
BranchTrueFalseTotal
$self->model->can('to_model') ? : 0 70 70
323
324 70 ▶ $options->{class} = join(' ', (grep { defined $_ } $options->{class}, $errors_classes))
Branch: 1 branch
BranchTrueFalseTotal
if $errors_classes and $model->can("errors") and $model->errors->where($attribute) 8 62 70
Condition66%4/6
$errors_classes and $model->can("errors") and $model->errors->where($attribute)
!ll&&!rl&&r
6208
$errors_classes and $model->can("errors")
!ll&&!rl&&r
6208
MC/DC33%1/3
$errors_classes and $model->can("errors") and $model->errors->where($attribute)
$errors_classes $model->can("errors") $model->errors->where($attribute)
Truth table50%2/4
A and B and C
A= $errors_classesB= $model->can("errors")C= $model->errors->where($attribute)
ABCresult
0XX0
10X0
1100
1111
325 if $errors_classes && $model->can('errors') && $model->errors->where($attribute);
326  
327 70 set_unless_defined(type => $options, 'text');
328 70 set_unless_defined(id => $options, $self->tag_id_for_attribute($attribute));
329 70 set_unless_defined(name => $options, $self->tag_name_for_attribute($attribute));
330 70 ▶ $options->{value} = $self->tag_value_for_attribute($attribute) unless defined($options->{value});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"value"} 36 34 70
331  
332 70 $options = $self->process_options($attribute, $options);
333  
334 70 return $self->tag_helpers->input_tag($attribute, $options);
335 }
336  
337 sub password {
338 4 ▶ my ($self, $attribute, $options) = (shift, shift, (@_ ? shift : +{}));
Subroutine password: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 3 1 4
339 4 $options->{type} = 'password';
340 4 ▶ $options->{value} = '' unless exists($options->{value});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"value"} 4 0 4
341 4 return $self->input($attribute, $options);
342 }
343  
344 sub hidden {
345 11 ▶ my ($self, $attribute, $options) = (shift, shift, (@_ ? shift : +{}));
Subroutine hidden: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 10 1 11
346 11 $options->{type} = 'hidden';
347 11 return $self->input($attribute, $options);
348 }
349  
350 sub text_area {
351 3 ▶ my ($self, $attribute, $options) = (shift, shift, (@_ ? shift : +{}));
Subroutine text_area: called CC=6 SCAR=18.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 1 3
352 3 $options = $self->merge_theme_field_opts('text_area', $attribute, $options);
353 3 ▶ my $errors_classes = exists($options->{errors_classes}) ? delete($options->{errors_classes}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'errors_classes'} ? : 1 2 3
354
355 3 ▶ $options->{class} = join(' ', (grep { defined $_ } $options->{class}, $errors_classes))
Branch: 1 branch
BranchTrueFalseTotal
if $errors_classes and $self->model->can("errors") and $self->model->errors->where($attribute) 1 2 3
Condition66%4/6
$errors_classes and $self->model->can("errors") and $self->model->errors->where($attribute)
!ll&&!rl&&r
201
$errors_classes and $self->model->can("errors")
!ll&&!rl&&r
201
MC/DC33%1/3
$errors_classes and $self->model->can("errors") and $self->model->errors->where($attribute)
$errors_classes $self->model->can("errors") $self->model->errors->where($attribute)
Truth table50%2/4
A and B and C
A= $errors_classesB= $self->model->can("errors")C= $self->model->errors->where($attribute)
ABCresult
0XX0
10X0
1100
1111
356 if $errors_classes && $self->model->can('errors') && $self->model->errors->where($attribute);
357  
358 3 set_unless_defined(id => $options, $self->tag_id_for_attribute($attribute));
359 3 return $self->tag_helpers->text_area_tag(
360 $self->tag_name_for_attribute($attribute),
361 $self->tag_value_for_attribute($attribute),
362 $self->process_options($attribute, $options),
363 );
364 }
365  
366 sub checkbox {
367 16 ▶ my ($self, $attribute) = (shift, shift);
Subroutine checkbox: called CC=15 SCAR=27.1
Pod documented
368 16 ▶ my $options = (ref($_[0])||'') eq 'HASH' ? shift(@_) : +{};
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[0] || '') eq 'HASH' ? : 14 2 16
Condition100%2/2
ref $_[0] || ''
l!l
142
MC/DC100%1/1
ref $_[0] || ''
ref $_[0]
Truth table100%2/2
A
A= ref $_[0]
Aresult
11
00
369 16 $options = $self->merge_theme_field_opts('checkbox', $attribute, $options);
370  
371 16 ▶ my $checked_value = @_ ? shift : 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 10 6 16
372 16 ▶ my $unchecked_value = @_ ? shift : 0;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 15 16
373 16 ▶ my $errors_classes = exists($options->{errors_classes}) ? delete($options->{errors_classes}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'errors_classes'} ? : 1 15 16
374 16 ▶ my $show_hidden_unchecked = exists($options->{include_hidden}) ? delete($options->{include_hidden}) : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'include_hidden'} ? : 13 3 16
375 16 my $name = $self->tag_name_for_attribute($attribute);
376  
377 16 my $checked = 0;
378 16 ▶ if(exists($options->{checked})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{'checked'}) { } 10 6 16
379 10 $checked = delete $options->{checked};
380 } else {
381 6 ▶ $checked = $self->tag_value_for_attribute($attribute) ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$self->tag_value_for_attribute($attribute) ? : 1 5 6
382 }
383  
384 16 $options->{type} = 'checkbox';
385 16 ▶ $options->{value} = $checked_value unless exists($options->{value});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"value"} 16 0 16
386 16 ▶ $options->{class} = join(' ', (grep { defined $_ } $options->{class}, $errors_classes))
Branch: 1 branch
BranchTrueFalseTotal
if $errors_classes and $self->model->can("errors") and $self->model->errors->where($attribute) 1 15 16
Condition66%4/6
$errors_classes and $self->model->can("errors") and $self->model->errors->where($attribute)
!ll&&!rl&&r
1501
$errors_classes and $self->model->can("errors")
!ll&&!rl&&r
1501
MC/DC33%1/3
$errors_classes and $self->model->can("errors") and $self->model->errors->where($attribute)
$errors_classes $self->model->can("errors") $self->model->errors->where($attribute)
Truth table50%2/4
A and B and C
A= $errors_classesB= $self->model->can("errors")C= $self->model->errors->where($attribute)
ABCresult
0XX0
10X0
1100
1111
387 if $errors_classes && $self->model->can('errors') && $self->model->errors->where($attribute);
388  
389 16 set_unless_defined(id => $options, $self->tag_id_for_attribute($attribute));
390  
391 16 my $checkbox = $self->tag_helpers->checkbox_tag(
392 $name,
393 $checked_value,
394 $checked,
395 $self->process_options($attribute, $options),
396 );
397  
398 16 ▶ if($show_hidden_unchecked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($show_hidden_unchecked) 3 13 16
399 3 ▶ my $hidden_name = exists($options->{name}) ? $options->{name} : $name;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'name'} ? : 0 3 3
400 3 my $hidden_checkbox = $self->tag_helpers->tag('input', +{type=>'hidden', name=>$hidden_name, value=>$unchecked_value});
401 3 $checkbox = $self->tag_helpers->join_tags($hidden_checkbox, $checkbox);
402 }
403  
404 16 return $checkbox;
405 }
406  
407 #radio_button(object_name, method, tag_value, options = {})
408 sub radio_button {
409 14 ▶ my ($self, $attribute) = (shift, shift);
Subroutine radio_button: called CC=8 SCAR=20.8
Pod documented
410 14 ▶ my $options = (ref($_[-1])||'') eq 'HASH' ? pop(@_) : +{};
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[-1] || '') eq 'HASH' ? : 11 3 14
Condition100%2/2
ref $_[-1] || ''
l!l
113
MC/DC100%1/1
ref $_[-1] || ''
ref $_[-1]
Truth table100%2/2
A
A= ref $_[-1]
Aresult
11
00
411 14 $options = $self->merge_theme_field_opts('radio_button', $attribute, $options);
412  
413 14 ▶ my $value = @_ ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 14 0 14
414  
415 14 $options->{type} = 'radio';
416 14 ▶ $options->{value} = $value unless exists($options->{value});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"value"} 14 0 14
417 14 ▶ $options->{checked} = do { $self->tag_value_for_attribute($attribute) eq $value ? 1:0 } unless exists($options->{checked});
Branch: 2 branches
BranchTrueFalseTotal
unless exists $options->{"checked"} 4 10 14
$self->tag_value_for_attribute($attribute) eq $value ? : 1 3 4
418 14 ▶ $options->{id} = $self->tag_id_for_attribute($attribute, $value) unless exists($options->{id});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"id"} 5 9 14
419  
420 14 return $self->input($attribute, $self->process_options($attribute, $options));
421 }
422
423 sub date_field {
424 6 ▶ my ($self, $attribute, $options) = (@_, +{});
Subroutine date_field: called CC=7 SCAR=20.2
Pod documented
425 6 my $value = $self->tag_value_for_attribute($attribute);
426 6 $options = $self->merge_theme_field_opts('date_field', $attribute, $options);
427 6 $options->{type} = 'date';
428 6 ▶ $options->{value} ||= Scalar::Util::blessed($value) ? $value->ymd : $value;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($value) ? : 6 0 6
Condition33%1/3
$options->{"value"} ||= builtin::blessed($value) ? $value->ymd : $value
l!l&&r!l&&!r
060
MC/DC0%0/2
$options->{"value"} ||= builtin::blessed($value) ? $value->ymd : $value
$options->{"value"} builtin::blessed($value) ? $value->ymd : $value
Truth table33%1/3
A ||= B
A= $options->{"value"}B= builtin::blessed($value) ? $value->ymd : $value
ABresult
1X1
011
000
429 6 ▶ $options->{min} = $options->{min}->ymd if exists($options->{min}) && Scalar::Util::blessed($options->{min});
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"min"} and builtin::blessed($options->{"min"}) 1 5 6
Condition66%2/3
exists $options->{"min"} and builtin::blessed($options->{"min"})
!ll&&!rl&&r
501
MC/DC50%1/2
exists $options->{"min"} and builtin::blessed($options->{"min"})
exists $options->{"min"} builtin::blessed($options->{"min"})
Truth table66%2/3
A and B
A= exists $options->{"min"}B= builtin::blessed($options->{"min"})
ABresult
0X0
100
111
430 6 ▶ $options->{max} = $options->{max}->ymd if exists($options->{max}) && Scalar::Util::blessed($options->{max});
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"max"} and builtin::blessed($options->{"max"}) 1 5 6
Condition66%2/3
exists $options->{"max"} and builtin::blessed($options->{"max"})
!ll&&!rl&&r
501
MC/DC50%1/2
exists $options->{"max"} and builtin::blessed($options->{"max"})
exists $options->{"max"} builtin::blessed($options->{"max"})
Truth table66%2/3
A and B
A= exists $options->{"max"}B= builtin::blessed($options->{"max"})
ABresult
0X0
100
111
431  
432 6 return $self->input($attribute, $options);
433 }
434  
435 sub datetime_local_field {
436 1 ▶ my ($self, $attribute, $options) = (@_, +{});
Subroutine datetime_local_field: called CC=7 SCAR=23.0
Pod documented
437 1 my $value = $self->tag_value_for_attribute($attribute);
438 1 $options = $self->merge_theme_field_opts('datetime_local_field', $attribute, $options);
439  
440 1 $options->{type} = 'datetime-local';
441 1 ▶ $options->{value} ||= Scalar::Util::blessed($value) ? $value->strftime('%Y-%m-%dT%T') : $value;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($value) ? : 1 0 1
Condition33%1/3
$options->{"value"} ||= builtin::blessed($value) ? $value->strftime("%Y-%m-%dT%T") : $value
l!l&&r!l&&!r
010
MC/DC0%0/2
$options->{"value"} ||= builtin::blessed($value) ? $value->strftime("%Y-%m-%dT%T") : $value
$options->{"value"} builtin::blessed($value) ? $value->strftime("%Y-%m-%dT%T") : $value
Truth table33%1/3
A ||= B
A= $options->{"value"}B= builtin::blessed($value) ? $value->strftime("%Y-%m-%dT%T") : $value
ABresult
1X1
011
000
442 1 ▶ $options->{min} = $options->{min}->strftime('%Y-%m-%dT%T') if exists($options->{min}) && Scalar::Util::blessed($options->{min});
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"min"} and builtin::blessed($options->{"min"}) 0 1 1
Condition33%1/3
exists $options->{"min"} and builtin::blessed($options->{"min"})
!ll&&!rl&&r
100
MC/DC0%0/2
exists $options->{"min"} and builtin::blessed($options->{"min"})
exists $options->{"min"} builtin::blessed($options->{"min"})
Truth table33%1/3
A and B
A= exists $options->{"min"}B= builtin::blessed($options->{"min"})
ABresult
0X0
100
111
443 1 ▶ $options->{max} = $options->{max}->strftime('%Y-%m-%dT%T') if exists($options->{max}) && Scalar::Util::blessed($options->{max});
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"max"} and builtin::blessed($options->{"max"}) 0 1 1
Condition33%1/3
exists $options->{"max"} and builtin::blessed($options->{"max"})
!ll&&!rl&&r
100
MC/DC0%0/2
exists $options->{"max"} and builtin::blessed($options->{"max"})
exists $options->{"max"} builtin::blessed($options->{"max"})
Truth table33%1/3
A and B
A= exists $options->{"max"}B= builtin::blessed($options->{"max"})
ABresult
0X0
100
111
444  
445 1 return $self->input($attribute, $options);
446 }
447  
448 sub time_field {
449 2 ▶ my ($self, $attribute, $options) = (@_, +{});
Subroutine time_field: called CC=5 SCAR=16.7
Pod documented
450 2 my $value = $self->tag_value_for_attribute($attribute);
451 2 $options = $self->merge_theme_field_opts('time_field', $attribute, $options);
452 2 ▶ my $format = (exists($options->{include_seconds}) && !delete($options->{include_seconds})) ? '%H:%M' : '%T.%3N';
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'include_seconds'} && !delete $options->{'include_seconds'} ? : 1 1 2
Condition66%2/3
exists $options->{"include_seconds"} and not delete $options->{"include_seconds"}
!ll&&!rl&&r
101
MC/DC50%1/2
exists $options->{"include_seconds"} and not delete $options->{"include_seconds"}
exists $options->{"include_seconds"} not delete $options->{"include_seconds"}
Truth table66%2/3
A and not(B)
A= exists $options->{"include_seconds"}B= not delete $options->{"include_seconds"}
ABresult
0X0
100
111
453  
454 2 $options->{type} = 'time';
455 2 ▶ $options->{value} ||= Scalar::Util::blessed($value) ? $value->strftime($format) : $value;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($value) ? : 2 0 2
Condition33%1/3
$options->{"value"} ||= builtin::blessed($value) ? $value->strftime($format) : $value
l!l&&r!l&&!r
020
MC/DC0%0/2
$options->{"value"} ||= builtin::blessed($value) ? $value->strftime($format) : $value
$options->{"value"} builtin::blessed($value) ? $value->strftime($format) : $value
Truth table33%1/3
A ||= B
A= $options->{"value"}B= builtin::blessed($value) ? $value->strftime($format) : $value
ABresult
1X1
011
000
456  
457 2 return $self->input($attribute, $options);
458 }
459  
460 sub submit {
461 2 ▶ my ($self) = shift;
Subroutine submit: called CC=4 SCAR=13.9
Pod documented
462 2 ▶ my $options = (ref($_[-1])||'') eq 'HASH' ? pop(@_) : +{};
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[-1] || '') eq 'HASH' ? : 1 1 2
Condition100%2/2
ref $_[-1] || ''
l!l
11
MC/DC100%1/1
ref $_[-1] || ''
ref $_[-1]
Truth table100%2/2
A
A= ref $_[-1]
Aresult
11
00
463 2 ▶ my $value = @_ ? shift(@_) : $self->_submit_default_value;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 1 2
464 2 $options = $self->merge_theme_field_opts('submit', undef, $options);
465  
466 2 return $self->tag_helpers->submit_tag($value, $options);
467 }
468  
469 sub _submit_default_value {
470 1 ▶ my $self = shift;
Subroutine _submit_default_value: called CC=5 SCAR=17.0
471 1 ▶ my $model = $self->model->can('to_model') ? $self->model->to_model : $self->model;
Branch: 1 branch
BranchTrueFalseTotal
$self->model->can('to_model') ? : 0 1 1
472 1 ▶ my $key = $model->can('in_storage') ? ( $model->in_storage ? 'update':'create' ) : 'submit';
Branch: 2 branches
BranchTrueFalseTotal
$model->can('in_storage') ? : 0 1 1
$model->in_storage ? : 0 0 0
473 1 ▶ my $model_placeholder = $model->can('model_name') ? $model->model_name->human : $self->tag_helpers->_humanize($self->name);
Branch: 1 branch
BranchTrueFalseTotal
$model->can('model_name') ? : 1 0 1
474  
475 1 my @defaults = ();
476  
477 1 push @defaults, _t "formbuilder.submit.@{[ $self->name ]}.${key}";
478 1 push @defaults, _t "formbuilder.submit.${key}";
479 1 push @defaults, "@{[ $self->tag_helpers->_humanize($key) ]} ${model_placeholder}";
480  
481 1 return $self->model->i18n->translate(
482 shift(@defaults),
483 model=>$model_placeholder,
484 default=>\@defaults,
485 );
486 }
487  
488 # ->button($name, \%attrs, \&block)
489 # ->button($name, \%attrs, $content)
490 # ->button($name, \&block)
491 # ->button($name, $content)
492  
493 sub button {
494 5 ▶ my $self = shift;
Subroutine button: called CC=8 SCAR=20.9
Pod documented
495 5 my $attribute = shift;
496 5 ▶ my $attrs = (ref($_[0])||'') eq 'HASH' ? shift(@_) : +{};
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[0] || '') eq 'HASH' ? : 2 3 5
Condition100%2/2
ref $_[0] || ''
l!l
32
MC/DC100%1/1
ref $_[0] || ''
ref $_[0]
Truth table100%2/2
A
A= ref $_[0]
Aresult
11
00
497 5 my $content = shift;
498  
499 5 ▶ $attrs->{type} = 'submit' unless exists($attrs->{type});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attrs->{"type"} 5 0 5
500 5 ▶ $attrs->{value} = $self->tag_value_for_attribute($attribute) unless exists($attrs->{value});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attrs->{"value"} 4 1 5
501 5 ▶ $attrs->{name} = $self->tag_name_for_attribute($attribute) unless exists($attrs->{name});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attrs->{"name"} 5 0 5
502 5 ▶ $attrs->{id} = $self->tag_id_for_attribute($attribute) unless exists($attrs->{id});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attrs->{"id"} 5 0 5
503 5 $attrs = $self->merge_theme_field_opts('button', $attribute, $attrs);
504  
505 5 ▶ return ref($content) ?
Branch: 1 branch
BranchTrueFalseTotal
ref $content ? : 1 4 5
506 $self->tag_helpers->button_tag($attrs, $content) :
507 $self->tag_helpers->button_tag($content, $self->process_options($attribute, $attrs));
508 }
509  
510 sub legend {
511 6 ▶ my ($self) = shift;
Subroutine legend: called CC=8 SCAR=20.8
Pod documented
512 6 my $value = my $default_value = $self->_legend_default_value;
513 6 my $options = +{};
514  
515 6 ▶ $value = pop(@_) if (ref($_[-1])||'') eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[-1] || "") eq "CODE" 2 4 6
Condition100%2/2
ref $_[-1] || ''
l!l
42
MC/DC100%1/1
ref $_[-1] || ''
ref $_[-1]
Truth table100%2/2
A
A= ref $_[-1]
Aresult
11
00
516 6 ▶ $options = pop(@_) if (ref($_[-1])||'') eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[-1] || "") eq "HASH" 3 3 6
Condition100%2/2
ref $_[-1] || ''
l!l
33
MC/DC100%1/1
ref $_[-1] || ''
ref $_[-1]
Truth table100%2/2
A
A= ref $_[-1]
Aresult
11
00
517 6 ▶ $value = shift(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 4 6
518 6 ▶ $value = $value->($default_value) if ((ref($value)||'') eq 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value || "") eq "CODE" 2 4 6
Condition100%2/2
ref $value || ''
l!l
24
MC/DC100%1/1
ref $value || ''
ref $value
Truth table100%2/2
A
A= ref $value
Aresult
11
00
519  
520 6 return $self->tag_helpers->legend_tag($value, $options);
521 }
522  
523 sub legend_for {
524 0 ▶ my $self = shift;
Subroutine legend_for: not called CC=5 SCAR=34.0
Pod documented
525 0 my $attribute = shift;
526 0 ▶ my $attrs = (ref($_[0])||'') eq 'HASH' ? shift(@_) : +{};
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[0] || '') eq 'HASH' ? : 0 0 0
Condition0%0/2
ref $_[0] || ''
l!l
00
MC/DC0%0/1
ref $_[0] || ''
ref $_[0]
Truth table0%0/2
A
A= ref $_[0]
Aresult
11
00
527 0 ▶ my $content = @_ ? shift(@_) : $self->human_name_for_attribute($attribute);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
528  
529 0 ▶ $attrs->{id} = "@{[ $self->tag_id_for_attribute($attribute) ]}_legend" unless exists($attrs->{id});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attrs->{"id"} 0 0 0
530 0 $attrs = $self->merge_theme_field_opts('legend_for', $attribute, $attrs);
531  
532 0 return $self->tag_helpers->legend_tag($attrs, $content);
533 }
534  
535 sub _legend_default_value {
536 6 ▶ my $self = shift;
Subroutine _legend_default_value: called CC=6 SCAR=19.5
537 6 ▶ my $model = $self->model->can('to_model') ? $self->model->to_model : $self->model;
Branch: 1 branch
BranchTrueFalseTotal
$self->model->can('to_model') ? : 0 6 6
538 6 ▶ my $key = $model->can('in_storage') ? ( $model->in_storage ? 'update':'create' ) : 'new';
Branch: 2 branches
BranchTrueFalseTotal
$model->can('in_storage') ? : 0 6 6
$model->in_storage ? : 0 0 0
539 6 ▶ my $model_placeholder = $model->can('model_name') ? $model->model_name->human : $self->tag_helpers->_humanize($self->name);
Branch: 1 branch
BranchTrueFalseTotal
$model->can('model_name') ? : 6 0 6
540  
541 6 my @defaults = ();
542  
543 6 ▶ return "@{[ $self->tag_helpers->_humanize($key) ]} ${model_placeholder}" unless $self->model->can('i18n');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->model->can("i18n") 0 6 6
544  
545 6 push @defaults, _t "formbuilder.legend.@{[ $self->model_name->i18n_key ]}.${key}";
546 6 push @defaults, _t "formbuilder.legend.${key}";
547 6 push @defaults, "@{[ $self->tag_helpers->_humanize($key) ]} ${model_placeholder}";
548  
549 6 return $self->model->i18n->translate(
550 shift(@defaults),
551 model=>$model_placeholder,
552 default=>\@defaults,
553 );
554 }
555  
556 # fields_for($related_attribute, ?\%options?, \&block)
557 sub fields_for {
558 2 ▶ my ($self, $related_attribute) = (shift, shift);
Subroutine fields_for: called CC=29 SCAR=40.3
Pod documented
559 2 ▶ my $options = (ref($_[0])||'') eq 'HASH' ? shift(@_) : +{};
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[0] || '') eq 'HASH' ? : 0 2 2
Condition50%1/2
ref $_[0] || ''
l!l
20
MC/DC0%0/1
ref $_[0] || ''
ref $_[0]
Truth table50%1/2
A
A= ref $_[0]
Aresult
11
00
560 2 ▶ my $codeblock = (ref($_[0])||'') eq 'CODE' ? shift(@_) : die "Missing required code block";
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[0] || '') eq 'CODE' ? : 2 0 2
Condition50%1/2
ref $_[0] || ''
l!l
20
MC/DC0%0/1
ref $_[0] || ''
ref $_[0]
Truth table50%1/2
A
A= ref $_[0]
Aresult
11
00
561 2 ▶ my $finally_block = (ref($_[0])||'') eq 'CODE' ? shift(@_) : undef;
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[0] || '') eq 'CODE' ? : 1 1 2
Condition100%2/2
ref $_[0] || ''
l!l
11
MC/DC100%1/1
ref $_[0] || ''
ref $_[0]
Truth table100%2/2
A
A= ref $_[0]
Aresult
11
00
562  
563 2 ▶ $options->{builder} = $self->options->{builder} if !exists($options->{builder}) && !defined($options->{builder}) && defined($self->options->{builder});
Branch: 1 branch
BranchTrueFalseTotal
if not exists $options->{"builder"} || defined $options->{"builder"} and defined $self->options->{"builder"} 0 2 2
Condition33%2/6
not exists $options->{"builder"} || defined $options->{"builder"} and defined $self->options->{"builder"}
!ll&&!rl&&r
020
exists $options->{'builder'} || defined $options->{'builder'}
l!l&&r!l&&!r
002
MC/DC0%0/3
not exists $options->{"builder"} || defined $options->{"builder"} and defined $self->options->{"builder"}
exists $options->{'builder'} defined $options->{'builder'} defined $self->options->{"builder"}
Truth table25%1/4
not(A || B) and C
A= exists $options->{'builder'}B= defined $options->{'builder'}C= defined $self->options->{"builder"}
ABCresult
1XX0
01X0
0000
0011
564 2 ▶ $options->{include_id} = $self->options->{include_id} if !exists($options->{include_id}) && !defined($options->{include_id}) && defined($self->options->{include_id});
Branch: 1 branch
BranchTrueFalseTotal
if not exists $options->{"include_id"} || defined $options->{"include_id"} and defined $self->options->{"include_id"} 0 2 2
Condition33%2/6
not exists $options->{"include_id"} || defined $options->{"include_id"} and defined $self->options->{"include_id"}
!ll&&!rl&&r
020
exists $options->{'include_id'} || defined $options->{'include_id'}
l!l&&r!l&&!r
002
MC/DC0%0/3
not exists $options->{"include_id"} || defined $options->{"include_id"} and defined $self->options->{"include_id"}
exists $options->{'include_id'} defined $options->{'include_id'} defined $self->options->{"include_id"}
Truth table25%1/4
not(A || B) and C
A= exists $options->{'include_id'}B= defined $options->{'include_id'}C= defined $self->options->{"include_id"}
ABCresult
1XX0
01X0
0000
0011
565 2 ▶ $options->{namespace} = $self->namespace if $self->has_namespace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_namespace 0 2 2
566 2 $options->{parent_builder} = $self;
567  
568 2 my $related_record = $self->tag_value_for_attribute($related_attribute);
569 2 my $name = "@{[ $self->name ]}.@{[ $related_attribute ]}";
570  
571 2 ▶ $related_record = $related_record->to_model if Scalar::Util::blessed($related_record) && $related_record->can('to_model');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($related_record) and $related_record->can("to_model") 0 2 2
Condition66%2/3
builtin::blessed($related_record) and $related_record->can("to_model")
!ll&&!rl&&r
110
MC/DC0%0/2
builtin::blessed($related_record) and $related_record->can("to_model")
builtin::blessed($related_record) $related_record->can("to_model")
Truth table66%2/3
A and B
A= builtin::blessed($related_record)B= $related_record->can("to_model")
ABresult
0X0
100
111
572  
573 # Coerce an array into a collection. Not sure if we want this here or not TBH...
574 2 ▶ $related_record = $self->tag_helpers->array_to_collection(map { $_->can('to_model') ? $_->to_model : $_ } @$related_record)
Branch: 2 branches
BranchTrueFalseTotal
if (ref $related_record || "") eq "ARRAY" 1 1 2
$_->can('to_model') ? : 0 3 3
Condition50%1/2
ref $related_record || ''
l!l
20
MC/DC0%0/1
ref $related_record || ''
ref $related_record
Truth table50%1/2
A
A= ref $related_record
Aresult
11
00
575 if (ref($related_record)||'') eq 'ARRAY';
576  
577 # Ok is the related record a collection or something else.
578 2 ▶ if($related_record->can('next')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($related_record->can('next')) { } 1 1 2
579 1 my @output = ();
580 1 ▶ my $explicit_child_index = exists($options->{child_index}) ? $options->{child_index} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'child_index'} ? : 0 1 1
581  
582 1 ▶ while(my $child_model = $related_record->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $child_model = $related_record->next) 3 1 4
583 3 ▶ if(defined($explicit_child_index)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $explicit_child_index) { } 0 3 3
584 0 ▶ $options->{child_index} = $options->{child_index}->($child_model) if ref() eq 'CODE'; # allow for callback version of this
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ eq "CODE" 0 0 0
585 } else {
586 3 $options->{child_index} = $self->nested_child_index($related_attribute);
587 }
588 3 my $nested = $self->fields_for_nested_model("${name}[@{[ $options->{child_index} ]}]", $child_model, $options, $codeblock);
589 3 push @output, $nested;
590 }
591 1 ▶ $related_record->reset if $related_record->can('reset');
Branch: 1 branch
BranchTrueFalseTotal
if $related_record->can("reset") 1 0 1
592 1 ▶ if($finally_block) {
Branch: 1 branch
BranchTrueFalseTotal
if ($finally_block) 1 0 1
593 1 ▶ my $finally_model = $related_record->can('build') ? $related_record->build : die "Can't have a finally block if the collection doesn't support 'build'";
Branch: 1 branch
BranchTrueFalseTotal
$related_record->can('build') ? : 1 0 1
594 1 ▶ if(defined($explicit_child_index)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $explicit_child_index) { } 0 1 1
595 0 ▶ $options->{child_index} = $options->{child_index}->($finally_model) if ref() eq 'CODE'; # allow for callback version of this
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ eq "CODE" 0 0 0
596 } else {
597 1 $options->{child_index} = $self->nested_child_index($related_attribute);
598 }
599 1 my $finally_content = $self->fields_for_nested_model("${name}[@{[ $options->{child_index} ]}]", $finally_model, $options, $finally_block);
600 1 push @output, $finally_content;
601 }
602 1 return $self->tag_helpers->join_tags(@output);
603 } else {
604 1 return $self->fields_for_nested_model($name, $related_record, $options, $codeblock);
605 }
606 }
607  
608 sub fields_for_nested_model {
609 5 ▶ my ($self, $name, $model, $options, $codeblock) = @_;
Subroutine fields_for_nested_model: called CC=5 SCAR=20.2
Pod undocumented
610 5 my $emit_hidden_id = 0;
611 5 ▶ $model = $model->to_model if $model->can('to_model');
Branch: 1 branch
BranchTrueFalseTotal
if $model->can("to_model") 0 5 5
612  
613 5 ▶ if($model->can('in_storage') && $model->in_storage) {
Branch: 1 branch
BranchTrueFalseTotal
if ($model->can("in_storage") and $model->in_storage) 0 5 5
Condition33%1/3
$model->can("in_storage") and $model->in_storage
!ll&&!rl&&r
500
MC/DC0%0/2
$model->can("in_storage") and $model->in_storage
$model->can("in_storage") $model->in_storage
Truth table33%1/3
A and B
A= $model->can("in_storage")B= $model->in_storage
ABresult
0X0
100
111
614 0 ▶ $emit_hidden_id = exists($options->{include_id}) ? $options->{include_id} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'include_id'} ? : 0 0 0
615 }
616  
617 return $self->tag_helpers->fields_for($name, $model, $options, sub {
618 5 ▶ my $view = shift;
Subroutine __ANON__: called CC=5 SCAR=20.2
619 5 my $fb = shift;
620 5 my @output = $codeblock->($view, $fb, $model);
621 5 ▶ if(@output && $emit_hidden_id && $model->can('primary_columns')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@output and $emit_hidden_id and $model->can("primary_columns")) 0 5 5
Condition33%2/6
@output and $emit_hidden_id and $model->can("primary_columns")
!ll&&!rl&&r
500
@output and $emit_hidden_id
!ll&&!rl&&r
050
MC/DC0%0/3
@output and $emit_hidden_id and $model->can("primary_columns")
@output $emit_hidden_id $model->can("primary_columns")
Truth table25%1/4
A and B and C
A= @outputB= $emit_hidden_idC= $model->can("primary_columns")
ABCresult
0XX0
10X0
1100
1111
622 0 foreach my $id_field ($model->primary_columns) {
623 0 push @output, $fb->hidden($id_field); #TODO this cant be right...
624 }
625 }
626 5 return $self->tag_helpers->join_tags(@output);
627 5 });
628 }
629  
630 sub select {
631 8 ▶ my ($self, $attribute_proto) = (shift, shift);
Subroutine select: called CC=34 SCAR=36.3
Pod documented
632 8 ▶ my $block = (ref($_[-1])||'') eq 'CODE' ? pop(@_) : undef;
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[-1] || '') eq 'CODE' ? : 2 6 8
Condition50%1/2
ref $_[-1] || ''
l!l
80
MC/DC0%0/1
ref $_[-1] || ''
ref $_[-1]
Truth table50%1/2
A
A= ref $_[-1]
Aresult
11
00
633 8 ▶ my $options = (ref($_[-1])||'') eq 'HASH' ? pop(@_) : +{};
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[-1] || '') eq 'HASH' ? : 6 2 8
Condition100%2/2
ref $_[-1] || ''
l!l
71
MC/DC100%1/1
ref $_[-1] || ''
ref $_[-1]
Truth table100%2/2
A
A= ref $_[-1]
Aresult
11
00
634 8 $options = $self->merge_theme_field_opts('select', undef, $options);
635  
636 8 ▶ my ($attribute) = (ref($attribute_proto)||'') eq 'HASH' ? %$attribute_proto : $attribute_proto;
Branch: 1 branch
BranchTrueFalseTotal
(ref $attribute_proto || '') eq 'HASH' ? : 1 7 8
Condition100%2/2
ref $attribute_proto || ''
l!l
17
MC/DC100%1/1
ref $attribute_proto || ''
ref $attribute_proto
Truth table100%2/2
A
A= ref $attribute_proto
Aresult
11
00
637 8 ▶ my $errors_classes = exists($options->{errors_classes}) ? delete($options->{errors_classes}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'errors_classes'} ? : 0 8 8
638 8 ▶ $options->{class} = join(' ', (grep { defined $_ } $options->{class}, $errors_classes))
Branch: 1 branch
BranchTrueFalseTotal
if $errors_classes and $self->model->can("errors") and $self->model->errors->where($attribute) 0 8 8
Condition33%2/6
$errors_classes and $self->model->can("errors") and $self->model->errors->where($attribute)
!ll&&!rl&&r
800
$errors_classes and $self->model->can("errors")
!ll&&!rl&&r
800
MC/DC0%0/3
$errors_classes and $self->model->can("errors") and $self->model->errors->where($attribute)
$errors_classes $self->model->can("errors") $self->model->errors->where($attribute)
Truth table25%1/4
A and B and C
A= $errors_classesB= $self->model->can("errors")C= $self->model->errors->where($attribute)
ABCresult
0XX0
10X0
1100
1111
639 if $errors_classes && $self->model->can('errors') && $self->model->errors->where($attribute);
640  
641 8 ▶ my $model = $self->model->can('to_model') ? $self->model->to_model : $self->model;
Branch: 1 branch
BranchTrueFalseTotal
$self->model->can('to_model') ? : 0 8 8
642 8 ▶ my $include_hidden = exists($options->{include_hidden}) ? $options->{include_hidden} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'include_hidden'} ? : 0 8 8
643 8 ▶ my $unselected_default = exists($options->{unselected_value}) ? delete($options->{unselected_value}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'unselected_value'} ? : 1 7 8
644  
645  
646 8 my @selected = ();
647 8 my $name = '';
648 8 ▶ if( (ref($attribute_proto)||'') eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $attribute_proto || '') eq 'HASH') { } 1 7 8
Condition100%2/2
ref $attribute_proto || ''
l!l
17
MC/DC100%1/1
ref $attribute_proto || ''
ref $attribute_proto
Truth table100%2/2
A
A= ref $attribute_proto
Aresult
11
00
649 1 ▶ $options->{multiple} = 1 unless exists($options->{multiple});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"multiple"} 1 0 1
650 1 $options->{include_hidden} = 0; # Avoid adding two
651 1 my ($bridge, $value_method) = %$attribute_proto;
652 1 my $collection = $model->$bridge;
653 1 ▶ $collection = $self->tag_helpers->array_to_collection(map { $_->can('to_model') ? $_->to_model : $_ } @$collection)
Branch: 2 branches
BranchTrueFalseTotal
if (ref $collection || "") eq "ARRAY" 1 0 1
$_->can('to_model') ? : 0 2 2
Condition50%1/2
ref $collection || ''
l!l
10
MC/DC0%0/1
ref $collection || ''
ref $collection
Truth table50%1/2
A
A= ref $collection
Aresult
11
00
654 if (ref($collection)||'') eq 'ARRAY';
655  
656 1 ▶ while(my $item = $collection->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = $collection->next) 2 1 3
657 2 push @selected, $item->$value_method;
658 }
659 1 $name = $self->tag_name_for_attribute($bridge, +{multiple=>1}) . ".$value_method";
660 1 ▶ $options->{id} = $self->tag_id_for_attribute($bridge) . "_$value_method" unless exists $options->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"id"} 1 0 1
661 } else {
662 7 my $tag_value_proto = $self->tag_value_for_attribute($attribute_proto);
663 7 ▶ if( (ref($tag_value_proto)||'') eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $tag_value_proto || '') eq 'ARRAY') { } 1 6 7
Condition100%2/2
ref $tag_value_proto || ''
l!l
16
MC/DC100%1/1
ref $tag_value_proto || ''
ref $tag_value_proto
Truth table100%2/2
A
A= ref $tag_value_proto
Aresult
11
00
664 1 @selected = @$tag_value_proto;
665 1 $options->{multiple} = 1;
666 1 $include_hidden = 1;
667 } else {
668 6 @selected = ($tag_value_proto);
669 }
670 7 $name = $self->tag_name_for_attribute($attribute_proto);
671 7 $options->{id} = $self->tag_id_for_attribute($attribute_proto);
672 }
673  
674 8 my $options_tags = '';
675 8 ▶ if(!$block) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $block) { } 6 2 8
676 6 ▶ my $option_tags_proto = @_ ? shift : ();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 6 0 6
677 6 ▶ my @disabled = ( @{delete($options->{disabled})||[]});
Branch: 1 branch
BranchTrueFalseTotal
delete $options->{"disabled"} or [] 5 1 6
678 6 ▶ @selected = @{ delete($options->{selected})||[]} if exists($options->{selected});
Branch: 2 branches
BranchTrueFalseTotal
if exists $options->{"selected"} 1 5 6
delete $options->{"selected"} or [] 0 1 1
679 6 $options_tags = $self->tag_helpers->options_for_select($option_tags_proto, +{
680 selected => \@selected,
681 disabled => \@disabled,
682 });
683 } else {
684 2 $options_tags = $self->tag_helpers->join_tags($block->($model, $attribute_proto, @selected));
685 }
686  
687 8 ▶ $options->{include_hidden} = 0 if $options->{multiple};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"multiple"} 3 5 8
688 8 my $select_tag = $self->tag_helpers->select_tag($name, $options_tags, $options);
689 8 ▶ if($include_hidden && $options->{multiple}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($include_hidden and $options->{"multiple"}) 3 5 8
Condition66%2/3
$include_hidden and $options->{"multiple"}
!ll&&!rl&&r
053
MC/DC50%1/2
$include_hidden and $options->{"multiple"}
$include_hidden $options->{"multiple"}
Truth table66%2/3
A and B
A= $include_hiddenB= $options->{"multiple"}
ABresult
0X0
100
111
690 3 ▶ if(ref($attribute_proto)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $attribute_proto) { } 1 2 3
elsif (defined $unselected_default) { } 1 1 2
691 1 my ($bridge, $value_method) = %$attribute_proto;
692 1 my $hidden = $self->hidden("${bridge}[0]._nop", +{value=>1, id=>$options->{id}.'_hidden'});
693 1 $select_tag = $self->tag_helpers->join_tags($hidden, $select_tag);
694 } elsif(defined $unselected_default) {
695 1 my $hidden = $self->hidden("${attribute_proto}[0]", +{value=>$unselected_default, id=>$options->{id}.'_hidden'});
696 1 $select_tag = $self->tag_helpers->join_tags($hidden, $select_tag);
697 }
698 }
699 8 return $select_tag;
700 }
701  
702 #collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
703 sub collection_select {
704 4 ▶ my ($self, $method_proto, $collection) = (shift, shift, shift);
Subroutine collection_select: called CC=26 SCAR=36.4
Pod documented
705 4 ▶ my $options = (ref($_[-1])||'') eq 'HASH' ? pop(@_) : +{};
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[-1] || '') eq 'HASH' ? : 2 2 4
Condition100%2/2
ref $_[-1] || ''
l!l
22
MC/DC100%1/1
ref $_[-1] || ''
ref $_[-1]
Truth table100%2/2
A
A= ref $_[-1]
Aresult
11
00
706 4 $options = $self->merge_theme_field_opts('collection_select', undef, $options);
707
708 4 my ($value_method, $label_method) = (@_, 'value', 'label');
709 4 ▶ my $model = $self->model->can('to_model') ? $self->model->to_model : $self->model;
Branch: 1 branch
BranchTrueFalseTotal
$self->model->can('to_model') ? : 0 4 4
710 4 ▶ my $include_hidden = exists($options->{include_hidden}) ? delete($options->{include_hidden}) : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'include_hidden'} ? : 0 4 4
711 4 ▶ $collection = $model->$collection unless Scalar::Util::blessed($collection);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($collection) 0 4 4
712  
713 4 my (@selected, $name, $id) = @_;
714 4 ▶ if(ref $method_proto) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $method_proto) { } 1 3 4
715 1 ▶ $options->{multiple} = 1 unless exists($options->{multiple});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"multiple"} 1 0 1
716 1 ▶ $options->{include_hidden} = 0 unless exists($options->{include_hidden}); # Avoid adding two
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"include_hidden"} 1 0 1
717 1 my ($bridge, $value_method) = %$method_proto;
718 1 my $collection = $model->$bridge;
719 1 ▶ $collection = $self->tag_helpers->array_to_collection(map { $_->can('to_model') ? $_->to_model : $_ } @$collection)
Branch: 2 branches
BranchTrueFalseTotal
if (ref $collection || "") eq "ARRAY" 1 0 1
$_->can('to_model') ? : 0 2 2
Condition50%1/2
ref $collection || ''
l!l
10
MC/DC0%0/1
ref $collection || ''
ref $collection
Truth table50%1/2
A
A= ref $collection
Aresult
11
00
720 if (ref($collection)||'') eq 'ARRAY';
721  
722 1 ▶ while(my $item = $collection->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = $collection->next) 2 1 3
723 2 push @selected, $item->$value_method;
724 }
725 1 $name = $self->tag_name_for_attribute($bridge, +{multiple=>1}) . ".$value_method";
726 1 ▶ $options->{id} = $self->tag_id_for_attribute($bridge) . "_$value_method" unless exists $options->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"id"} 1 0 1
727 } else {
728 3 my $value = $self->tag_value_for_attribute($method_proto);
729 3 ▶ my $errors_classes = exists($options->{errors_classes}) ? delete($options->{errors_classes}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'errors_classes'} ? : 0 3 3
730 3 ▶ $options->{class} = join(' ', (grep { defined $_ } $options->{class}, $errors_classes))
Branch: 1 branch
BranchTrueFalseTotal
if $model->can("errors") and $model->errors->where($method_proto) 0 3 3
Condition33%1/3
$model->can("errors") and $model->errors->where($method_proto)
!ll&&!rl&&r
030
MC/DC0%0/2
$model->can("errors") and $model->errors->where($method_proto)
$model->can("errors") $model->errors->where($method_proto)
Truth table33%1/3
A and B
A= $model->can("errors")B= $model->errors->where($method_proto)
ABresult
0X0
100
111
731 if $model->can('errors') && $model->errors->where($method_proto);
732  
733 3 ▶ if((ref($value)||'') eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ((ref $value || '') eq 'ARRAY') { } 0 3 3
elsif (defined $value) { } 3 0 3
Condition50%1/2
ref $value || ''
l!l
03
MC/DC0%0/1
ref $value || ''
ref $value
Truth table50%1/2
A
A= ref $value
Aresult
11
00
734 0 @selected = @$value;
735 0 ▶ $options->{multiple} = 1 unless exists($options->{multiple});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"multiple"} 0 0 0
736 } elsif(defined($value)) {
737 3 @selected = ($value);
738 } else {
739 0 @selected = ();
740 }
741  
742 3 $name = $self->tag_name_for_attribute($method_proto);
743 3 $id = $self->tag_id_for_attribute($method_proto);
744 }
745
746 4 ▶ my @disabled = ( @{delete($options->{disabled})||[]});
Branch: 1 branch
BranchTrueFalseTotal
delete $options->{"disabled"} or [] 3 1 4
747 4 ▶ @selected = @{ delete($options->{selected})||[]} if exists($options->{selected});
Branch: 2 branches
BranchTrueFalseTotal
if exists $options->{"selected"} 1 3 4
delete $options->{"selected"} or [] 0 1 1
748 4 my $select_tag = $self->tag_helpers->select_tag(
749 $name,
750 $self->tag_helpers->options_from_collection_for_select($collection, $value_method, $label_method, +{
751 selected => \@selected,
752 disabled => \@disabled,
753 }),
754 $options);
755  
756 4 ▶ if($include_hidden && ref($method_proto)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($include_hidden and ref $method_proto) 1 3 4
Condition66%2/3
$include_hidden and ref $method_proto
!ll&&!rl&&r
031
MC/DC50%1/2
$include_hidden and ref $method_proto
$include_hidden ref $method_proto
Truth table66%2/3
A and B
A= $include_hiddenB= ref $method_proto
ABresult
0X0
100
111
757 1 my ($bridge, $value_method) = %$method_proto;
758 1 my $hidden = $self->hidden("${bridge}[0]._nop", +{value=>1, id=>$options->{id}.'_hidden'});
759 1 $select_tag = $self->tag_helpers->join_tags($hidden, $select_tag);
760 }
761  
762 4 return $select_tag;
763 }
764  
765 2 ▶sub default_collection_checkbox_include_hidden { return 1 }
Subroutine default_collection_checkbox_include_hidden: called CC=1 SCAR=0.0
Pod undocumented
766  
767 # $fb->collection_checkbox({person_roles => role_id}, $roles_rs, $value_method, $text_method, \%options, \&block);
768 # $fb->collection_checkbox({person_roles => role_id}, $roles_rs, $value_method, $text_method, \%options, \&block);
769 sub collection_checkbox {
770 3 ▶ my ($self, $attribute_spec, $collection) = (shift, shift, shift);
Subroutine collection_checkbox: called CC=31 SCAR=37.6
Pod documented
771 3 ▶ my $codeblock = (ref($_[-1])||'') eq 'CODE' ? pop(@_) : undef;
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[-1] || '') eq 'CODE' ? : 1 2 3
Condition100%2/2
ref $_[-1] || ''
l!l
21
MC/DC100%1/1
ref $_[-1] || ''
ref $_[-1]
Truth table100%2/2
A
A= ref $_[-1]
Aresult
11
00
772 3 ▶ my $options = (ref($_[-1])||'') eq 'HASH' ? pop(@_) : +{};
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[-1] || '') eq 'HASH' ? : 1 2 3
Condition100%2/2
ref $_[-1] || ''
l!l
12
MC/DC100%1/1
ref $_[-1] || ''
ref $_[-1]
Truth table100%2/2
A
A= ref $_[-1]
Aresult
11
00
773 3 $options = $self->merge_theme_field_opts('collection_checkbox', undef, $options);
774  
775 3 ▶ my $value_method = @_ ? shift(@_) : 'value';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 3 0 3
776 3 ▶ my $label_method = @_ ? shift(@_) : 'label';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 3 0 3
777 3 ▶ my $model = $self->model->can('to_model') ? $self->model->to_model : $self->model;
Branch: 1 branch
BranchTrueFalseTotal
$self->model->can('to_model') ? : 0 3 3
778 3 ▶ my $include_hidden = exists($options->{include_hidden}) ? delete($options->{include_hidden}) : $self->default_collection_checkbox_include_hidden;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'include_hidden'} ? : 1 2 3
779 3 ▶ my $container_tag = exists($options->{container_tag}) ? delete($options->{container_tag}) : 'div';
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'container_tag'} ? : 0 3 3
780  
781 # It's either +{ person_roles => role_id } or roles
782 3 my ($attribute, $attribute_value_method) = ();
783 3 ▶ if( (ref($attribute_spec)||'') eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $attribute_spec || '') eq 'HASH') { } 3 0 3
Condition50%1/2
ref $attribute_spec || ''
l!l
30
MC/DC0%0/1
ref $attribute_spec || ''
ref $attribute_spec
Truth table50%1/2
A
A= ref $attribute_spec
Aresult
11
00
784 3 ($attribute, $attribute_value_method) = (%{ $attribute_spec });
785 } else {
786 0 $attribute = $attribute_spec;
787 0 $attribute_value_method = $value_method;
788 }
789  
790 3 ▶ $codeblock = $self->_default_collection_checkbox_content unless defined($codeblock);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $codeblock 2 1 3
791  
792 3 my @checked_values = ();
793 3 my $value_collection = $self->tag_value_for_attribute($attribute);
794 3 ▶ $value_collection = $self->tag_helpers->array_to_collection(map { $_->can('to_model') ? $_->to_model : $_ } @$value_collection)
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value_collection || "") eq "ARRAY" 3 0 3
$_->can('to_model') ? : 0 6 6
Condition50%1/2
ref $value_collection || ''
l!l
30
MC/DC0%0/1
ref $value_collection || ''
ref $value_collection
Truth table50%1/2
A
A= ref $value_collection
Aresult
11
00
795 if (ref($value_collection)||'') eq 'ARRAY';
796  
797 3 ▶ while(my $value_model = $value_collection->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $value_model = $value_collection->next) 6 3 9
798 6 ▶ push @checked_values, $value_model->$attribute_value_method unless $value_model->can('is_marked_for_deletion') && $value_model->is_marked_for_deletion;
Branch: 1 branch
BranchTrueFalseTotal
unless $value_model->can("is_marked_for_deletion") and $value_model->is_marked_for_deletion 6 0 6
Condition33%1/3
$value_model->can("is_marked_for_deletion") and $value_model->is_marked_for_deletion
!ll&&!rl&&r
600
MC/DC0%0/2
$value_model->can("is_marked_for_deletion") and $value_model->is_marked_for_deletion
$value_model->can("is_marked_for_deletion") $value_model->is_marked_for_deletion
Truth table33%1/3
A and B
A= $value_model->can("is_marked_for_deletion")B= $value_model->is_marked_for_deletion
ABresult
0X0
100
111
799 }
800  
801 3 my @checkboxes = ();
802 my $checkbox_builder_options = +{
803 3 ▶ builder => (exists($options->{builder}) ? delete($options->{builder}) : $self->DEFAULT_COLLECTION_CHECKBOX_BUILDER),
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'builder'} ? : 0 3 3
804 value_method => $value_method,
805 label_method => $label_method,
806 attribute_value_method => $attribute_value_method,
807 parent_builder => $self,
808 attribute => $attribute,
809 tag_helpers => $self->tag_helpers,
810 errors => [$model->errors->where($attribute)],
811 };
812 3 ▶ $checkbox_builder_options->{namespace} = $self->namespace if $self->has_namespace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_namespace 0 3 3
813  
814 3 ▶ $collection = $model->$collection unless Scalar::Util::blessed($collection);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($collection) 0 3 3
815  
816 3 ▶ while (my $checkbox_model = $collection->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $checkbox_model = $collection->next) 9 3 12
817 9 my $index = $self->nested_child_index($attribute);
818 9 my $name = "@{[ $self->name ]}.${attribute}";
819 my $checked = grep {
820 9 ▶ my $current_value = $checkbox_model->can('read_attribute_for_html') ?
Branch: 1 branch
BranchTrueFalseTotal
$checkbox_model->can('read_attribute_for_html') ? : 0 18 18
821 $checkbox_model->read_attribute_for_html($value_method)
822 : $checkbox_model->$value_method;
823 18 $_ eq $current_value;
824 } @checked_values;
825  
826 9 ▶ if($include_hidden && !scalar(@checkboxes)) { # Add nop as first to handle empty list
Branch: 1 branch
BranchTrueFalseTotal
if ($include_hidden and not scalar @checkboxes) 2 7 9
Condition100%3/3
$include_hidden and not scalar @checkboxes
!ll&&!rl&&r
342
MC/DC100%2/2
$include_hidden and not scalar @checkboxes
$include_hidden not scalar @checkboxes
Truth table100%3/3
A and not(B)
A= $include_hiddenB= not scalar @checkboxes
ABresult
0X0
100
111
827 2 my $hidden_fb = $self->tag_helpers->_instantiate_builder($name, $value_collection->build, {%$checkbox_builder_options, index=>$index});
828 2 push @checkboxes, $hidden_fb->hidden('_nop', +{value=>'1'});
829 2 $index = $self->nested_child_index($attribute);
830 }
831  
832 9 $checkbox_builder_options->{index} = $index;
833 9 $checkbox_builder_options->{checked} = $checked;
834 9 $checkbox_builder_options->{parent_builder} = $self;
835 9 my $checkbox_fb = $self->tag_helpers->_instantiate_builder($name, $checkbox_model, $checkbox_builder_options);
836 9 push @checkboxes, $codeblock->($checkbox_fb);
837 }
838 3 ▶ $collection->reset if $collection->can('reset');
Branch: 1 branch
BranchTrueFalseTotal
if $collection->can("reset") 3 0 3
839 3 my $checkbox_content = $self->view->safe_concat(@checkboxes);
840  
841 3 ▶ my $errors_classes = exists($options->{errors_classes}) ? delete($options->{errors_classes}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'errors_classes'} ? : 0 3 3
842 3 ▶ $options->{class} = join(' ', (grep { defined $_ } $options->{class}, $errors_classes))
Branch: 1 branch
BranchTrueFalseTotal
if $errors_classes and $model->can("errors") and $model->errors->where($attribute) 0 3 3
Condition33%2/6
$errors_classes and $model->can("errors") and $model->errors->where($attribute)
!ll&&!rl&&r
300
$errors_classes and $model->can("errors")
!ll&&!rl&&r
300
MC/DC0%0/3
$errors_classes and $model->can("errors") and $model->errors->where($attribute)
$errors_classes $model->can("errors") $model->errors->where($attribute)
Truth table25%1/4
A and B and C
A= $errors_classesB= $model->can("errors")C= $model->errors->where($attribute)
ABCresult
0XX0
10X0
1100
1111
843 if $errors_classes && $model->can('errors') && $model->errors->where($attribute);
844  
845 3 return $self->tag_helpers->content_tag($container_tag, $checkbox_content, +{
846 id => $self->tag_id_for_attribute($attribute),
847 %$options,
848 });
849 }
850  
851 sub _default_collection_checkbox_content {
852 2 ▶ my ($self) = @_;
Subroutine _default_collection_checkbox_content: called CC=1 SCAR=0.0
853 return sub {
854 6 ▶ my ($fb) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
855 6 my $label = $fb->label;
856 6 my $checkbox = $fb->checkbox;
857 6 return $self->tag_helpers->join_tags($label, $checkbox);
858 2 };
859 }
860  
861 2 ▶sub default_collection_radio_buttons_include_hidden { return 1 }
Subroutine default_collection_radio_buttons_include_hidden: called CC=1 SCAR=0.0
Pod undocumented
862  
863 sub collection_radio_buttons {
864 3 ▶ my ($self, $attribute, $collection) = (shift, shift, shift);
Subroutine collection_radio_buttons: called CC=26 SCAR=34.8
Pod documented
865 3 ▶ my $codeblock = (ref($_[-1])||'') eq 'CODE' ? pop(@_) : undef;
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[-1] || '') eq 'CODE' ? : 1 2 3
Condition100%2/2
ref $_[-1] || ''
l!l
21
MC/DC100%1/1
ref $_[-1] || ''
ref $_[-1]
Truth table100%2/2
A
A= ref $_[-1]
Aresult
11
00
866 3 ▶ my $options = (ref($_[-1])||'') eq 'HASH' ? pop(@_) : +{};
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[-1] || '') eq 'HASH' ? : 1 2 3
Condition100%2/2
ref $_[-1] || ''
l!l
12
MC/DC100%1/1
ref $_[-1] || ''
ref $_[-1]
Truth table100%2/2
A
A= ref $_[-1]
Aresult
11
00
867 3 $options = $self->merge_theme_field_opts('collection_radio_buttons', $attribute, $options);
868  
869 3 ▶ my $value_method = @_ ? shift(@_) : 'value';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 3 0 3
870 3 ▶ my $label_method = @_ ? shift(@_) : 'label';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 3 0 3
871 3 ▶ my $model = $self->model->can('to_model') ? $self->model->to_model : $self->model;
Branch: 1 branch
BranchTrueFalseTotal
$self->model->can('to_model') ? : 0 3 3
872 3 ▶ my $checked_value = exists($options->{checked_value}) ? delete($options->{checked_value}) : $self->tag_value_for_attribute($attribute);
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'checked_value'} ? : 0 3 3
873 3 ▶ my $include_hidden = exists($options->{include_hidden}) ? delete($options->{include_hidden}) : $self->default_collection_radio_buttons_include_hidden;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'include_hidden'} ? : 1 2 3
874 3 ▶ my $container_tag = exists($options->{container_tag}) ? delete($options->{container_tag}) : 'div';
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'container_tag'} ? : 0 3 3
875  
876 3 ▶ $codeblock = $self->_default_collection_radio_buttons_content unless defined($codeblock);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $codeblock 2 1 3
877  
878 3 my @radio_buttons = ();
879 my $radio_buttons_builder_options = +{
880 3 ▶ builder => (exists($options->{builder}) ? delete($options->{builder}) : $self->DEFAULT_COLLECTION_RADIO_BUTTON_BUILDER),
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'builder'} ? : 0 3 3
881 value_method => $value_method,
882 label_method => $label_method,
883 checked_value => $checked_value,
884 parent_builder => $self,
885 attribute => $attribute,
886 tag_helpers => $self->tag_helpers,
887 errors => [$model->errors->where($attribute)],
888 };
889 3 ▶ $radio_buttons_builder_options->{namespace} = $self->namespace if $self->has_namespace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_namespace 0 3 3
890  
891 3 ▶ $collection = $model->$collection unless Scalar::Util::blessed($collection);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($collection) 0 3 3
892  
893 3 ▶ while (my $radio_button_model = $collection->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $radio_button_model = $collection->next) 9 3 12
894 9 my $name = "@{[ $self->name ]}.${attribute}";
895 9 ▶ my $current_value = $radio_button_model->can('read_attribute_for_html') ?
Branch: 1 branch
BranchTrueFalseTotal
$radio_button_model->can('read_attribute_for_html') ? : 0 9 9
896 $radio_button_model->read_attribute_for_html($value_method) :
897 $radio_button_model->$value_method;
898 9 ▶ my $checked = $current_value eq ($checked_value||'') ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$current_value eq ($checked_value || '') ? : 3 6 9
Condition50%1/2
$checked_value || ''
l!l
90
MC/DC0%0/1
$checked_value || ''
$checked_value
Truth table50%1/2
A
A= $checked_value
Aresult
11
00
899  
900 9 ▶ if($include_hidden && !scalar(@radio_buttons) ) { # Add nop as first to handle empty list
Branch: 1 branch
BranchTrueFalseTotal
if ($include_hidden and not scalar @radio_buttons) 2 7 9
Condition100%3/3
$include_hidden and not scalar @radio_buttons
!ll&&!rl&&r
342
MC/DC100%2/2
$include_hidden and not scalar @radio_buttons
$include_hidden not scalar @radio_buttons
Truth table100%3/3
A and not(B)
A= $include_hiddenB= not scalar @radio_buttons
ABresult
0X0
100
111
901 2 my $hidden_fb = $self->tag_helpers->_instantiate_builder($name, $model, $radio_buttons_builder_options);
902 2 push @radio_buttons, $hidden_fb->hidden($name, +{name=>$name, id=>$self->tag_id_for_attribute($attribute).'_hidden', value=>''});
903 }
904  
905 9 $radio_buttons_builder_options->{checked} = $checked;
906  
907 9 my $radio_button_fb = $self->tag_helpers->_instantiate_builder($name, $radio_button_model, $radio_buttons_builder_options);
908 9 push @radio_buttons, $codeblock->($radio_button_fb);
909 }
910 3 ▶ $collection->reset if $collection->can('reset');
Branch: 1 branch
BranchTrueFalseTotal
if $collection->can("reset") 3 0 3
911 3 my $radios = $self->view->safe_concat(@radio_buttons);
912  
913 3 ▶ my $errors_classes = exists($options->{errors_classes}) ? delete($options->{errors_classes}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'errors_classes'} ? : 0 3 3
914 3 ▶ $options->{class} = join(' ', (grep { defined $_ } $options->{class}, $errors_classes))
Branch: 1 branch
BranchTrueFalseTotal
if $errors_classes and $model->can("errors") and $model->errors->where($attribute) 0 3 3
Condition33%2/6
$errors_classes and $model->can("errors") and $model->errors->where($attribute)
!ll&&!rl&&r
300
$errors_classes and $model->can("errors")
!ll&&!rl&&r
300
MC/DC0%0/3
$errors_classes and $model->can("errors") and $model->errors->where($attribute)
$errors_classes $model->can("errors") $model->errors->where($attribute)
Truth table25%1/4
A and B and C
A= $errors_classesB= $model->can("errors")C= $model->errors->where($attribute)
ABCresult
0XX0
10X0
1100
1111
915 if $errors_classes && $model->can('errors') && $model->errors->where($attribute);
916  
917 3 return $self->tag_helpers->content_tag($container_tag, $radios, +{
918 id => $self->tag_id_for_attribute($attribute),
919 %$options,
920 });
921 }
922  
923 sub _default_collection_radio_buttons_content {
924 2 ▶ my ($self) = @_;
Subroutine _default_collection_radio_buttons_content: called CC=1 SCAR=0.0
925 return sub {
926 6 ▶ my ($fb) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
927 6 my $label = $fb->label();
928 6 my $checkbox = $fb->radio_button();
929 6 return $self->tag_helpers->join_tags($label, $checkbox);
930 2 };
931 }
932  
933 sub radio_buttons {
934 0 ▶ my ($self, $attribute, $collection_proto) = (shift, shift, shift);
Subroutine radio_buttons: not called CC=3 SCAR=24.8
Pod documented
935 0 ▶ $collection_proto = $self->model->$collection_proto unless (ref($collection_proto)||'') eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $collection_proto || "") eq "ARRAY" 0 0 0
Condition0%0/2
ref $collection_proto || ''
l!l
00
MC/DC0%0/1
ref $collection_proto || ''
ref $collection_proto
Truth table0%0/2
A
A= ref $collection_proto
Aresult
11
00
936 0 my $collection = $self->tag_helpers->array_to_collection(@$collection_proto);
937 0 return $self->collection_radio_buttons($attribute, $collection, @_);
938 }
939  
940 # select collection needs work (with multiple)
941 # select with opt grounps needs to work
942 # ?? date and date time helpers (month field, weeks, etc) ??
943  
944 2 1;
945  
946 =head1 NAME
947  
948 Valiant::HTML::FormBuilder - General HTML Forms
949  
950 =head1 SYNOPSIS
951  
952 Given a model with the correct API such as:
953  
954 package Local::Person;
955  
956 use Moo;
957 use Valiant::Validations;
958  
959 has first_name => (is=>'ro');
960 has last_name => (is=>'ro');
961  
962 validates ['first_name', 'last_name'] => (
963 length => {
964 maximum => 10,
965 minimum => 3,
966 }
967 );
968  
969 Wrap a formbuilder object around it and generate HTML form field controls:
970  
971 my $person = Local::Person->new(first_name=>'J', last_name=>'Napiorkowski');
972 $person->validate;
973  
974 my $fb = Valiant::HTML::FormBuilder->new(
975 model => $person,
976 name => 'person'
977 );
978  
979 print $fb->input('first_name');
980 # <input id="person_first_name" name="person.first_name" type="text" value="J"/>
981  
982 print $fb->errors_for('first_name');
983 # <div>First Name is too short (minimum is 3 characters)</div>
984  
985 Although you can create a formbuilder instance directly as in the above example you might
986 find it easier to use the export helper method L<Valiant::HTML::Form/form_for> which encapsulates
987 the display logic needed for creating the C<form> tags. This builder creates form tag elements
988 but not the actual C<form> open and close tags.
989  
990 =head1 DESCRIPTION
991  
992 This class wraps an underlying data model and makes it easy to build HTML form elements based on
993 the state of that model. Inspiration for this design come from Ruby on Rails Formbuilder as well
994 as similar designs in the Phoenix Framework.
995  
996 You can subclass this to future customize how your form elements display as well as to add more complex
997 form elements for your templates.
998  
999 Documentation here is basically API level, a more detailed tutorial will follow eventually but for now
1000 you'll need to review the source, test cases and example application bundled with this distribution
1001 for for hand holding.
1002  
1003 Currently this is designed to work mostly with the L<Valiant> model validation framework as well as the
1004 glue for L<DBIx:Class>, L<DBIx:Class::Valiant>, although I did take pains to try and make the API
1005 agnostic many of the test cases are assuming that stack and getting that integration working well is
1006 the primary use case for me. Thoughts and code to make this more stand alone are very welcomed.
1007  
1008 =head1 ATTRIBUTES
1009  
1010 This class defines the following attributes used in creating an instance.
1011  
1012 =head2 model
1013  
1014 This is the data model that the formbuilder inspects for field state and error conditions. This should be
1015 a model that does the API described here: L<Valiant::HTML::Form/'REQUIRED MODEL API'>. Required but the API
1016 is pretty flexible (see docs).
1017  
1018 Please note that my initial use case for this is using L<Valiant> for validation and L<DBIx::Class> as the
1019 model (via L<DBIx:Class::Valiant>) so that combination has the most testing and examples. If you are using
1020 a different storage or validation setup you need to complete the API described. Please send test cases
1021 and pull requests to improve interoperability!
1022  
1023 =head2 name
1024  
1025 This is a string which is the internal name given to the model. This is used to set a namespace for form
1026 field C<name> attributes and the default namespace for C<id> attributes. Required.
1027  
1028 =head2 options
1029  
1030 A optional hashref of options used in form field generation. Some of these might become attributes in the
1031 future. Here's a list of the current options
1032  
1033 =over 4
1034  
1035 =item index
1036  
1037 The index of the formbuilder when it is a sub formbuilder with a parent and we are iterating over a collection.
1038  
1039 =item child_index
1040  
1041 When creating a sub formbuilder that is an element in a collection, this is used to pass the index value
1042  
1043 =item builder
1044  
1045 The package name of the current builder
1046  
1047 =item parent_builder
1048  
1049 The parent formbuilder instance to a sub builder.
1050  
1051 =item include_id
1052  
1053 Used to indicated that a sub formbuilder should add hidden fields indicating the storage ID for the current model.
1054  
1055 =item namespace
1056  
1057 The ID namespace; used to populate the C<namespace> attribute.
1058  
1059 =item as
1060  
1061 Used to override how the class and ids are made for your forms.
1062  
1063 =back
1064  
1065 =head2 index
1066  
1067 The current index of a collection for which the current formbuilder is one item in.
1068  
1069 =head2 namespace
1070  
1071 Used to add a prefix to the ID for your form elements.
1072  
1073 =head2 allow_method_names_outside_model
1074  
1075 Default is false. Generally we expect C<method_name> to be an actual method on the
1076 C<model> and if its not we expect an exception. This helps to prevent typos from
1077 leading to unexpected results. However sometimes you may wish create a form field that
1078 has a name that isn't on the model but still respects the current namespace and index.
1079 These names would appear in the POST request body and could be used for things other than
1080 updating or creating a model.
1081  
1082 =head2 skip_default_ids
1083  
1084 Defaults to false. Generally we create an html C<id> attribute for the field based
1085 on a convention which includes the model name, index and method name. Setting this
1086 to true prevents that so you should set C<id> manually unless you don't want them.
1087 Please note that even if this is false, you can always override the C<id> on a per
1088 field basis by setting it manually.
1089  
1090 =head2 view
1091  
1092 Optional. The view or template object that is using the formbuilder. If available can be used to
1093 influence how the HTML for controls are created.
1094  
1095 Generally used to provide HTML escaping and safe string tagging methods that are compatible with
1096 your template system. For example L<Mojo::Template> provides its own system for marking strings
1097 safe for template display. I you don't provide a view then we will use L<Valiant::HTML::SafeString>
1098 for automatic HTML escaping. If you are not using a view or template system (for example like
1099 L<Template::Toolkit> ) that does automatic escaping then the built in escaping features are probably
1100 fine.
1101  
1102 If you provide a view it should provide the following API methods:
1103  
1104 =over 4
1105  
1106 =item raw
1107  
1108 given a string return a single tagged object which is marked as safe for display. Do not do any HTML
1109 escaping on the string. This is used when you want to pass strings straight to display and that you
1110 know is safe. Be careful with this to avoid HTML injection attacks.
1111  
1112 =item safe
1113  
1114 given a string return a single tagged object which is marked as safe for display. First HTML escape the
1115 string as safe unless its already been done (no double escaping).
1116  
1117 =item safe_concat
1118  
1119 Same as C<safe> but instead works an an array of strings (or mix of strings and safe string objects) and
1120 concatenates them all into one big safe marked string.
1121  
1122 =item html_escape
1123  
1124 Given a string return string that has been HTML escaped.
1125  
1126 =item read_attribute_for_view
1127  
1128 Given an attribute name return the value that the view has defined for it.
1129  
1130 =item attribute_for_view_exists
1131  
1132 Given an attribute name return true if the view has defined a value for it.
1133  
1134 =back
1135  
1136 Both C<raw>, C<safe> and C<safe_concat> should return a 'tagged' object which is specific to your view or
1137 template system. However this object must 'stringify' to the safe version of the string to be displayed. See
1138 L<Valiant::HTML::SafeString> for example API. We use <Valiant::HTML::SafeString> internally to provide
1139 safe escaping if you're view doesn't do automatic escaping, as many older template systems like Template
1140 Toolkit.
1141  
1142 B<NOTE>: In the future the view API might change so keep an eye on this spot.
1143  
1144 =head1 METHODS
1145  
1146 This class defines the following public instance methods.
1147  
1148 =head2 form_has_errors
1149  
1150 Display a message if the form has errors. This is a convenience method that you can use in your
1151 template to display a message if there are any errors in the form. This is useful if you want to
1152 display a message at the top of the form. You can also use this method to display a message at the
1153 top of a sub formbuilder if you are using sub formbuilders.
1154  
1155 A form has errors if any of the fields has an error or if the model has errors.
1156  
1157 $fb->form_has_errors();
1158 $fb->form_has_errors(\%attrs);
1159 $fb->form_has_errors(\%attrs, $content);
1160 $fb->form_has_errors(\$content);
1161 $fb->form_has_errors(\&template);
1162  
1163 Examples:
1164  
1165 # with default content
1166 $fb->form_has_errors;
1167  
1168 # with simple content
1169 $fb->form_has_errors('There were errors in the form. Please correct them.');
1170  
1171 # Simple content with attributes
1172 $fb->form_has_errors({ class=>'alert alert-danger', role=>'alert' },
1173 'There were errors in the form. Please correct them and try again.');
1174  
1175 # with complex content
1176 $fb->form_has_errors(sub ($self, $fb, $contact) {
1177 div +{ class=>'alert alert-danger', role=>'alert' }, [
1178 'There were errors in the form. Please correct them and try again.',
1179 ]
1180 });
1181  
1182 If you pass a scalar as content, the scalar can be a string or a translation tag.
1183  
1184 =head2 model_errors
1185  
1186 $fb->model_errors();
1187 $fb->model_errors(\%attrs);
1188 $fb->model_errors(\%attrs, \&template); # %attrs limited to 'max_errors' and 'show_message_on_field_errors'
1189 $fb->model_errors(\&template);
1190  
1191 Display model level errors, either with a default or custom template. 'Model' errors are
1192 errors that are not associated with a model attribute in particular, but rather the model
1193 as a whole.
1194  
1195 Arguments to this method are optional. "\%attrs" is a hashref which is passed to the tag
1196 builder to create any needed HTML attributes (such as class and style). "\&template" is
1197 a coderef that gets the @errors as an argument and you can use it to customize how the errors
1198 are displayed. Otherwise we use a default template that lists the errors with an HTML ordered
1199 list, or a C<div> if there's only one error.
1200  
1201 "\%attrs" can also contain two options that gives you some additional control over the display
1202  
1203 =over
1204  
1205 =item max_errors
1206  
1207 Don't display more than a certain number of errors
1208  
1209 =item show_message_on_field_errors
1210  
1211 Sometimes you want a global message displayed when there are field errors. L<Valiant> doesn't
1212 add a model error if there's field errors (although it would be easy for you to add this yourself
1213 with a model validation) so this makes it easy to display such a message. If a string or translation
1214 tag then show that, if its a '1' the show the default message, which is "Form has errors" unless
1215 you overide it.
1216  
1217 This can be a useful option when you have a long form and you want a user to know there's errors
1218 possibly off the browser screen.
1219  
1220 =back
1221  
1222 Examples. Assume two model level errors "Trouble 1" and "Trouble 2":
1223  
1224 $fb->model_errors;
1225 # <ol><li>Trouble 1</li><li>Trouble 2</li></ol>
1226  
1227 $fb->model_errors({class=>'foo'});
1228 # <ol class="foo"><li>Trouble 1</li><li>Trouble 2</li></ol>
1229  
1230 $fb->model_errors({max_errors=>1});
1231 # <div>Trouble 1</div>
1232  
1233 $fb->model_errors({max_errors=>1, class=>'foo'})
1234 # <div class="foo">Trouble 1</div>
1235  
1236 $fb->model_errors({show_message_on_field_errors=>1})
1237 # <ol><li>Form has errors</li><li>Trouble 1</li><li>Trouble 2</li></ol>
1238  
1239 $fb->model_errors({show_message_on_field_errors=>"Bad!"})
1240 # <ol><li>Bad!</li><li>Trouble 1</li><li>Trouble 2</li></ol>
1241  
1242 $fb->model_errors(sub {
1243 my (@errors) = @_;
1244 join " | ", @errors;
1245 });
1246 # Trouble 1 | Trouble 2
1247  
1248 =head2 label
1249  
1250 $fb->label($attribute)
1251 $fb->label($attribute, \%options)
1252 $fb->label($attribute, $content)
1253 $fb->label($attribute, \%options, $content)
1254 $fb->label($attribute, \&content); sub content { my ($translated_attribute) = @_; ... }
1255 $fb->label($attribute, \%options, \&content); sub content { my ( $translated_attribute) = @_; ... }
1256  
1257 Creates a HTML form element C<label> with the given "\%options" passed to the tag builder to
1258 create HTML attributes and an optional "$content". If "$content" is not provided we use the
1259 human, translated (if available) version of the "$attribute" for the C<label> content. Alternatively
1260 you can provide a template which is a subroutine reference which recieves the translated attribute
1261 as an argument. Examples:
1262  
1263 $fb->label('first_name');
1264 # <label for="person_first_name">First Name</label>
1265  
1266 $fb->label('first_name', {class=>'foo'});
1267 # <label class="foo" for="person_first_name">First Name</label>
1268  
1269 $fb->label('first_name', 'Your First Name');
1270 # <label for="person_first_name">Your First Name</label>
1271  
1272 $fb->label('first_name', {class=>'foo'}, 'Your First Name');
1273 # <label class="foo" for="person_first_name">Your First Name</label>
1274  
1275 $fb->label('first_name', sub {
1276 my $translated_attribute = shift;
1277 return "$translated_attribute ",
1278 $fb->input('first_name');
1279 });
1280 # <label for="person_first_name">
1281 # First Name
1282 # <input id="person_first_name" name="person.first_name" type="text" value="John"/>
1283 # </label>
1284  
1285 $fb->label('first_name', +{class=>'foo'}, sub {
1286 my $translated_attribute = shift;
1287 return "$translated_attribute ",
1288 $fb->input('first_name');
1289 });
1290 # <label class="foo" for="person_first_name">
1291 # First Name
1292 # <input id="person_first_name" name="person.first_name" type="text" value="John"/>
1293 # </label>
1294  
1295 =head2 errors_for
1296  
1297 $fb->errors_for($attribute)
1298 $fb->errors_for($attribute, \%options)
1299 $fb->errors_for($attribute, \%options, \&template)
1300 $fb->errors_for($attribute, \&template)
1301  
1302 Similar to L</model_errors> but for errors associated with an attribute of a model. Accepts
1303 the $attribute name, a hashref of \%options (used to set options controling the display of
1304 errors as well as used by the tag builder to create HTML attributes for the containing tag) and
1305 lastly an optional \&template which is a subroutine reference that received an array of the
1306 translated errors for when you need very custom error display. If omitted we use a default
1307 template displaying errors in an ordered list (if more than one) or wrapped in a C<div> tag
1308 (if only one error).
1309  
1310 \%options used for error display and which are not passed to the tag builder as HTML attributes:
1311  
1312 =over
1313  
1314 =item max_errors
1315  
1316 Don't display more than a certain number of errors
1317  
1318 =back
1319  
1320 Assume the attribute 'last_name' has the following two errors in the given examples: "first Name
1321 is too short", "First Name contains non alphabetic characters".
1322  
1323 $fb->errors_for('first_name');
1324 # <ol><li>First Name is too short (minimum is 3 characters)</li><li>First Name contains non alphabetic characters</li></ol>
1325  
1326 $fb->errors_for('first_name', {class=>'foo'});
1327 # <ol class="foo"><li>First Name is too short (minimum is 3 characters)</li><li>First Name contains non alphabetic characters</li></ol>
1328  
1329 $fb->errors_for('first_name', {class=>'foo', max_errors=>1});
1330 # <div class="foo">First Name is too short (minimum is 3 characters)</div>
1331  
1332 $fb->errors_for('first_name', sub {
1333 my (@errors) = @_;
1334 join " | ", @errors;
1335 });
1336 # First Name is too short (minimum is 3 characters) | First Name contains non alphabetic characters
1337  
1338 =head2 input
1339  
1340 $fb->input($attribute, \%options)
1341 $fb->input($attribute)
1342  
1343 Create an C<input> form tag using the $attribute's value (if any) and optionally passing a hashref of
1344 \%options which are passed to the tag builder to create HTML attributes for the C<input> tag. Optionally
1345 add C<errors_classes> which is a string that is appended to the C<class> attribute when the $attribute has
1346 errors. Examples:
1347  
1348 $fb->input('first_name');
1349 # <input id="person_first_name" name="person.first_name" type="text" value="J"/>
1350  
1351 $fb->input('first_name', {class=>'foo'});
1352 # <input class="foo" id="person_first_name" name="person.first_name" type="text" value="J"/>
1353  
1354 $fb->input('first_name', {errors_classes=>'error'});
1355 # <input class="error" id="person_first_name" name="person.first_name" type="text" value="J"/>
1356  
1357 $fb->input('first_name', {class=>'foo', errors_classes=>'error'});
1358 # <input class="foo error" id="person_first_name" name="person.first_name" type="text" value="J"/>
1359  
1360 Special \%options:
1361  
1362 =over 4
1363  
1364 =item errors_classes
1365  
1366 A string that is appended to the C<class> attribute if the $attribute has errors (as defined by the model API)
1367  
1368 =back
1369  
1370 =head2 password
1371  
1372 $fb->password($attribute, \%options)
1373 $fb->password($attribute)
1374  
1375 Create a C<password> HTML form field. Similar to L</input> but sets the C<type> to 'password' and also
1376 sets C<value> to '' since generally you don't want to show the current password (and if you are doing the
1377 right thing and saving a 1 way hash not the plain text you don't even have it to show anyway).
1378  
1379 Example:
1380  
1381 $fb->password('password');
1382 # <input id="person_password" name="person.password" type="password" value=""/>
1383  
1384 $fb->password('password', {class='foo'});
1385 # <input class="foo" id="person_password" name="person.password" type="password" value=""/>
1386  
1387 $fb->password('password', {class='foo', errors_classes=>'error'});
1388 # <input class="foo error" id="person_password" name="person.password" type="password" value=""/>
1389  
1390 =head2 hidden
1391  
1392 $fb->hidden($attribute, \%options)
1393 $fb->hidden($attribute)
1394  
1395 Create a C<hidden> HTML form field. Similar to L</input> but sets the C<type> to 'hidden'.
1396  
1397 $fb->hidden('id');
1398 # <input id="person_id name="person.id" type="hidden" value="101"/>
1399  
1400 $fb->hidden('id', {class='foo'});
1401 # <input class="foo" id="person_id name="person.id" type="hidden" value="101"/>
1402  
1403 =head2 text_area
1404  
1405 $fb->text_area($attribute);
1406 $fb->text_area($attribute, \%options);
1407  
1408 Create an HTML C<text_area> tag based on the attribute value and with optional \%options
1409 which is a a hashref passed to the tag builder for generating HTML attributes. Can also set
1410 C<errors_classes> that will append a string of additional CSS classes when the $attribute has
1411 errors. Examples:
1412  
1413 $fb->text_area('comments');
1414 # <textarea id="person_comments" name="person.comments">J</textarea>
1415  
1416 $fb->text_area('comments', {class=>'foo'});
1417 # <textarea class="foo" id="person_comments" name="person.comments">J</textarea>
1418  
1419 $fb->text_area('comments', {class=>'foo', errors_classes=>'error'});
1420 # <textarea class="foo error" id="person_comments" name="person.comments">J</textarea>
1421  
1422 Special \%options:
1423  
1424 =over 4
1425  
1426 =item errors_classes
1427  
1428 A string that is appended to the C<class> attribute if the $attribute has errors (as defined by the model API)
1429  
1430 =back
1431  
1432 =head2 checkbox
1433  
1434 $fb->checkbox($attribute);
1435 $fb->checkbox($attribute, \%options);
1436 $fb->checkbox($attribute, $checked_value, $unchecked_value);
1437 $fb->checkbox($attribute, \%options, $checked_value, $unchecked_value);
1438  
1439 Generate an HTML form checkbox element with its state based on evaluating the value of $attribute
1440 in a boolean context. If $attribute is true then the C<checkbox> will be checked. May also pass a
1441 hashref of \%options, which contain render instructions and HTML attributes used by the tag builder.
1442 "$checked_value" and "$unchecked_value" specify the values when the checkbox is checked or not (defaults
1443 to 1 for checked and 0 for unchecked, but $unchecked is ignored if option C<include_hidden> is set to
1444 false; see below).
1445  
1446 Special \%options:
1447  
1448 =over 4
1449  
1450 =item errors_classes
1451  
1452 A string that is appended to the C<class> attribute if the $attribute has errors (as defined by the model API)
1453  
1454 =item include_hidden
1455  
1456 Defaults to true. Since the rules for an HTML form checkbox specify that if the checkbox is 'unchecked' then
1457 nothing is submitted. This can cause issues if you are expecting a submission that somehow indicates 'unchecked'
1458 For example you might have a status field boolean where unchecked should indicate 'false'. So by default we add
1459 a hidden field with the same name as the checkbox, with a value set to $unchecked_value (defaults to 0). In the
1460 case where the field is checked then you'll get two values for the same field name so you should have logic that
1461 in the case that field name is an array then take the last one (if you are using L<Plack::Request> this is using
1462 L<Hash::MultiValue> which does this by default; if you are using L<Catalyst> you can use the C<use_hash_multivalue_in_request>
1463 option or you can use something like L<Catalyst::TraitFor::Request::StructuredParameters> which has options to
1464 help with this. If you are using L<Mojolicious> then the C<param> method works this way as well.
1465  
1466 =item checked
1467  
1468 A boolean value to indicate if the checkbox field is 'checked' when generated. By default a checkbox state is
1469 determined by the value of $attribute for the underlying model. You can use this to override (for example you
1470 might wish a checkbox default state to be checked when creating a new entry when it would otherwise be false).
1471  
1472 =back
1473  
1474 Examples:
1475  
1476 $fb->checkbox('status');
1477 # <input name="person.status" type="hidden" value="0"/>
1478 # <input id="person_status" name="person.status" type="checkbox" value="1"/>
1479  
1480 $fb->checkbox('status', {class=>'foo'});
1481 # <input name="person.status" type="hidden" value="0"/>
1482 # <input class="foo" id="person_status" name="person.status" type="checkbox" value="1"/>
1483  
1484 $fb->checkbox('status', 'active', 'deactive');
1485 # <input name="person.status" type="hidden" value="deactive"/>
1486 # <input id="person_status" name="person.status" type="checkbox" value="active"/>
1487  
1488 $fb->checkbox('status', {include_hidden=>0});
1489 # <input id="person_status" name="person.status" type="checkbox" value="1"/>
1490  
1491 $person->status(1);
1492 $fb->checkbox('status', {include_hidden=>0});
1493 # <input checked id="person_status" name="person.status" type="checkbox" value="1"/>
1494  
1495 $person->status(0);
1496 $fb->checkbox('status', {include_hidden=>0, checked=>1});
1497 # <input checked id="person_status" name="person.status" type="checkbox" value="1"/>
1498  
1499 $fb->checkbox('status', {include_hidden=>0, errors_classes=>'err'});
1500 # <input class="err" id="person_status" name="person.status" type="checkbox" value="1"/>
1501  
1502 =head2 radio_button
1503  
1504 $fb->radio_button($attribute, $value);
1505 $fb->radio_button($attribute, $value, \%options);
1506  
1507 Generate an HTML input type 'radio', typically part of a group including 2 or more controls.
1508 Generated value attributes uses $value, the control is marked 'checked' when $value matches
1509 the value of $attribute (or you can override, see below). \%options are HTML attributes which
1510 are passed to the tag builder unless special as described below.
1511  
1512 Special \%options:
1513  
1514 =over 4
1515  
1516 =item errors_classes
1517  
1518 A string that is appended to the C<class> attribute if the $attribute has errors (as defined by the model API)
1519  
1520 =item checked
1521  
1522 A boolean which determines if the input radio control is marked 'checked'. Used if you want to
1523 override the default.
1524  
1525 =back
1526  
1527 Examples:
1528  
1529 # Example radio group
1530  
1531 $person->type('admin');
1532  
1533 $fb->radio_button('type', 'admin');
1534 $fb->radio_button('type', 'user');
1535 $fb->radio_button('type', 'guest');
1536  
1537 #<input checked id="person_type_admin" name="person.type" type="radio" value="admin"/>
1538 #<input id="person_type_user" name="person.type" type="radio" value="user"/>
1539 #<input id="person_type_guest" name="person.type" type="radio" value="guest"/>
1540
1541 # Example \%options
1542  
1543 $fb->radio_button('type', 'guest', {class=>'foo', errors_classes=>'err'});
1544 # <input class="foo err" id="person_type_guest" name="person.type" type="radio" value="guest"/>
1545  
1546 $fb->radio_button('type', 'guest', {checked=>1});
1547 # <input checked id="person_type_guest" name="person.type" type="radio" value="guest"/>
1548  
1549 =head2 date_field
1550  
1551 $fb->date_field($attribute);
1552 $fb->date_field($attribute, \%options);
1553  
1554 Generates a 'type' C<date> HTML input control. Used when your $attribute value is a L<DateTime>
1555 object to get proper string formatting. Although the C<date> type is considered HTML5 you can
1556 use this for older HTML versions as well when you need to get the object formatting (you just don't
1557 get the date HTML controls).
1558  
1559 When the $attribute value is a L<DateTime> object (or actually is any object) we call '->ymd' to stringify
1560 the object to the expected format. '\%options' as in the input control are passed to the tag builder
1561 to create HTML attributes on the input tag with the exception of the specials ones already documented
1562 (such as C<errors_classes>) and the following special \%options
1563  
1564 =over 4
1565  
1566 =item min
1567  
1568 =item max
1569  
1570 When these are L<DateTime> objects we stringify using ->ymd to get the expected format; otherwise
1571 they are passed as is to the tag builder.
1572  
1573 =back
1574  
1575 Examples:
1576  
1577 $person->birthday(DateTime->new(year=>1969, month=>2, day=>13));
1578  
1579 $fb->date_field('birthday');
1580 # <input id="person_birthday" name="person.birthday" type="date" value="1969-02-13"/>
1581  
1582 $fb->date_field('birthday', {class=>'foo', errors_classes=>'err'});
1583 # <input class="foo err" id="person_birthday" name="person.birthday" type="date" value="1969-02-13"/>
1584  
1585 $fb->date_field('birthday', +{
1586 min => DateTime->new(year=>1900, month=>1, day=>1),
1587 max => DateTime->new(year=>2030, month=>1, day=>1),
1588 });
1589 #<input id="person_birthday" max="2030-01-01" min="1900-01-01" name="person.birthday" type="date" value="1969-02-13"/>
1590  
1591 =head2 datetime_local_field
1592  
1593 =head2 time_field
1594  
1595 Like L</date_field> but sets the input type to C<datetime-local> or C<time> respectively and formats any
1596 <DateTime> values with "->strftime('%Y-%m-%dT%T')" (for C<datetime-local>) or either "->strftime('%H:%M')"
1597 or "->strftime('%T.%3N')" for C<time> depending on the \%options C<include_seconds>, which defaults to true).
1598  
1599 Examples:
1600  
1601 $person->due(DateTime->new(year=>1969, month=>2, day=>13, hour=>10, minute=>45, second=>11, nanosecond=> 500000000));
1602  
1603 $fb->datetime_local_field('due');
1604 # <input id="person_due" name="person.due" type="datetime-local" value="1969-02-13T10:45:11"/>
1605  
1606 $fb->time_field('due');
1607 # <input id="person_due" name="person.due" type="time" value="10:45:11.500"/>
1608  
1609 $fb->time_field('due', +{include_seconds=>0});
1610 # <input id="person_due" name="person.due" type="time" value="10:45"/>
1611  
1612 =head2 submit
1613  
1614 $fb->submit;
1615 $fb->submit(\%options);
1616 $fb->submit($value);
1617 $fb->submit($value, \%options);
1618  
1619 Create an HTML submit C<input> tag with a meaningful default value based on the model name and its state
1620 in storage (if supported by the model). Will also look up the following two translation tag keys:
1621  
1622 "formbuilder.submit.@{[ $self->name ]}.${key}"
1623 "formbuilder.submit.${key}"
1624  
1625 Where $key is by default 'submit' and if the model supports 'in_storage' its either 'update' or 'create'
1626 depending on if the model is new or already existing.
1627  
1628 Examples:
1629  
1630 $fb->submit;
1631 # <input id="commit" name="commit" type="submit" value="Submit Person"/>
1632  
1633 $fb->submit('Login', {class=>'foo'});
1634 # <input class="foo" id="commit" name="commit" type="submit" value="Login"/>
1635  
1636 =head2 button
1637  
1638 $fb->button($name, \%attrs, \&block)
1639 $fb->button($name, \%attrs, $content)
1640 $fb->button($name, \&block)
1641 $fb->button($name, $content)
1642  
1643 Create a C<button> tag with custom attibutes and content. Content be be a string or a coderef if you
1644 need to do complex layout.
1645  
1646 Useful to create submit buttons with fancy formatting or when you need a button that submits in the
1647 form namespace.
1648  
1649 Examples:
1650  
1651 $person->type('admin');
1652  
1653 $fb->button('type');
1654 # <button id="person_type" name="person.type" type="submit" value="admin">Button</button>
1655  
1656 $fb->button('type', {class=>'foo'});
1657 # <button class="foo" id="person_type" name="person.type" type="submit" value="admin">Button</button>
1658  
1659 $fb->button('type', "Press Me")
1660 # <button id="person_type" name="person.type" type="submit" value="admin">Press Me</button>
1661  
1662 $fb->button('type', sub { "Press Me" })
1663 # <button id="person_type" name="person.type" type="submit" value="admin">Press Me</button>
1664  
1665 =head2 legend
1666  
1667 $fb->legend;
1668 $fb->legend(\%options);
1669 $fb->legend($content);
1670 $fb->legend($content, \%options);
1671 $fb->legend(\&template);
1672 $fb->legend(\%options, \&template);
1673  
1674 Create an HTML Form C<legend> element with default content that is based on the model name.
1675 Accepts \%options which are passed to the tag builder and used to create HTML element attributes.
1676 You can override the content with either a $content string or a \&template coderef (which will
1677 receive the default content translation as its first argument).
1678  
1679 The default content will be based on the model name and can be influenced by its storage status
1680 if C<in_storage> is supplied by the model. We attempt to lookup the content string via the
1681 following translation tags (if the body supports ->i18n):
1682  
1683 "formbuilder.legend.@{[ $self->name ]}.${key}"
1684 "formbuilder.legend.${key}"
1685  
1686 Where $key is 'new' if the model doesn't support C<in_storage> else it's either 'update' or 'create'
1687 based on if the current model is already in storage (update) or its new and needs to be created.
1688  
1689 Examples:
1690  
1691 $fb->legend;
1692 # <legend>New Person</legend>
1693  
1694 $fb->legend({class=>'foo'});
1695 # <legend class="foo">New Person</legend>
1696  
1697 $fb->legend("Person");
1698 # <legend>Person</legend>
1699  
1700 $fb->legend("Persons", {class=>'foo'});
1701 # <legend class="foo">Persons</legend>
1702  
1703 $fb->legend(sub { shift . " Info"});
1704 # <legend>New Person Info</legend>
1705  
1706 $fb->legend({class=>'foo'}, sub {"Person"});
1707 # <legend class="foo">Person</legend>
1708  
1709 =head2 legend_for
1710  
1711 $fb->legend_for($attr);
1712 $fb->legend_for($attr, \%options);
1713  
1714 Creates an HTML C<legend> tags with it's content set to the human translated name of the given
1715 $attribute. Allows you to pass some additional HTML attributes to the legend tag. Examples:
1716  
1717 $fb->legend_for('status')
1718 # <legend id="status_legend" >Status</legend>
1719
1720 $fb->legend_for('status', {class=>'foo'})
1721 # <legend id="status_legend" class="foo" >Status</legend>
1722  
1723 =head2 fields_for
1724  
1725 $fb->fields_for($attribute, sub {
1726 my ($nested_fb, $model) = @_;
1727 });
1728  
1729 $fb->fields_for($attribute, \%options, sub {
1730 my ($nested_fb, $model) = @_;
1731 });
1732  
1733 # With a 'finally' block when $attribute is a collection
1734  
1735 $fb->fields_for($attribute, sub {
1736 my ($nested_fb, $model) = @_;
1737 }, sub {
1738 my ($nested_fb, $new_model) = @_;
1739 });
1740  
1741  
1742 Used to create sub form builders under the current one for nested models (either a collection of models
1743 or a single model.) This sub form builder will be passed as the first argument to the enclosing subref
1744 and will encapsulate any indexing or namespacing; its model will be set to the sub model. You also get
1745 a second argument which is the sub model for ease of access. Note that if the $attribute refers to a collection
1746 then $model will be set to the current item model of that collection.
1747  
1748 When the $attribute refers to a collection the collection object must provide a C<next> method which should
1749 iterate thru the collection in the order desired and return C<undef> to indicate all records have been rolled
1750 thru. This collection object may also implement a C<reset> method to return the index to the start of the
1751 collection (which will be called after the final record is processed) and a C<build> method which should
1752 return a new empty record (required if you want a C<finally> block as described below).
1753  
1754 Please see L<Valiant::HTML::Util::Collection> for example. B<NOTE>: If you supply an arrayref instead of
1755 a collection object, we will build one using L<Valiant::HTML::Util::Collection> automatically. This behavior
1756 might change in the future so it would be ideal to not rely on it.
1757  
1758 If the $attribute is a collection you may optionally add a second coderef template which is called after
1759 the collect has been fully iterated thru and it recieves a sub formbuilder with a new blank model as an
1760 argument. This finally block is always called, even if the collection is empty so it can he used to
1761 generate a blank entry for adding new items to the collection (for example) or for any extra code or field
1762 controls that you want under the sub model namespace.
1763  
1764 Available \%options:
1765  
1766 =over 4
1767  
1768 =item builder
1769  
1770 The class name of the formbuilder. Defaults to L<Valiant::HTML::FormBuilder> or whatever the current
1771 builder is (if overridden in the parent).
1772  
1773 =item namespace
1774  
1775 The ID namespace. Will default to the parent formbuilder C<namespace> if there is one.
1776  
1777 =item child_index
1778  
1779 The index of the sub object. Can be a coderef. Used if you need explicit control over the index generated
1780  
1781 =item include_id
1782  
1783 Defaults to true. If the sub model does C<in_storage> and C<primary_columns> then add hidden form fields
1784 with those IDs to the sub model namespace. Often needed to properly match a record to its existing state
1785 in storage (such as a database). Not sure why you'd want to turn this off but the option is a carry over
1786 from Rails so I presume there is a use case.
1787  
1788 =item id
1789  
1790 Override the ID namespace for th sub model.
1791  
1792 =item index
1793  
1794 Explicitly override the index of the sub model.
1795  
1796 =back
1797  
1798 Example of an attribute that refers to a nested object.
1799  
1800 $person->profile(Local::Profile->new(zip=>'78621', address=>'ab'));
1801  
1802 $fb->fields_for('profile', sub {
1803 my $fb_profile = shift;
1804 return $fb_profile->input('address'),
1805 $fb_profile->errors_for('address'),
1806 $fb_profile->input('zip');
1807 });
1808  
1809 # <input id="person_profile_address" name="person.profile.address" type="text" value="ab"/>
1810 # <div>Address is too short (minimum is 3 characters)</div>
1811 # <input id="person_profile_zip" name="person.profile.zip" type="text" value="78621"/>
1812  
1813 Example of an attribute that refers to a nested collection object (and with a "finally block")
1814  
1815 $person->credit_cards([
1816 Local::CreditCard->new(number=>'234234223444', expiration=>DateTime->now->add(months=>11)),
1817 Local::CreditCard->new(number=>'342342342322', expiration=>DateTime->now->add(months=>11)),
1818 Local::CreditCard->new(number=>'111112222233', expiration=>DateTime->now->subtract(months=>11)), # An expired card
1819 ]);
1820  
1821 $fb->fields_for('credit_cards', sub {
1822 my $fb_cc = shift;
1823 return $fb_cc->input('number'),
1824 $fb_cc->date_field('expiration'),
1825 $fb_cc->errors_for('expiration');
1826 }, sub {
1827 my $fb_finally = shift;
1828 return $fb_finally->button('add', +{value=>1}, 'Add a New Credit Card');
1829 });
1830  
1831 # <input id="person_credit_cards_0_number" name="person.credit_cards[0].number" type="text" value="234234223444"/>
1832 # <input id="person_credit_cards_0_expiration" name="person.credit_cards[0].expiration" type="date" value="2023-01-23"/>
1833 # <input id="person_credit_cards_1_number" name="person.credit_cards[1].number" type="text" value="342342342322"/>
1834 # <input id="person_credit_cards_1_expiration" name="person.credit_cards[1].expiration" type="date" value="2023-01-23"/>
1835 # <input id="person_credit_cards_2_number" name="person.credit_cards[2].number" type="text" value="111112222233"/>
1836 # <input id="person_credit_cards_2_expiration" name="person.credit_cards[2].expiration" type="date" value="2021-03-23"/>
1837 # <div>Expiration chosen date can&#39;t be earlier than 2022-02-23</div>
1838 # <button id="person_credit_cards_3_add" name="person.credit_cards[3].add" type="submit" value="1">Add a New Credit Card</button>
1839  
1840 =head2 select
1841  
1842 $fb->select($attribute_proto, \@options, \%options)
1843 $fb->select($attribute_proto, \@options)
1844 $fb->select($attribute_proto, \%options, \&template)
1845 $fb->select($attribute_proto, \&template)
1846  
1847 Where C<$attribute_proto> is one of:
1848  
1849 $attribute # A scalar value which is an attribute on the underlying $model
1850 { $attribute => $method } # A hashref composed of an $attribute on the underlying $model
1851 # which returns a sub model or a collection of sub models
1852 # and a $method to be called on the value of that sub model (
1853 # or on each item sub model if the $attribute is a collection).
1854  
1855 Used to create a C<select> tag group with option tags. \@options can be anything that can be
1856 accepted by L<Valiant::HTML::FormTags/options_for_select>. The value(s) of $attribute_proto
1857 are automatically marked as C<selected>.
1858  
1859 Since this is built on top if C<select_tag> \%options can be anything supported by that
1860 method. See L<Valiant::HTML::FormTags/select_tag> for more. In addition we have the following
1861 special handling for \%options:
1862  
1863 =over 4
1864  
1865 =item selected
1866  
1867 =item disabled
1868  
1869 Mark C\<option> tags as selected or disabled. If you manual set selected then we ignore
1870 the value of $attribute (or @values when $attribute is a collection)
1871  
1872 =item unselected_value
1873  
1874 The value to set the hidden 'unselected' field to. No default value. See 'include_hidden' for details.
1875  
1876 =item include_hidden
1877  
1878 Defaults to true for 'multiple' and false for single type selects.
1879  
1880 The rules for an HTML form select field specify that if the no option is 'selected' then
1881 nothing is submitted. This can cause issues if you are expecting a submission that somehow indicates 'nothing selected'
1882 means to unset some settings. So we do one of two things when 'include_hidden' is true. When the select is a
1883 simple 'single value' select (not multiple) we add a hidden field with the same name as the select name but indexed to 0 so its
1884 always the first value; its value is whatever you set 'unselected_value' to. If you don't set 'unselected_value' this hidden
1885 field is NOT created. If you are using L<Plack::Request> or L<Mojolicious> (or using L<Catalyst> with C<use_hash_multivalue_in_request> option set to
1886 true, or something like L<Catalyst::TraitFor::Request::StructuredParameters>) then the last value of an array
1887 body parameter will be returned which will let you choose between a default value or an actual returned value. Example:
1888  
1889 # $fb->select('state_ids', [map { [$_->label, $_->id] } $roles_collection->all], +{include_hidden=>1, unselected_value=>-1} );
1890 # <input id="person_state_ids_hidden" name="person.state_ids[0]" type="hidden" value="-1"/>
1891 # <select id="person_state_ids" multiple name="person.state_ids[]">
1892 # <option selected value="1">user</option>
1893 # <option value="2">admin</option>
1894 # <option selected value="3">guest</option>
1895 # </select>
1896  
1897  
1898 If you've set the 'multiple' attribute to true, or we detect that multiple values are intended (either when the form value
1899 of the field is an arrayref or a collection) indicting your select drop list allows one to choose more than one option,
1900 we add a hidden field '_nop' at index 0 which you will need to treat at the signal for 'this means unset. If you are using this with
1901 L<DBIx:Class::Valiant> then that code will automatically handle this for you. Otherwise you'll need to handle it
1902 manually or add code to detect that there is no form submission value under that name. If you don't want this
1903 behavior you can manually turn it off be explicitly setting 'include_hidden' to false.
1904  
1905 =back
1906  
1907 Optionally you can provide a \&template which should return C<option> tags. This coderef
1908 will recieve the $model, $attribute and an array of @selected values based on the $attribute.
1909  
1910 Examples:
1911  
1912 $fb->select('state_id', [1,2,3], +{class=>'foo'} );
1913 # <select class="foo" id="person_state_id" name="person.state_id">
1914 # <option selected value="1">1</option>
1915 # <option value="2">2</option>
1916 # <option value="3">3</option>
1917 # </select>
1918  
1919 $fb->select('state_id', [1,2,3], +{selected=>[3], disabled=>[1]} );
1920 # <select id="person_state_id" name="person.state_id">
1921 # <option disabled value="1">1</option>
1922 # <option value="2">2</option>
1923 # <option selected value="3">3</option>
1924 # </select>
1925  
1926 $fb->select('state_id', [map { [$_->name, $_->id] } $states_collection->all], +{include_blank=>1} );
1927 # <select id="person_state_id" name="person.state_id">
1928 # <option label=" " value=""></option>
1929 # <option selected value="1">TX</option>
1930 # <option value="2">NY</option>
1931 # <option value="3">CA</option>
1932 # </select>
1933  
1934 $fb->select('state_id', sub {
1935 my ($model, $attribute, $value) = @_;
1936 return map {
1937 my $selected = $_->id eq $value ? 1:0;
1938 option_tag($_->name, +{class=>'foo', selected=>$selected, value=>$_->id});
1939 } $states_collection->all;
1940 });
1941 # <select id="person_state_id" name="person.state_id">
1942 # <option class="foo" selected value="1">TX</option>
1943 # <option class="foo" value="2">NY</option>
1944 # <option class="foo" value="3">CA</option>
1945 # </select>
1946  
1947 Examples when $attribute is a collection:
1948  
1949 $fb->select({roles => 'id'}, [map { [$_->label, $_->id] } $roles_collection->all]),
1950 # <input id="person_roles_id_hidden" name="person.roles[0]._nop" type="hidden" value="1"/>
1951 # <select id="person_roles_id" multiple name="person.roles[].id">
1952 # <option selected value="1">user</option>
1953 # <option selected value="2">admin</option>
1954 # <option value="3">guest</option>
1955 # </select>
1956  
1957 Please note when the $attribute is a collection we add a hidden field to cope with case when
1958 no items are selected, you'll need to write form processing code to mark and notice the
1959 C<_nop> field.
1960  
1961 =head2 collection_select
1962  
1963 $fb->collection_select($attribute_proto, $collection, $value_method, $text_method, \%options);
1964 $fb->collection_select($attribute_proto, $collection, $value_method, $text_method);
1965 $fb->collection_select($attribute_proto, $collection);
1966  
1967 Where C<$attribute_proto> is one of:
1968  
1969 $attribute # A string which is an attribute on the underlying $model
1970 # that returns a scalar value.
1971 { $attribute => $method } # A hashref composed of an $attribute on the underlying $model
1972 # which returns a sub model or a collection of sub models
1973 # and a $method to be called on the value of that sub model (
1974 # or on each item sub model if the $attribute is a collection).
1975  
1976 Similar to L</select> but works with a $collection instead of delineated options. The collection can be
1977 an actual collection object, or the string name of a method on the model which provides the actual
1978 collection objection. Its a type of shortcut to reduce boilerplate at the expense of some flexibility (
1979 if you need that you'll need to use L</select>). Examples:
1980  
1981 $fb->collection_select('state_id', $states_collection, id=>'name');
1982 # <select id="person.state_id" name="person.state_id">
1983 # <option selected value="1">TX</option>
1984 # <option value="2">NY</option>
1985 # <option value="3">CA</option>
1986 # </select>
1987  
1988 $fb->collection_select('state_id', $states_collection, id=>'name', {class=>'foo', include_blank=>1});
1989 # <select class="foo" id="person.state_id" name="person.state_id">
1990 # <option label=" " value=""></option>
1991 # <option selected value="1">TX</option>
1992 # <option value="2">NY</option>
1993 # <option value="3">CA</option>
1994 # </select>
1995  
1996 $fb->collection_select('state_id', $states_collection, id=>'name', {selected=>[3], disabled=>[1]});
1997 # <select id="person.state_id" name="person.state_id">
1998 # <option disabled value="1">TX</option>
1999 # <option value="2">NY</option>
2000 # <option selected value="3">CA</option>
2001 # </select>
2002  
2003 is $fb->collection_select({roles => 'id'}, $roles_collection, id=>'label');
2004 # <input id="person_roles_id_hidden" name="person.roles[0]._nop" type="hidden" value="1"/>
2005 # <select id="person_roles_id" multiple name="person.roles[].id">
2006 # <option selected value="1">user</option>
2007 # <option selected value="2">admin</option>
2008 # <option value="3">guest</option>
2009 # </select>
2010  
2011 Please note when the $attribute is a collection value we add a hidden field to allow you to send a signal
2012 to the form processor that this namespace contains no records. Otherwise the form will just send
2013 nothing. If you have a custom way to handle this you can disable the behavior if you wish by explicitly
2014 setting include_hidden=>0
2015  
2016 =head2 collection_checkbox
2017  
2018 $fb->collection_checkbox({$attribute=>$value_method}, $collection, $value_method, $text_method, \%options);
2019 $fb->collection_checkbox({$attribute=>$value_method}, $collection, $value_method, $text_method);
2020 $fb->collection_checkbox({$attribute=>$value_method}, $collection);
2021 $fb->collection_checkbox({$attribute=>$value_method}, $collection, $value_method, $text_method, \%options, \&template);
2022 $fb->collection_checkbox({$attribute=>$value_method}, $collection, $value_method, $text_method, \&template);
2023 $fb->collection_checkbox({$attribute=>$value_method}, $collection, \&template);
2024  
2025 Create a checkbox group for a collection attribute
2026  
2027 Examples:
2028  
2029 Where the $attribute C<roles> refers to a collection of sub models, each of which provides a method C<id>
2030 which is used to fetch a matching value and $roles_collection refers to the full set of available roles
2031 which can be added or removed from the parent model.
2032  
2033 In these examples C<$collection> and C<$roles_collection> can be either a collection object or a string
2034 which is the method name on the current model which provides the collection.
2035  
2036 $fb->collection_checkbox({roles => 'id'}, $roles_collection, id=>'label');
2037 # <input id="person_roles_0__nop" name="person.roles[0]._nop" type="hidden" value="1"/>
2038 # <label for="person_roles_1_id">user</label>
2039 # <input checked id="person_roles_1_id" name="person.roles[1].id" type="checkbox" value="1"/>
2040 # <label for="person_roles_2_id">admin</label>
2041 # <input checked id="person_roles_2_id" name="person.roles[2].id" type="checkbox" value="2"/>
2042 # <label for="person_roles_3_id">guest</label>
2043 # <input id="person_roles_3_id" name="person.roles[3].id" type="checkbox" value="3"/>
2044  
2045 Please note when the $attribute is a collection value we add a hidden field to allow you to send a signal
2046 to the form processor that this namespace contains no records. Otherwise the form will just send
2047 nothing. If you have a custom way to handle this you can disable the behavior if you wish by explicitly
2048 setting include_hidden=>0
2049  
2050 If you have special needs for formatting or layout you can override the default template with a coderef
2051 that will receive a special type of formbuilder localized to the current value (an instance of
2052 L<Valiant::HTML::FormBuilder::Checkbox>):
2053  
2054 $fb->collection_checkbox({roles => 'id'}, $roles_collection, id=>'label', sub {
2055 my $fb_roles = shift;
2056 return $fb_roles->checkbox({class=>'form-check-input'}),
2057 $fb_roles->label({class=>'form-check-label'});
2058 });
2059  
2060 # <input id="person_roles_4__nop" name="person.roles[4]._nop" type="hidden" value="1"/>
2061 # <input checked class="form-check-input" id="person_roles_5_id" name="person.roles[5].id" type="checkbox" value="1"/>
2062 # <label class="form-check-label" for="person_roles_5_id">user</label>
2063 # <input checked class="form-check-input" id="person_roles_6_id" name="person.roles[6].id" type="checkbox" value="2"/>
2064 # <label class="form-check-label" for="person_roles_6_id">admin</label>
2065 # <input class="form-check-input" id="person_roles_7_id" name="person.roles[7].id" type="checkbox" value="3"/>
2066 # <label class="form-check-label" for="person_roles_7_id">guest</label>
2067  
2068 In addition to overriding C<checkbox> and C<label> to already contain value and state (if its checked or
2069 not) information. This special builder contains some additional methods of possible use, you should see
2070 the documentation of L<Valiant::HTML::FormBuilder::Checkbox> for more.
2071  
2072 If provided C<%options> is a hashref of the following optional values
2073  
2074 =over 4
2075  
2076 =item include_hidden
2077  
2078 Defaults to whatever the method C<default_collection_checkbox_include_hidden> returns. In the core code
2079 the returns true. If true will include a hidden field set to the name of the collection, which is uses
2080 to indicate 'no checked values' since HTML will send nothing by default if there's no checked values. It
2081 will add this hidden field for each checkbox item to represent the 'off' value.
2082  
2083 =item builder
2084  
2085 Defaults to the values of C<DEFAULT_COLLECTION_CHECKBOX_BUILDER> method. In core code this is
2086 L<Valiant::HTML::FormBuilder::Checkbox>. Overide if you need to make a custom builder (tricky work).
2087  
2088 =back
2089  
2090 =head2 collection_radio_buttons
2091  
2092 $fb->collection_radio_buttons($attribute, $collection, $value_method, $text_method, \%options);
2093 $fb->collection_radio_buttons($attribute, $collection, $value_method, $text_method);
2094 $fb->collection_radio_buttons($attribute, $collection);
2095 $fb->collection_radio_buttons($attribute, $collection, $value_method, $text_method, \%options, \&template);
2096 $fb->collection_radio_buttons($attribute, $collection, $value_method, $text_method, \&template);
2097 $fb->collection_radio_buttons($attribute, $collection, \&template);
2098  
2099 A collection of radio buttons. Similar to L<\collection_checkbox> but used one only one value is
2100 permitted. Example:
2101  
2102 $fb->collection_radio_buttons('state_id', $states_collection, id=>'name');
2103 # <input id="person_state_id_hidden" name="person.state_id" type="hidden" value=""/>
2104 # <label for="person_state_id_1">TX</label>
2105 # <input checked id="person_state_id_1_1" name="person.state_id" type="radio" value="1"/>
2106 # <label for="person_state_id_2">NY</label>
2107 # <input id="person_state_id_2_2" name="person.state_id" type="radio" value="2"/>
2108 # <label for="person_state_id_3">CA</label>
2109 # <input id="person_state_id_3_3" name="person.state_id" type="radio" value="3"/>
2110  
2111 Please note when the $attribute is a collection value we add a hidden field to allow you to send a signal
2112 to the form processor that this namespace contains no records. Otherwise the form will just send
2113 nothing. If you have a custom way to handle this you can disable the behavior if you wish by explicitly
2114 setting include_hidden=>0
2115  
2116 If you have special needs for formatting or layout you can override the default template with a coderef
2117 that will receive a special type of formbuilder localized to the current value (an instance of
2118 L<Valiant::HTML::FormBuilder::RadioButton>):
2119  
2120 $fb->collection_radio_buttons('state_id', $states_collection, id=>'name', sub {
2121 my $fb_states = shift;
2122 return $fb_states->radio_button({class=>'form-check-input'}),
2123 $fb_states->label({class=>'form-check-label'});
2124 });
2125 # <div id='person_state_id'>
2126 # <input id="person_state_id_hidden" name="person.state_id" type="hidden" value=""/>
2127 # <input checked class="form-check-input" id="person_state_id_1_1" name="person.state_id" type="radio" value="1"/>
2128 # <label class="form-check-label" for="person_state_id_1">TX</label>
2129 # <input class="form-check-input" id="person_state_id_2_2" name="person.state_id" type="radio" value="2"/>
2130 # <label class="form-check-label" for="person_state_id_2">NY</label>
2131 # <input class="form-check-input" id="person_state_id_3_3" name="person.state_id" type="radio" value="3"/>
2132 # <label class="form-check-label" for="person_state_id_3">CA</label>
2133 # </div>
2134  
2135 In addition to overriding C<radio_button> and C<label> to already contain value and state (if its checked or
2136 not) information. This special builder contains some additional methods of possible use, you should see
2137 the documentation of L<Valiant::HTML::FormBuilder::RadioButton> for more.
2138  
2139 Please note that the generated radio inputs will be wrapped in a containing C<div> tag. You can change this tag
2140 using the C<container_tag> option. For example:
2141  
2142 $fb->collection_radio_buttons('state_id', $states_collection, id=>'name', +{container_tag=>'span'}, sub {
2143 my $fb_states = shift;
2144 return $fb_states->radio_button({class=>'form-check-input'}),
2145 $fb_states->label({class=>'form-check-label'});
2146 });
2147  
2148 Here's all the values for the '%options' argument. Any options that are not one of these will be passed to
2149 the container tag as html attributes:
2150  
2151 =over4
2152  
2153 =item checked_value
2154  
2155 This is the current value of the attribute. By default its the attribute value (via L<\tag_value_for_attribute>)
2156 but you can override as needed.
2157  
2158 =item include_hidden.
2159  
2160 Defaults to true. The value returned if you don't check one of the radio buttons.
2161  
2162 =item container_tag
2163  
2164 The tag that contains the generated radio buttons.
2165  
2166 =item builder
2167  
2168 The builder subclass used in the radio input generator.
2169  
2170 =back
2171  
2172 =head2 radio_buttons
2173  
2174 $fb->radio_buttons($attribute, \@options, \%options);
2175 $fb->radio_buttons($attribute, \@options, \%options, \&template);
2176 $fb->radio_buttons($attribute, \@options);
2177 $fb->radio_buttons($attribute, \@options, \&template);
2178  
2179 Similar to L</collection_radio_buttons> but takes an arrayref of label / values instead of a collection.
2180 Useful when you have a list of radio buttons (like from an ENUM) but you don't want to list each radio
2181 separately. Example:
2182  
2183 $fb_profile->radio_buttons('status', [[Pending=>'pending'],[Active=>'active'],[Inactive=>'inactive']]);
2184  
2185 # <input id="person_profile_status_hidden" name="person.profile.status" type="hidden" value="">
2186 # <input id="person_profile_status_pending_pending" name="person.profile.status" type="radio" value="pending">
2187 # <label for="person_profile_status_pending">Pending</label>
2188 # <input checked="" id="person_profile_status_active_actie" name="person.profile.status" type="radio" value="active">
2189 # <label for="person_profile_status_active">Active</label>
2190 # <input id="person_profile_status_inactive_inactive" name="person.profile.status" type="radio" value="inactive">
2191 # <label for="person_profile_status_inactive">Inactive</label>
2192  
2193 Supports using a template subroutine reference (like L</collection_radio_buttons>) when you need to be
2194 fussy about style and positioning.
2195  
2196 =head1 SEE ALSO
2197  
2198 L<Valiant>
2199  
2200 =head1 AUTHOR
2201
2202 See L<Valiant>
2203  
2204 =head1 COPYRIGHT & LICENSE
2205
2206 See L<Valiant>
2207  
2208 =cut
« Valiant/Validator/Numericality.pm ↑ Summary Valiant/Validates.pm »