Valiant/HTML/FormBuilder.pm

filter: Statementstmt 81.2% 594 / 731 Branchbran 64.6% 370 / 572 Conditioncond 58.2% 148 / 254 MC/DCmcdc 33.7% 49 / 145 Subroutinesub 79.3% 73 / 92 Podpod 60.0% 33 / 55 totaltotal 68.5% 1267 / 1849 CC 391 SCAR 38.0
CC
391
Cov
71%
radio_buttons
182.0
collection_checkbox
114.5
collection_select
105.9
CRAP 44.9
« Catalyst/View/Valiant/HTMLBuilder.pm ↑ Summary Valiant/Validator/Collection.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 # Non public helper methods
10  
11 sub set_unless_defined {
12 313 ▶ my ($key, $options, $value) = @_;
Subroutine set_unless_defined: called CC=3 SCAR=11.0
Pod undocumented
13 313 ▶ return if exists($options->{$key}) && defined($options->{$key});
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{$key} and defined $options->{$key} 93 220 313
Condition66%2/3
exists $options->{$key} and defined $options->{$key}
!ll&&!rl&&r
220093
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
14 220 $options->{$key} = $value;
15 }
16  
17 2 has model => ( is => 'ro', required => 1);
18 2 has name => ( is => 'ro', required => 1 );
19 0 ▶has options => ( is => 'ro', required => 1, default => sub { +{} } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
20 2 has index => ( is => 'ro', required => 0, predicate => 'has_index' );
21 2 has namespace => ( is => 'ro', required => 0, predicate => 'has_namespace' );
22 2 has tag_helpers => (
23 is => 'ro',
24 required => 1,
25 handles => [qw/view/],
26 builder => '_build_tag_helpers',
27 lazy => 1,
28 );
29  
30 sub _build_tag_helpers {
31 1 ▶ my ($self) = @_;
Subroutine _build_tag_helpers: called CC=2 SCAR=7.0
32 1 my %args = ();
33 $args{view} = exists($self->options->{view}) ?
34 $self->options->{view} :
35 1 ▶ Module::Runtime::use_module('Valiant::HTML::Util::View')->new;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->options->{'view'} ? : 0 1 1
36 1 return Module::Runtime::use_module('Valiant::HTML::Util::Form')->new(%args);
37 }
38  
39 2 has theme => ( is => 'ro', required => 1, lazy =>1, builder => '_build_theme' );
40  
41 sub _build_theme {
42 18 ▶ my ($self) = @_;
Subroutine _build_theme: called CC=3 SCAR=11.4
43 18 ▶ my $theme = $self->can('default_theme') ? $self->default_theme : +{};
Branch: 1 branch
BranchTrueFalseTotal
$self->can('default_theme') ? : 0 18 18
44 18 ▶ my $view_theme = $self->view->can('formbuilder_theme') ? $self->view->formbuilder_theme : +{};
Branch: 1 branch
BranchTrueFalseTotal
$self->view->can('formbuilder_theme') ? : 0 18 18
45 18 return +{ %$theme, %$view_theme };
46 }
47  
48 58 ▶has _nested_child_index => (is=>'rw', init_arg=>undef, required=>1, default=>sub { +{} });
Subroutine __ANON__: called CC=1 SCAR=0.0
49  
50 around BUILDARGS => sub {
51 58 ▶ my ($orig, $class, @args) = @_;
Subroutine __ANON__: called CC=4 SCAR=14.3
52 58 my $options = $class->$orig(@args);
53  
54 58 ▶ $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 58 58
Condition33%1/3
not exists $options->{"index"} and exists $options->{"child_index"}
!ll&&!rl&&r
0580
MC/DC0%0/2
not exists $options->{"index"} and exists $options->{"child_index"}
not exists $options->{"index"} exists $options->{"child_index"}
Truth table33%1/3
not(A) and B
A= not exists $options->{"index"}B= exists $options->{"child_index"}
ABresult
0X0
100
111
55 58 ▶ $options->{options} = exists($options->{options}) ? +{%$options, %{$options->{options}}} : $options;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'options'} ? : 57 1 58
56  
57 58 return $options;
58 2 };
59  
60 0 ▶sub form_action { shift->options->{html}{action} }
Subroutine form_action: not called CC=1 SCAR=6.9
Pod documented
61 0 ▶sub form_method { shift->options->{html}{method} }
Subroutine form_method: not called CC=1 SCAR=6.9
Pod documented
62 0 ▶sub form_enctype { shift->options->{html}{method} }
Subroutine form_enctype: not called CC=1 SCAR=6.9
Pod documented
63 0 ▶sub csrf_token { shift->options->{html}{data}{csrf_token} }
Subroutine csrf_token: not called CC=1 SCAR=6.9
Pod documented
64 0 ▶sub containing_view { shift->options->{view} }
Subroutine containing_view: not called CC=1 SCAR=6.9
Pod undocumented
65  
66 sub allow_method_names_outside_object {
67 189 ▶ my $self = shift;
Subroutine allow_method_names_outside_object: called CC=2 SCAR=6.9
Pod documented
68 return exists($self->options->{allow_method_names_outside_object})
69 ? $self->options->{allow_method_names_outside_object}
70 189 ▶ : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->options->{'allow_method_names_outside_object'} ? : 29 160 189
71 }
72  
73 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
74 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
75 5 ▶sub DEFAULT_COLLECTION_CHECKBOX_BUILDER { return 'Valiant::HTML::FormBuilder::Checkbox' }
Subroutine DEFAULT_COLLECTION_CHECKBOX_BUILDER: called CC=1 SCAR=0.0
Pod undocumented
76 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
77  
78 0 ▶sub id { return shift->options->{id} }
Subroutine id: not called CC=1 SCAR=6.9
Pod documented
79  
80 sub nested_child_index {
81 4 ▶ my ($self, $attribute) = @_;
Subroutine nested_child_index: called CC=2 SCAR=6.9
Pod undocumented
82 4 ▶ if(exists($self->_nested_child_index->{$attribute})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->_nested_child_index->{$attribute}) { } 3 1 4
83 3 return ++$self->_nested_child_index->{$attribute};
84 } else {
85 1 return $self->_nested_child_index->{$attribute} = 0
86 }
87 }
88  
89 sub tag_id_for_attribute {
90 228 ▶ my ($self, $attribute, @extra) = @_;
Subroutine tag_id_for_attribute: called CC=3 SCAR=11.0
Pod undocumented
91 228 my $opts = +{};
92 228 ▶ $opts->{namespace} = $self->namespace if $self->has_namespace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_namespace 1 227 228
93 228 ▶ $opts->{index} = $self->index if $self->has_index;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_index 0 228 228
94  
95 228 return $self->tag_helpers->field_id($self->name, $attribute, $opts, @extra);
96 }
97  
98 # $self->tag_name_for_attribute($attribute, +{ multiple=>1 });
99 sub tag_name_for_attribute {
100 125 ▶ my ($self, $attribute, $opts, @extra) = @_;
Subroutine tag_name_for_attribute: called CC=4 SCAR=13.9
Pod undocumented
101 125 ▶ $opts = +{} unless defined $opts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts 123 2 125
102 125 ▶ $opts->{namespace} = $self->namespace if $self->has_namespace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_namespace 1 124 125
103 125 ▶ $opts->{index} = $self->index if $self->has_index;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_index 0 125 125
104  
105 125 return $self->tag_helpers->field_name($self->name, $attribute, $opts, @extra);
106 }
107  
108 sub tag_value_for_attribute {
109 189 ▶ my ($self, $attribute, $options) = @_;
Subroutine tag_value_for_attribute: called CC=1 SCAR=0.0
Pod documented
110 my %options = (
111 strict => delete($options->{strict_method_check}),
112 189 no_strict_method_check => delete($options->{no_strict_method_check}),
113 allow_method_names_outside_object => $self->allow_method_names_outside_object,
114 );
115 189 return $self->tag_helpers->field_value($self->model, $attribute, \%options);
116 }
117  
118 sub tag_errors_for_attribute {
119 19 ▶ my ($self, $attribute) = @_;
Subroutine tag_errors_for_attribute: called CC=1 SCAR=0.0
Pod documented
120 19 return $self->tag_helpers->field_errors($self->model, $attribute);
121 }
122  
123 sub human_name_for_attribute {
124 45 ▶ my ($self, $attribute) = @_;
Subroutine human_name_for_attribute: called CC=2 SCAR=7.2
Pod undocumented
125 45 ▶ return $self->model->can('human_attribute_name') ?
Branch: 1 branch
BranchTrueFalseTotal
$self->model->can('human_attribute_name') ? : 45 0 45
126 $self->model->human_attribute_name($attribute) :
127 $self->tag_helpers->_humanize($attribute);
128 }
129  
130 sub human_name_for_label {
131 33 ▶ my ($self, $label) = @_;
Subroutine human_name_for_label: called CC=2 SCAR=7.2
Pod undocumented
132 33 ▶ return $self->model->can('human_label_name') ?
Branch: 1 branch
BranchTrueFalseTotal
$self->model->can('human_label_name') ? : 33 0 33
133 $self->model->human_label_name($label) :
134 $self->tag_helpers->_humanize($label);
135 }
136  
137 sub attribute_has_errors {
138 186 ▶ my ($self, $attribute) = @_;
Subroutine attribute_has_errors: called CC=3 SCAR=11.0
Pod undocumented
139 186 ▶ 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) ? : 103 83 186
Condition100%3/3
$self->model->can("errors") and $self->model->errors->where($attribute)
!ll&&!rl&&r
1073103
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
140 }
141  
142 # my $u = $fb->form_action->clone; $u->query_param_append('add_comment', 1);
143  
144 sub form_action_for {
145 0 ▶ my $self = shift;
Subroutine form_action_for: not called CC=5 SCAR=34.0
Pod documented
146 0 ▶ my $params = pop @_ if (ref($_[-1])||'') eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[-1] || "") eq "HASH" 0 0 0
Condition0%0/2
ref $_[-1] || ''
l!l
00
MC/DC0%0/1
ref $_[-1] || ''
ref $_[-1]
Truth table0%0/2
A
A= ref $_[-1]
Aresult
11
00
147 0 my @path_parts = @_;
148 0 my $url = $self->form_action->clone;
149 0 my @segments = $url->path_segments;
150  
151 0 ▶ $url->query_param_append(%$params) if $params;
Branch: 1 branch
BranchTrueFalseTotal
if $params 0 0 0
152 0 ▶ $url->path_segments(@segments, @path_parts) if @path_parts;
Branch: 1 branch
BranchTrueFalseTotal
if @path_parts 0 0 0
153  
154 0 return $url;
155 }
156  
157 # $fb->has_errors()
158 # $fb->model_errors($content)
159 # $fb->error_for_form(\&template)
160  
161 sub form_has_errors {
162 0 ▶ my ($self) = shift;
Subroutine form_has_errors: not called CC=7 SCAR=40.3
Pod documented
163 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
164 || $self->_get_model_errors; # There is at least one error on the model itself
165  
166 0 ▶ my $options = ref($_[0]) eq 'HASH' ? shift : +{};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
167 0 ▶ my $content = @_ ? shift : _t('invalid_form');
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
168  
169 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
170  
171 0 $options = $self->merge_theme_field_opts('model_errors', undef, $options);
172 0 ▶ $content = $self->model->i18n->translate(
Branch: 1 branch
BranchTrueFalseTotal
if $self->model->i18n->is_i18n_tag($content) 0 0 0
173 $content,
174 scope=>'valiant.html.errors.messages',
175 default=>[ _t("errors.messages.invalid_form"), _t("messages.invalid_form") ],
176 ) if $self->model->i18n->is_i18n_tag($content);
177  
178 0 return $self->tag_helpers->tags->div($options, $content);
179 }
180  
181 sub _default_form_has_errors_content {
182 0 ▶ my ($self) = shift;
Subroutine _default_form_has_errors_content: not called CC=1 SCAR=6.9
183 0 my $message = $self->_generate_default_model_error;
184 return sub {
185 0 ▶ my ($view, $self) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
186 0 return $self->tag_helpers->div($message);
187 }
188 0 }
189  
190 # Public methods for HTML generation
191  
192 # $fb->model_errors()
193 # $fb->model_errors(\%options)
194 # $fb->model_errors(\%options, \&template)
195 # $fb->model_errors(\&template)
196  
197 sub model_errors {
198 7 ▶ my ($self) = shift;
Subroutine model_errors: called CC=12 SCAR=25.0
Pod documented
199 7 my ($options, $content) = (+{}, undef);
200  
201 7 ▶ while(my $arg = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift()) 6 7 13
202 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
203 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
204 }
205 7 $options = $self->merge_theme_field_opts('model_errors', undef, $options);
206  
207 7 my @errors = $self->_get_model_errors;
208 7 my $show_message_on_field_errors = delete $options->{show_message_on_field_errors};
209  
210 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
211 $self->_model_has_errors && # We have errors
212 # !scalar(@errors) && # but no model errorsS_VIEW
213 ($show_message_on_field_errors) # And a default model error
214 ) {
215 2 unshift @errors, $self->_generate_default_model_error($show_message_on_field_errors);
216 }
217 7 ▶ return '' unless @errors;
Branch: 1 branch
BranchTrueFalseTotal
unless @errors 0 7 7
218  
219 7 ▶ my $max_errors = exists($options->{max_errors}) ? delete($options->{max_errors}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'max_errors'} ? : 2 5 7
220 7 ▶ @errors = @errors[0..($max_errors-1)] if($max_errors);
Branch: 1 branch
BranchTrueFalseTotal
if $max_errors 2 5 7
221 7 ▶ $content = $self->_default_model_errors_content($options) unless defined($content);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 6 1 7
222  
223 7 my $error_content = $content->(@errors);
224 7 return $error_content;
225 }
226  
227 sub _model_has_errors {
228 7 ▶ my ($self) = @_;
Subroutine _model_has_errors: called CC=2 SCAR=8.1
229 7 ▶ return $self->model->has_errors if $self->model->can('has_errors');
Branch: 1 branch
BranchTrueFalseTotal
if $self->model->can("has_errors") 7 0 7
230 0 return 0;
231 }
232 sub _get_model_errors {
233 7 ▶ my ($self) = @_;
Subroutine _get_model_errors: called CC=2 SCAR=8.1
234 7 ▶ return my @errors = $self->model->errors->model_messages if $self->model->can('errors');
Branch: 1 branch
BranchTrueFalseTotal
if $self->model->can("errors") 7 0 7
235 0 return ();
236 }
237  
238 sub _generate_default_model_error {
239 2 ▶ my ($self, $tag) = @_;
Subroutine _generate_default_model_error: called CC=4 SCAR=13.9
240 2 ▶ $tag = _t('invalid_form') if $tag eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq 1 1 1 2
241 2 ▶ return $tag unless ref $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tag 1 1 2
242 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
243 1 return $self->model->i18n->translate(
244 $tag,
245 scope=>'valiant.html.errors.messages',
246 default=>[ _t("errors.messages.$tag"), _t("messages.$tag") ],
247 );
248 }
249  
250 sub _default_model_errors_content {
251 6 ▶ my ($self, $options) = @_;
Subroutine _default_model_errors_content: called CC=1 SCAR=0.0
252 return sub {
253 6 ▶ my (@errors) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.0
254 6 ▶ if( scalar(@errors) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @errors == 1) { } 2 4 6
255 2 return $self->tag_helpers->content_tag('div', $errors[0], $options);
256 0 return $self->tag_helpers->content_tag('div', $errors[0], $options);
257 } else {
258 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
259 }
260 }
261 6 }
262  
263 sub merge_theme {
264 0 ▶ my ($self, %theme) = @_;
Subroutine merge_theme: not called CC=1 SCAR=6.9
Pod undocumented
265 0 $self->theme(+{ %{$self->theme}, %theme });
266 }
267  
268 sub merge_theme_field_opts {
269 253 ▶ my ($self, $tag_type, $attribute, $existing) = @_;
Subroutine merge_theme_field_opts: called CC=4 SCAR=16.8
Pod undocumented
270 253 my $theme = $self->theme;
271  
272 253 ▶ if(exists $theme->{$tag_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $theme->{$tag_type}) 0 253 253
273 0 $existing = +{ %{$theme->{$tag_type}}, %$existing };
274 }
275 253 ▶ if($attribute && exists $theme->{attributes}{$attribute}{$tag_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute and exists $theme->{"attributes"}{$attribute}{$tag_type}) 0 253 253
Condition66%2/3
$attribute and exists $theme->{"attributes"}{$attribute}{$tag_type}
!ll&&!rl&&r
522010
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
276 0 $existing = +{ %{$theme->{attributes}{$attribute}{$tag_type}}, %$existing };
277 }
278 253 return $existing;
279 }
280  
281 # $fb->label($attribute)
282 # $fb->label($attribute, \%options)
283 # $fb->label($attribute, $content)
284 # $fb->label($attribute, \%options, $content)
285 # $fb->label($attribute, \&content); sub content { my ($translated_attribute) = @_; ... }
286 # $fb->label($attribute, \%options, \&content); sub content { my ( $translated_attribute) = @_; ... }
287  
288 sub label {
289 45 ▶ my ($self, $attribute) = (shift, shift);
Subroutine label: called CC=10 SCAR=23.0
Pod documented
290 45 my ($options, $content) = (+{}, (my $translated_attribute = $self->human_name_for_attribute($attribute)));
291 45 ▶ while(my $arg = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift()) 61 45 106
292 61 ▶ $options = $arg if (ref($arg)||'') eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg || "") eq "HASH" 33 28 61
Condition100%2/2
ref $arg || ''
l!l
3526
MC/DC100%1/1
ref $arg || ''
ref $arg
Truth table100%2/2
A
A= ref $arg
Aresult
11
00
293 61 ▶ $content = $arg if (ref($arg)||'') eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg || "") eq "CODE" 2 59 61
Condition100%2/2
ref $arg || ''
l!l
3526
MC/DC100%1/1
ref $arg || ''
ref $arg
Truth table100%2/2
A
A= ref $arg
Aresult
11
00
294 61 ▶ $content = $arg if (ref(\$arg)||'') eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if (ref \$arg || "") eq "SCALAR" 26 35 61
Condition50%1/2
ref \$arg || ''
l!l
610
MC/DC0%0/1
ref \$arg || ''
ref \$arg
Truth table50%1/2
A
A= ref \$arg
Aresult
11
00
295 }
296  
297 45 set_unless_defined(for => $options, $self->tag_id_for_attribute($attribute));
298  
299 45 $options = $self->merge_theme_field_opts(label=>$attribute, $options);
300  
301 45 my $label ='';
302 45 ▶ if((ref($content)||'') eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $content || '') eq 'CODE') { } 2 43 45
Condition100%2/2
ref $content || ''
l!l
243
MC/DC100%1/1
ref $content || ''
ref $content
Truth table100%2/2
A
A= ref $content
Aresult
11
00
303 2 ▶ $label = $self->tag_helpers->label_tag($attribute, $self->process_options($attribute, $options), sub { $content->($translated_attribute) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
304 } else {
305 43 $label = $self->tag_helpers->label_tag($attribute, $content, $self->process_options($attribute, $options));
306 }
307 45 return $label;
308 }
309  
310 # $fb->errors_for($attribute)
311 # $fb->errors_for($attribute, \%options)
312 # $fb->errors_for($attribute, \%options, \&template)
313 # $fb->errors_for($attribute, \&template)
314  
315 sub errors_for {
316 19 ▶ my ($self, $attribute) = (shift, shift);
Subroutine errors_for: called CC=14 SCAR=26.4
Pod documented
317 19 my ($options, $content) = (+{}, undef);
318 19 ▶ while(my $arg = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift()) 6 19 25
319 6 ▶ $options = $arg if (ref($arg)||'') eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg || "") eq "HASH" 4 2 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
320 6 ▶ $content = $arg if (ref($arg)||'') eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg || "") eq "CODE" 2 4 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
321 }
322 19 $options = $self->merge_theme_field_opts(errors_for=>$attribute, $options);
323 19 ▶ my $show_empty = exists($options->{show_empty}) ? delete($options->{show_empty}) : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'show_empty'} ? : 1 18 19
324 19 ▶ if($show_empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($show_empty) 1 18 19
325 1 ▶ $options->{always_array} = 1 unless exists($options->{always_array});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"always_array"} 1 0 1
326 }
327  
328 19 my @errors = $self->tag_errors_for_attribute($attribute);
329 19 ▶ return '' if scalar(@errors) == 0 && !$show_empty;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @errors == 0 and not $show_empty 2 17 19
Condition100%3/3
scalar @errors == 0 and not $show_empty
!ll&&!rl&&r
1612
MC/DC100%2/2
scalar @errors == 0 and not $show_empty
scalar @errors == 0 not $show_empty
Truth table100%3/3
A and not(B)
A= scalar @errors == 0B= not $show_empty
ABresult
0X0
100
111
330
331 17 ▶ my $max_errors = exists($options->{max_errors}) ? delete($options->{max_errors}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'max_errors'} ? : 2 15 17
332 17 ▶ @errors = @errors[0..($max_errors-1)] if($max_errors);
Branch: 1 branch
BranchTrueFalseTotal
if $max_errors 2 15 17
333 17 $options = $self->process_options($attribute, $options);
334 17 ▶ $content = $self->_default_errors_for_content($attribute, $options) unless defined($content);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 15 2 17
335  
336 17 my $response = $self->view->safe('');
337 17 $response = $content->(@errors);# if @errors;
338  
339 17 return $response;
340 }
341  
342 sub _default_errors_for_content {
343 15 ▶ my ($self, $attribute, $options) = @_;
Subroutine _default_errors_for_content: called CC=2 SCAR=6.9
344 15 ▶ my $always_array = exists($options->{always_array}) ? delete($options->{always_array}) : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'always_array'} ? : 1 14 15
345 15 my $id = $self->tag_id_for_attribute($attribute) . '_errors';
346  
347 return sub {
348 15 ▶ my (@errors) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.1
349 15 my $response = '';
350 15 ▶ if( !$always_array && (scalar(@errors) < 2) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $always_array and scalar @errors < 2) { } 11 4 15
Condition100%3/3
not $always_array and scalar @errors < 2
!ll&&!rl&&r
1311
MC/DC100%2/2
not $always_array and scalar @errors < 2
not $always_array scalar @errors < 2
Truth table100%3/3
not(A) and B
A= not $always_arrayB= scalar @errors < 2
ABresult
0X0
100
111
351 11 $response = $self->tag_helpers->content_tag('div', $errors[0], {%$options, id=>$id, data=>{error_param=>1}});
352 } else {
353 4 ▶ if($always_array) {
Branch: 1 branch
BranchTrueFalseTotal
if ($always_array) 1 3 4
354 1 ▶ @errors = ('') unless @errors;
Branch: 1 branch
BranchTrueFalseTotal
unless @errors 1 0 1
355 }
356 4 my @li_content = map { $self->tag_helpers->content_tag('li', $_, { data=>{error_param=>1}}) } @errors;
357 4 $response = $self->tag_helpers->content_tag('ol', $self->view->safe_concat(@li_content), {%$options, id=>$id, data=>{error_list=>1}});
358 }
359 15 return $response;
360 }
361 15 }
362  
363 sub process_options {
364 186 ▶ my ($self, $attribute, $options) = @_;
Subroutine process_options: called CC=11 SCAR=34.4
Pod undocumented
365  
366 186 ▶ if($options->{data}{remote}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"data"}{"remote"}) 0 186 186
367 0 my $url = $self->form_action->clone;
368 my $replace = exists($options->{data}{replace})
369 ? $options->{data}{replace}
370 0 ▶ : '#'.$options->{id};
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'data'}{'replace'} ? : 0 0 0
371 0 $url->query_param(replace=>$replace);
372  
373 0 $options->{data}{url} = $url;
374 0 ▶ $options->{data}{method} ||= $self->form_method;
Condition0%0/3
$options->{"data"}{"method"} ||= $self->form_method
l!l&&r!l&&!r
000
MC/DC0%0/2
$options->{"data"}{"method"} ||= $self->form_method
$options->{"data"}{"method"} $self->form_method
Truth table0%0/3
A ||= B
A= $options->{"data"}{"method"}B= $self->form_method
ABresult
1X1
011
000
375 0 $options->{data}{params} = $self->view->uri_escape(csrf_token => $self->csrf_token);
376 }
377  
378 186 ▶ 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 185 186
Condition100%3/3
$self->attribute_has_errors($attribute) and my $errors_attrs = delete $options->{"errors_attrs"}
!ll&&!rl&&r
831021
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
379 1 foreach my $key(keys %$errors_attrs) {
380 1 ▶ if(exists $options->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{$key}) { } 1 0 1
381 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
382 0 $options->{$key} = +{ %{$options->{$key}}, %{$errors_attrs->{$key}} };
383 } elsif($key eq 'class') {
384 1 $options->{$key} .= " $errors_attrs->{$key}";
385 } else {
386 0 $options->{$key} .= $errors_attrs->{$key};
387 }
388 } else {
389 0 $options->{$key} = $errors_attrs->{$key};
390 }
391 }
392 }
393 186 return $options;
394 }
395  
396 # $fb->input($attribute, \%options)
397 # $fb->input($attribute)
398  
399 sub input {
400 81 ▶ my ($self, $attribute, $options) = (shift, shift, (@_ ? shift : +{}));
Subroutine input: called CC=5 SCAR=16.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 44 37 81
401 81 ▶ $options = $self->merge_theme_field_opts($options->{type} || 'input', $attribute, $options);
Condition100%2/2
$options->{'type'} || 'input'
l!l
4041
MC/DC100%1/1
$options->{'type'} || 'input'
$options->{'type'}
Truth table100%2/2
A
A= $options->{'type'}
Aresult
11
00
402  
403 81 my %flags = ();
404 81 ▶ $flags{force_validity} = delete $options->{force_validity} if exists $options->{force_validity};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"force_validity"} 0 81 81
405 81 ▶ $flags{errors_classes} = delete $options->{errors_classes} if exists $options->{errors_classes};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"errors_classes"} 8 73 81
406  
407 81 my $response = $self->_input($attribute, $options, \%flags);
408 81 return $response;
409 }
410  
411 sub _input {
412 81 ▶ my ($self, $attribute, $html_attrs, $flags) = @_;
Subroutine _input: called CC=8 SCAR=21.0
413 81 ▶ my $model = $self->model->can('to_model') ? $self->model->to_model : $self->model;
Branch: 1 branch
BranchTrueFalseTotal
$self->model->can('to_model') ? : 0 81 81
414 81 my $valid = 1;
415 81 ▶ if(exists($flags->{force_validity})) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $flags->{'force_validity'}) { } 0 81 81
elsif ($model->can('errors')) { } 72 9 81
416 0 $valid = $flags->{force_validity};
417 } elsif($model->can('errors')) {
418 72 ▶ $valid = $model->errors->where($attribute) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$model->errors->where($attribute) ? : 49 23 72
419 }
420  
421 16 $html_attrs->{class} = join(' ', (grep { defined $_ } $html_attrs->{class}, $flags->{errors_classes}))
422 81 ▶ if $flags->{errors_classes} && !$valid;
Branch: 1 branch
BranchTrueFalseTotal
if $flags->{"errors_classes"} and not $valid 8 73 81
Condition66%2/3
$flags->{"errors_classes"} and not $valid
!ll&&!rl&&r
7308
MC/DC50%1/2
$flags->{"errors_classes"} and not $valid
$flags->{"errors_classes"} not $valid
Truth table66%2/3
A and not(B)
A= $flags->{"errors_classes"}B= not $valid
ABresult
0X0
100
111
423  
424 81 set_unless_defined(type => $html_attrs, 'text');
425 81 set_unless_defined(id => $html_attrs, $self->tag_id_for_attribute($attribute));
426 81 set_unless_defined(name => $html_attrs, $self->tag_name_for_attribute($attribute));
427 81 ▶ $html_attrs->{value} = $self->tag_value_for_attribute($attribute) unless defined($html_attrs->{value});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $html_attrs->{"value"} 45 36 81
428 81 $html_attrs = $self->process_options($attribute, $html_attrs);
429  
430 81 return my $input_safe_string = $self->tag_helpers->input_tag($attribute, $html_attrs);
431 }
432  
433 sub password {
434 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
435 4 $options->{type} = 'password';
436 4 ▶ $options->{value} = '' unless exists($options->{value});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"value"} 4 0 4
437 4 return $self->input($attribute, $options);
438 }
439  
440 sub hidden {
441 13 ▶ my ($self, $attribute, $options) = (shift, shift, (@_ ? shift : +{}));
Subroutine hidden: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 12 1 13
442 13 $options->{type} = 'hidden';
443 13 return $self->input($attribute, $options);
444 }
445  
446 sub text_area {
447 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
448 3 $options = $self->merge_theme_field_opts('text_area', $attribute, $options);
449 3 ▶ my $errors_classes = exists($options->{errors_classes}) ? delete($options->{errors_classes}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'errors_classes'} ? : 1 2 3
450
451 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
452 if $errors_classes && $self->model->can('errors') && $self->model->errors->where($attribute);
453  
454 3 set_unless_defined(id => $options, $self->tag_id_for_attribute($attribute));
455 3 return $self->tag_helpers->text_area_tag(
456 $self->tag_name_for_attribute($attribute),
457 $self->tag_value_for_attribute($attribute),
458 $self->process_options($attribute, $options),
459 );
460 }
461  
462 sub checkbox {
463 22 ▶ my ($self, $attribute) = (shift, shift);
Subroutine checkbox: called CC=15 SCAR=27.1
Pod documented
464 22 ▶ my $options = (ref($_[0])||'') eq 'HASH' ? shift(@_) : +{};
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[0] || '') eq 'HASH' ? : 20 2 22
Condition100%2/2
ref $_[0] || ''
l!l
202
MC/DC100%1/1
ref $_[0] || ''
ref $_[0]
Truth table100%2/2
A
A= ref $_[0]
Aresult
11
00
465 22 $options = $self->merge_theme_field_opts('checkbox', $attribute, $options);
466  
467 22 ▶ my $checked_value = @_ ? shift : 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 16 6 22
468 22 ▶ my $unchecked_value = @_ ? shift : 0;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 21 22
469 22 ▶ my $errors_classes = exists($options->{errors_classes}) ? delete($options->{errors_classes}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'errors_classes'} ? : 1 21 22
470 22 ▶ my $show_hidden_unchecked = exists($options->{include_hidden}) ? delete($options->{include_hidden}) : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'include_hidden'} ? : 19 3 22
471 22 my $name = $self->tag_name_for_attribute($attribute);
472  
473 22 my $checked = 0;
474 22 ▶ if(exists($options->{checked})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{'checked'}) { } 16 6 22
475 16 $checked = delete $options->{checked};
476 } else {
477 6 ▶ $checked = $self->tag_value_for_attribute($attribute) ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$self->tag_value_for_attribute($attribute) ? : 1 5 6
478 }
479  
480 22 $options->{type} = 'checkbox';
481 22 ▶ $options->{value} = $checked_value unless exists($options->{value});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"value"} 22 0 22
482 22 ▶ $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 21 22
Condition66%4/6
$errors_classes and $self->model->can("errors") and $self->model->errors->where($attribute)
!ll&&!rl&&r
2101
$errors_classes and $self->model->can("errors")
!ll&&!rl&&r
2101
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
483 if $errors_classes && $self->model->can('errors') && $self->model->errors->where($attribute);
484  
485 22 set_unless_defined(id => $options, $self->tag_id_for_attribute($attribute));
486  
487 22 my $checkbox = $self->tag_helpers->checkbox_tag(
488 $name,
489 $checked_value,
490 $checked,
491 $self->process_options($attribute, $options),
492 );
493  
494 22 ▶ if($show_hidden_unchecked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($show_hidden_unchecked) 3 19 22
495 3 ▶ my $hidden_name = exists($options->{name}) ? $options->{name} : $name;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'name'} ? : 0 3 3
496 3 my $hidden_checkbox = $self->tag_helpers->tag('input', +{type=>'hidden', name=>$hidden_name, value=>$unchecked_value});
497 3 $checkbox = $self->tag_helpers->join_tags($hidden_checkbox, $checkbox);
498 }
499  
500 22 return $checkbox;
501 }
502  
503 #radio_button(object_name, method, tag_value, options = {})
504 sub radio_button {
505 14 ▶ my ($self, $attribute) = (shift, shift);
Subroutine radio_button: called CC=8 SCAR=20.8
Pod documented
506 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
507 14 $options = $self->merge_theme_field_opts('radio_button', $attribute, $options);
508  
509 14 ▶ my $value = @_ ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 14 0 14
510  
511 14 $options->{type} = 'radio';
512 14 ▶ $options->{value} = $value unless exists($options->{value});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"value"} 14 0 14
513 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
514 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
515  
516 14 return $self->input($attribute, $self->process_options($attribute, $options));
517 }
518
519 sub date_field {
520 6 ▶ my ($self, $attribute, $options) = (@_, +{});
Subroutine date_field: called CC=7 SCAR=20.2
Pod documented
521 6 my $value = $self->tag_value_for_attribute($attribute);
522 6 $options = $self->merge_theme_field_opts('date_field', $attribute, $options);
523 6 $options->{type} = 'date';
524 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
525 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
526 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
527  
528 6 return $self->input($attribute, $options);
529 }
530  
531 sub datetime_local_field {
532 1 ▶ my ($self, $attribute, $options) = (@_, +{});
Subroutine datetime_local_field: called CC=7 SCAR=23.0
Pod documented
533 1 my $value = $self->tag_value_for_attribute($attribute);
534 1 $options = $self->merge_theme_field_opts('datetime_local_field', $attribute, $options);
535  
536 1 $options->{type} = 'datetime-local';
537 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
538 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
539 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
540  
541 1 return $self->input($attribute, $options);
542 }
543  
544 sub time_field {
545 2 ▶ my ($self, $attribute, $options) = (@_, +{});
Subroutine time_field: called CC=5 SCAR=16.7
Pod documented
546 2 my $value = $self->tag_value_for_attribute($attribute);
547 2 $options = $self->merge_theme_field_opts('time_field', $attribute, $options);
548 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
549  
550 2 $options->{type} = 'time';
551 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
552  
553 2 return $self->input($attribute, $options);
554 }
555  
556 sub submit {
557 2 ▶ my ($self) = shift;
Subroutine submit: called CC=4 SCAR=13.9
Pod documented
558 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
559 2 ▶ my $value = @_ ? shift(@_) : $self->_submit_default_value;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 1 2
560 2 $options = $self->merge_theme_field_opts('submit', undef, $options);
561  
562 2 return $self->tag_helpers->submit_tag($value, $options);
563 }
564  
565 sub _submit_default_value {
566 1 ▶ my $self = shift;
Subroutine _submit_default_value: called CC=4 SCAR=14.4
567 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
568 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
569 1 my $model_placeholder = $self->tag_helpers->_humanize($self->name);
570  
571 1 my @defaults = ();
572  
573 1 push @defaults, _t "formbuilder.submit.@{[ $self->name ]}.${key}";
574 1 push @defaults, _t "formbuilder.submit.${key}";
575 1 push @defaults, "@{[ $self->tag_helpers->_humanize($key) ]} ${model_placeholder}";
576  
577 1 return $self->model->i18n->translate(
578 shift(@defaults),
579 model=>$model_placeholder,
580 default=>\@defaults,
581 );
582 }
583  
584 # ->button($name, \%attrs, \&block)
585 # ->button($name, \%attrs, $content)
586 # ->button($name, \&block)
587 # ->button($name, $content)
588  
589 sub button {
590 5 ▶ my $self = shift;
Subroutine button: called CC=8 SCAR=20.9
Pod documented
591 5 my $attribute = shift;
592 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
593 5 my $content = shift;
594  
595 5 ▶ $attrs->{type} = 'submit' unless exists($attrs->{type});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attrs->{"type"} 5 0 5
596 5 ▶ $attrs->{value} = $self->tag_value_for_attribute($attribute, $attrs) unless exists($attrs->{value});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attrs->{"value"} 4 1 5
597 5 ▶ $attrs->{name} = $self->tag_name_for_attribute($attribute) unless exists($attrs->{name});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attrs->{"name"} 5 0 5
598 5 ▶ $attrs->{id} = $self->tag_id_for_attribute($attribute) unless exists($attrs->{id});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attrs->{"id"} 5 0 5
599 5 $attrs = $self->merge_theme_field_opts('button', $attribute, $attrs);
600  
601 5 ▶ return ref($content) ?
Branch: 1 branch
BranchTrueFalseTotal
ref $content ? : 1 4 5
602 $self->tag_helpers->button_tag($attrs, $content) :
603 $self->tag_helpers->button_tag($content, $self->process_options($attribute, $attrs));
604 }
605  
606 sub legend {
607 6 ▶ my ($self) = shift;
Subroutine legend: called CC=8 SCAR=20.8
Pod documented
608 6 my $value = my $default_value = $self->_legend_default_value;
609 6 my $options = +{};
610  
611 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
612 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
613 6 ▶ $value = shift(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 4 6
614 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
615  
616 6 return $self->tag_helpers->legend_tag($value, $options);
617 }
618  
619 sub legend_for {
620 0 ▶ my $self = shift;
Subroutine legend_for: not called CC=5 SCAR=34.0
Pod documented
621 0 my $attribute = shift;
622 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
623 0 ▶ my $content = @_ ? shift(@_) : $self->human_name_for_attribute($attribute);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
624  
625 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
626 0 $attrs = $self->merge_theme_field_opts('legend_for', $attribute, $attrs);
627 0 return $self->tag_helpers->legend_tag($content, $attrs);
628 }
629  
630 sub _legend_default_value {
631 6 ▶ my $self = shift;
Subroutine _legend_default_value: called CC=5 SCAR=17.2
632 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
633 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
634 6 my $model_placeholder = $self->tag_helpers->_humanize($self->name);
635  
636 6 my @defaults = ();
637  
638 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
639  
640 6 push @defaults, _t "formbuilder.legend.@{[ $self->name ]}.${key}";
641 6 push @defaults, _t "formbuilder.legend.${key}";
642 6 push @defaults, "@{[ $self->tag_helpers->_humanize($key) ]} ${model_placeholder}";
643  
644 6 return $self->model->i18n->translate(
645 shift(@defaults),
646 model=>$model_placeholder,
647 default=>\@defaults,
648 );
649 }
650  
651 # fields_for($related_attribute, ?\%options?, \&block)
652 sub fields_for {
653 2 ▶ my ($self, $related_attribute) = (shift, shift);
Subroutine fields_for: called CC=32 SCAR=42.6
Pod documented
654 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
655 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
656 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
657  
658 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
659 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
660 2 ▶ $options->{namespace} = $self->namespace if $self->has_namespace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_namespace 0 2 2
661 2 $options->{parent_builder} = $self;
662  
663 2 my $related_record;
664 2 ▶ if(Scalar::Util::blessed($related_attribute)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($related_attribute)) { } 0 2 2
665 0 $related_record = $related_attribute;
666 0 $related_attribute = $related_record->model_name->param_key;
667 } else {
668 2 $related_record = $self->tag_value_for_attribute($related_attribute);
669 }
670  
671 my $builder_name = exists($options->{scope})
672 ? delete($options->{scope})
673 2 ▶ : $self->name;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'scope'} ? : 0 2 2
674 my $attribute_name = exists($options->{as})
675 ? delete($options->{as})
676 2 ▶ : $related_attribute;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'as'} ? : 0 2 2
677 2 my $name = "${builder_name}.${attribute_name}";
678  
679 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
680  
681 # Coerce an array into a collection. Not sure if we want this here or not TBH...
682 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
683 if (ref($related_record)||'') eq 'ARRAY';
684  
685 # Ok is the related record a collection or something else.
686 2 ▶ if($related_record->can('next')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($related_record->can('next')) { } 1 1 2
687 1 my @output = ();
688 1 ▶ my $explicit_child_index = exists($options->{child_index}) ? $options->{child_index} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'child_index'} ? : 0 1 1
689  
690 1 ▶ while(my $child_model = $related_record->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $child_model = $related_record->next) 3 1 4
691 3 ▶ if(defined($explicit_child_index)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $explicit_child_index) { } 0 3 3
692 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
693 } else {
694 3 $options->{child_index} = $self->nested_child_index($related_attribute);
695 }
696 3 my $nested = $self->fields_for_nested_model("${name}[@{[ $options->{child_index} ]}]", $child_model, $options, $codeblock);
697 3 push @output, $nested;
698 }
699 1 ▶ $related_record->reset if $related_record->can('reset');
Branch: 1 branch
BranchTrueFalseTotal
if $related_record->can("reset") 1 0 1
700 1 ▶ if($finally_block) {
Branch: 1 branch
BranchTrueFalseTotal
if ($finally_block) 1 0 1
701 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
702 1 ▶ if(defined($explicit_child_index)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $explicit_child_index) { } 0 1 1
703 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
704 } else {
705 1 $options->{child_index} = $self->nested_child_index($related_attribute);
706 }
707 1 my $finally_content = $self->fields_for_nested_model("${name}[@{[ $options->{child_index} ]}]", $finally_model, $options, $finally_block);
708 1 push @output, $finally_content;
709 }
710 1 return $self->tag_helpers->join_tags(@output);
711 } else {
712 1 return $self->fields_for_nested_model($name, $related_record, $options, $codeblock);
713 }
714 }
715  
716 sub fields_for_nested_model {
717 5 ▶ my ($self, $name, $model, $options, $codeblock) = @_;
Subroutine fields_for_nested_model: called CC=5 SCAR=19.3
Pod undocumented
718 5 my $emit_hidden_id = 0;
719 5 ▶ $model = $model->to_model if $model->can('to_model');
Branch: 1 branch
BranchTrueFalseTotal
if $model->can("to_model") 0 5 5
720  
721 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
722 0 ▶ $emit_hidden_id = exists($options->{include_id}) ? $options->{include_id} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'include_id'} ? : 0 0 0
723 }
724  
725 return $self->tag_helpers->fields_for($name, $model, $options, sub {
726 5 ▶ my $view = shift;
Subroutine __ANON__: called CC=3 SCAR=12.3
727 5 my $fb = shift;
728 5 my @output = $codeblock->($view, $fb, $model);
729 5 ▶ if(@output && $emit_hidden_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (@output and $emit_hidden_id) 0 5 5
Condition33%1/3
@output and $emit_hidden_id
!ll&&!rl&&r
050
MC/DC0%0/2
@output and $emit_hidden_id
@output $emit_hidden_id
Truth table33%1/3
A and B
A= @outputB= $emit_hidden_id
ABresult
0X0
100
111
730 0 push @output, $fb->emit_hidden_ids;
731 }
732 5 return $self->tag_helpers->join_tags(@output);
733 5 });
734 }
735  
736 ## TODO this seems very much tied to DBIx::Class
737 sub emit_hidden_ids {
738 0 ▶ my ($self) = @_;
Subroutine emit_hidden_ids: not called CC=4 SCAR=30.0
Pod documented
739 0 ▶ my $model = $self->model->can('to_model') ? $self->model->to_model : $self->model;
Branch: 1 branch
BranchTrueFalseTotal
$self->model->can('to_model') ? : 0 0 0
740
741 0 ▶ return '' unless $model->can('primary_columns');
Branch: 1 branch
BranchTrueFalseTotal
unless $model->can("primary_columns") 0 0 0
742  
743 0 my @output = ();
744 0 foreach my $id_field ($model->primary_columns) {
745 0 push @output, $self->hidden($id_field);
746 }
747 0 return $self->tag_helpers->join_tags(@output);
748 }
749  
750 sub select {
751 8 ▶ my ($self, $attribute_proto) = (shift, shift);
Subroutine select: called CC=44 SCAR=41.2
Pod documented
752 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
753 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
754 8 $options = $self->merge_theme_field_opts('select', undef, $options);
755  
756 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
757 8 ▶ my $errors_classes = exists($options->{errors_classes}) ? delete($options->{errors_classes}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'errors_classes'} ? : 0 8 8
758 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
759 if $errors_classes && $self->model->can('errors') && $self->model->errors->where($attribute);
760  
761 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
762 8 ▶ my $include_hidden = exists($options->{include_hidden}) ? $options->{include_hidden} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'include_hidden'} ? : 0 8 8
763 8 ▶ my $unselected_default = exists($options->{unselected_value}) ? delete($options->{unselected_value}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'unselected_value'} ? : 1 7 8
764  
765  
766 8 my @selected = ();
767 8 my $name = '';
768 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
769 1 ▶ $options->{multiple} = 1 unless exists($options->{multiple});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"multiple"} 1 0 1
770 1 $options->{include_hidden} = 0; # Avoid adding two
771 1 my ($bridge, $value_method) = %$attribute_proto;
772 1 my $collection = $model->$bridge;
773 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
774 if (ref($collection)||'') eq 'ARRAY';
775  
776 1 ▶ while(my $item = $collection->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = $collection->next) 2 1 3
777 2 push @selected, $item->$value_method;
778 }
779 1 $name = $self->tag_name_for_attribute($bridge, +{multiple=>1}) . ".$value_method";
780 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
781 } else {
782 7 my $tag_value_proto = $self->tag_value_for_attribute($attribute_proto);
783 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
784 1 @selected = @$tag_value_proto;
785 1 $options->{multiple} = 1;
786 1 $include_hidden = 1;
787 } else {
788 6 @selected = ($tag_value_proto);
789 }
790 7 $name = $self->tag_name_for_attribute($attribute_proto);
791 7 $options->{id} = $self->tag_id_for_attribute($attribute_proto);
792 }
793  
794 8 my $options_tags = '';
795 8 ▶ if(!$block) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $block) { } 6 2 8
796 6 ▶ my $option_tags_proto = @_ ? shift : ();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 6 0 6
797  
798 # if $option_tags_proto is a string then we assume it is a method name on the model
799 6 ▶ if((ref(\$option_tags_proto)||'') eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref \$option_tags_proto || "") eq "SCALAR") 0 6 6
Condition50%1/2
ref \$option_tags_proto || ''
l!l
60
MC/DC0%0/1
ref \$option_tags_proto || ''
ref \$option_tags_proto
Truth table50%1/2
A
A= ref \$option_tags_proto
Aresult
11
00
800 0 $option_tags_proto = $model->$option_tags_proto;
801 }
802 # if $option_tags_proto is an array ref, where each element is a string
803 # then we assume it is a list of values to use for the options
804 6 ▶ if((ref($option_tags_proto)||'') eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $option_tags_proto || "") eq "ARRAY") 6 0 6
Condition50%1/2
ref $option_tags_proto || ''
l!l
60
MC/DC0%0/1
ref $option_tags_proto || ''
ref $option_tags_proto
Truth table50%1/2
A
A= ref $option_tags_proto
Aresult
11
00
805 $option_tags_proto = [
806 map {
807 6 ▶ ((ref(\$_)||"") eq 'SCALAR')
Branch: 1 branch
BranchTrueFalseTotal
(ref \$_ || '') eq 'SCALAR' ? : 9 9 18
Condition50%1/2
ref \$_ || ''
l!l
180
MC/DC0%0/1
ref \$_ || ''
ref \$_
Truth table50%1/2
A
A= ref \$_
Aresult
11
00
808 ? [$self->human_name_for_label($_), $_]
809 : $_;
810 } @$option_tags_proto
811 ];
812 }
813  
814 6 ▶ my @disabled = ( @{delete($options->{disabled})||[]});
Branch: 1 branch
BranchTrueFalseTotal
delete $options->{"disabled"} or [] 5 1 6
815 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
816  
817 6 ▶ my @default_selected = @{ delete($options->{default_selected})||[]} if exists($options->{default_selected});
Branch: 2 branches
BranchTrueFalseTotal
if exists $options->{"default_selected"} 0 6 6
delete $options->{"default_selected"} or [] 0 0 0
818 6 ▶ @selected = @default_selected if @default_selected && !@selected;
Branch: 1 branch
BranchTrueFalseTotal
if @default_selected and not @selected 0 6 6
Condition33%1/3
@default_selected and not @selected
!ll&&!rl&&r
600
MC/DC0%0/2
@default_selected and not @selected
@default_selected not @selected
Truth table33%1/3
A and not(B)
A= @default_selectedB= not @selected
ABresult
0X0
100
111
819  
820 6 $options_tags = $self->tag_helpers->options_for_select($option_tags_proto, +{
821 selected => \@selected,
822 disabled => \@disabled,
823 });
824 } else {
825 2 $options_tags = $self->tag_helpers->join_tags($block->($model, $attribute_proto, @selected));
826 }
827  
828 8 ▶ $options->{include_hidden} = 0 if $options->{multiple};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"multiple"} 3 5 8
829 8 my $select_tag = $self->tag_helpers->select_tag($name, $options_tags, $options);
830 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
831 3 ▶ if(ref($attribute_proto)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $attribute_proto) { } 1 2 3
elsif (defined $unselected_default) { } 1 1 2
832 1 my ($bridge, $value_method) = %$attribute_proto;
833 1 my $hidden = $self->hidden("${bridge}[0]._nop", +{value=>1, id=>$options->{id}.'_hidden'});
834 1 $select_tag = $self->tag_helpers->join_tags($hidden, $select_tag);
835 } elsif(defined $unselected_default) {
836 1 my $hidden = $self->hidden("${attribute_proto}[0]", +{value=>$unselected_default, id=>$options->{id}.'_hidden'});
837 1 $select_tag = $self->tag_helpers->join_tags($hidden, $select_tag);
838 }
839 }
840 8 return $select_tag;
841 }
842  
843 #collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
844 sub collection_select {
845 6 ▶ my ($self, $method_proto) = (shift, shift);
Subroutine collection_select: called CC=41 SCAR=46.6
Pod documented
846 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
847 6 ▶ my $options = (ref($_[-1])||'') eq 'HASH' ? pop(@_) : +{};
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[-1] || '') eq 'HASH' ? : 4 2 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
848 6 $options = $self->merge_theme_field_opts('collection_select', undef, $options);
849  
850 6 my ($collection, $value_method, $label_method) = @_;
851  
852 # If the collection of options is not provided then we need to figure out what it is
853 6 ▶ if(!defined $collection) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $collection) 0 6 6
854 0 my ($options_model, $options_method) = ($model, $method_proto);
855 0 ▶ if(ref $method_proto) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $method_proto) 0 0 0
856 0 my $bridge; ($bridge, $options_method) = %$method_proto;
857 0 ▶ if($model->can('related_model')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($model->can('related_model')) { } 0 0 0
858 0 $options_model = $model->related_model($bridge);
859 } else {
860 0 my $local_model = $model->$bridge;
861 0 ▶ if($local_model->can('next')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($local_model->can("next")) 0 0 0
862 0 $local_model = $local_model->next;
863 }
864 0 $options_model = $local_model;
865 }
866 }
867 0 ($collection, $label_method, $value_method) = $options_model->select_options_for($options_method, %$options);
868 }
869 6 ▶ $collection = $model->$collection if (ref(\$collection)||'') eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if (ref \$collection || "") eq "SCALAR" 0 6 6
Condition50%1/2
ref \$collection || ''
l!l
60
MC/DC0%0/1
ref \$collection || ''
ref \$collection
Truth table50%1/2
A
A= ref \$collection
Aresult
11
00
870 6 ▶ $value_method = 'value' unless defined($value_method);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value_method 0 6 6
871 6 ▶ $label_method = 'label' unless defined($label_method);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $label_method 0 6 6
872  
873 6 ▶ $collection = $model->$collection if (ref(\$collection)||'') eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if (ref \$collection || "") eq "SCALAR" 0 6 6
Condition50%1/2
ref \$collection || ''
l!l
60
MC/DC0%0/1
ref \$collection || ''
ref \$collection
Truth table50%1/2
A
A= ref \$collection
Aresult
11
00
874  
875 6 ▶ my $include_hidden = exists($options->{include_hidden}) ? delete($options->{include_hidden}) : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'include_hidden'} ? : 0 6 6
876 6 my (@selected, $name, $id) = ();
877 6 ▶ if(ref $method_proto) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $method_proto) { } 1 5 6
878 1 ▶ $options->{multiple} = 1 unless exists($options->{multiple});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"multiple"} 1 0 1
879 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
880 1 my ($bridge, $value_method) = %$method_proto;
881 1 my $collection = $model->$bridge;
882 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
883 if (ref($collection)||'') eq 'ARRAY';
884  
885 1 ▶ while(my $item = $collection->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = $collection->next) 2 1 3
886 2 push @selected, $item->$value_method;
887 }
888 1 $name = $self->tag_name_for_attribute($bridge, +{multiple=>1}) . ".$value_method";
889 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
890 } else {
891 5 my $value = $self->tag_value_for_attribute($method_proto);
892 5 ▶ my $errors_classes = exists($options->{errors_classes}) ? delete($options->{errors_classes}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'errors_classes'} ? : 0 5 5
893 5 ▶ $options->{class} = join(' ', (grep { defined $_ } $options->{class}, $errors_classes))
Branch: 1 branch
BranchTrueFalseTotal
if $model->can("errors") and $model->errors->where($method_proto) 0 5 5
Condition33%1/3
$model->can("errors") and $model->errors->where($method_proto)
!ll&&!rl&&r
050
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
894 if $model->can('errors') && $model->errors->where($method_proto);
895  
896 5 ▶ if((ref($value)||'') eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ((ref $value || '') eq 'ARRAY') { } 0 5 5
elsif (defined $value) { } 4 1 5
Condition50%1/2
ref $value || ''
l!l
05
MC/DC0%0/1
ref $value || ''
ref $value
Truth table50%1/2
A
A= ref $value
Aresult
11
00
897 0 @selected = @$value;
898 0 ▶ $options->{multiple} = 1 unless exists($options->{multiple});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"multiple"} 0 0 0
899 } elsif(defined($value)) {
900 4 @selected = ($value);
901 } else {
902 1 @selected = ();
903 }
904  
905 5 $name = $self->tag_name_for_attribute($method_proto);
906 5 $options->{id} = $id = $self->tag_id_for_attribute($method_proto);
907 }
908  
909 6 ▶ $collection = $self->tag_helpers->array_to_collection(@$collection)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $collection || "") eq "ARRAY" 0 6 6
Condition50%1/2
ref $collection || ''
l!l
60
MC/DC0%0/1
ref $collection || ''
ref $collection
Truth table50%1/2
A
A= ref $collection
Aresult
11
00
910 if (ref($collection)||'') eq 'ARRAY';
911  
912 6 ▶ my @disabled = ( @{delete($options->{disabled})||[]});
Branch: 1 branch
BranchTrueFalseTotal
delete $options->{"disabled"} or [] 5 1 6
913 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
914  
915 6 ▶ my @default_selected = @{ delete($options->{default_selected})||[]} if exists($options->{default_selected});
Branch: 2 branches
BranchTrueFalseTotal
if exists $options->{"default_selected"} 2 4 6
delete $options->{"default_selected"} or [] 0 2 2
916 6 ▶ @selected = @default_selected if @default_selected && !@selected;
Branch: 1 branch
BranchTrueFalseTotal
if @default_selected and not @selected 1 5 6
Condition100%3/3
@default_selected and not @selected
!ll&&!rl&&r
411
MC/DC100%2/2
@default_selected and not @selected
@default_selected not @selected
Truth table100%3/3
A and not(B)
A= @default_selectedB= not @selected
ABresult
0X0
100
111
917  
918 6 my $select_tag = $self->tag_helpers->select_tag(
919 $name,
920 $self->tag_helpers->options_from_collection_for_select($collection, $value_method, $label_method, +{
921 selected => \@selected,
922 disabled => \@disabled,
923 }),
924 $options);
925  
926 6 ▶ if($include_hidden && ref($method_proto)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($include_hidden and ref $method_proto) 1 5 6
Condition66%2/3
$include_hidden and ref $method_proto
!ll&&!rl&&r
051
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
927 1 my ($bridge, $value_method) = %$method_proto;
928 1 my $hidden = $self->hidden("${bridge}[0]._nop", +{value=>1, id=>$options->{id}.'_hidden'});
929 1 $select_tag = $self->tag_helpers->join_tags($hidden, $select_tag);
930 }
931  
932 6 return $select_tag;
933 }
934  
935 4 ▶sub default_collection_checkbox_include_hidden { return 1 }
Subroutine default_collection_checkbox_include_hidden: called CC=1 SCAR=0.0
Pod undocumented
936  
937 # $fb->collection_checkbox({person_roles => role_id}, $roles_rs, $value_method, $text_method, \%options, \&block);
938 # $fb->collection_checkbox({person_roles => role_id}, $roles_rs, $value_method, $text_method, \%options, \&block);
939 sub collection_checkbox {
940 5 ▶ my ($self, $attribute_spec) = (shift, shift);
Subroutine collection_checkbox: called CC=46 SCAR=47.4
Pod documented
941 5 ▶ my $codeblock = (ref($_[-1])||'') eq 'CODE' ? pop(@_) : undef;
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[-1] || '') eq 'CODE' ? : 1 4 5
Condition100%2/2
ref $_[-1] || ''
l!l
23
MC/DC100%1/1
ref $_[-1] || ''
ref $_[-1]
Truth table100%2/2
A
A= ref $_[-1]
Aresult
11
00
942 5 ▶ my $options = (ref($_[-1])||'') eq 'HASH' ? pop(@_) : +{};
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[-1] || '') eq 'HASH' ? : 1 4 5
Condition100%2/2
ref $_[-1] || ''
l!l
14
MC/DC100%1/1
ref $_[-1] || ''
ref $_[-1]
Truth table100%2/2
A
A= ref $_[-1]
Aresult
11
00
943 5 $options = $self->merge_theme_field_opts('collection_checkbox', undef, $options);
944 5 ▶ my $model = $self->model->can('to_model') ? $self->model->to_model : $self->model;
Branch: 1 branch
BranchTrueFalseTotal
$self->model->can('to_model') ? : 0 5 5
945  
946 5 my ($collection, $value_method, $label_method) = @_;
947 5 ▶ if(!defined $collection) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $collection) 0 5 5
948 0 ▶ if(ref $attribute_spec) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attribute_spec) { } 0 0 0
949 0 my ($bridge, $method) = %$attribute_spec;
950 0 ▶ if($model->can('related_model')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($model->can('related_model')) { } 0 0 0
951 0 ($collection, $label_method, $value_method) = $model->related_model($bridge)->checkbox_rs_for($method, %$options);
952 } else {
953 0 my $local_model = $model->$bridge;
954 0 ▶ if($local_model->can('next')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($local_model->can("next")) 0 0 0
955 0 $local_model = $local_model->next;
956 }
957 0 ($collection, $label_method, $value_method) = $local_model->checkbox_rs_for($method, %$options);
958 }
959 } else {
960 0 ($collection, $label_method, $value_method) = $model->checkbox_rs_for($attribute_spec, %$options);
961 }
962 }
963  
964 5 ▶ $collection = $model->$collection if (ref(\$collection)||'') eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if (ref \$collection || "") eq "SCALAR" 0 5 5
Condition50%1/2
ref \$collection || ''
l!l
50
MC/DC0%0/1
ref \$collection || ''
ref \$collection
Truth table50%1/2
A
A= ref \$collection
Aresult
11
00
965 5 ▶ $value_method = 'value' unless defined($value_method);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value_method 0 5 5
966 5 ▶ $label_method = 'label' unless defined($label_method);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $label_method 0 5 5
967  
968 5 ▶ 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 4 5
969 5 ▶ my $container_tag = exists($options->{container_tag}) ? delete($options->{container_tag}) : 'div';
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'container_tag'} ? : 0 5 5
970  
971 # It's either +{ person_roles => role_id } or roles
972 5 my ($attribute, $attribute_value_method, $is_spec_attribute) = ();
973 5 ▶ if( (ref($attribute_spec)||'') eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $attribute_spec || '') eq 'HASH') { } 4 1 5
Condition100%2/2
ref $attribute_spec || ''
l!l
41
MC/DC100%1/1
ref $attribute_spec || ''
ref $attribute_spec
Truth table100%2/2
A
A= ref $attribute_spec
Aresult
11
00
974 4 ($attribute, $attribute_value_method) = (%{ $attribute_spec });
975 4 $is_spec_attribute = 1;
976 } else {
977 1 $attribute = $attribute_spec;
978 1 $attribute_value_method = $value_method;
979 }
980  
981 5 ▶ $codeblock = $self->_default_collection_checkbox_content unless defined($codeblock);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $codeblock 4 1 5
982  
983 5 my @checked_values = ();
984 5 my $value_collection = $self->tag_value_for_attribute($attribute);
985 5 ▶ $value_collection = $self->tag_helpers->array_to_collection(map { Scalar::Util::blessed($_) && $_->can('to_model') ? $_->to_model : $_ } @$value_collection)
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value_collection || "") eq "ARRAY" 5 0 5
builtin::blessed($_) && $_->can('to_model') ? : 0 10 10
Condition60%3/5
ref $value_collection || ''
l!l
50
builtin::blessed($_) and $_->can("to_model")
!ll&&!rl&&r
280
MC/DC0%0/1
ref $value_collection || ''
ref $value_collection
MC/DC0%0/2
builtin::blessed($_) and $_->can("to_model")
builtin::blessed($_) $_->can("to_model")
Truth table50%1/2
A
A= ref $value_collection
Aresult
11
00
Truth table66%2/3
A and B
A= builtin::blessed($_)B= $_->can("to_model")
ABresult
0X0
100
111
986 if (ref($value_collection)||'') eq 'ARRAY';
987  
988 5 ▶ while(my $value_model = $value_collection->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $value_model = $value_collection->next) 10 5 15
989 10 ▶ if($value_model->can($attribute_value_method)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value_model->can($attribute_value_method)) { } 8 2 10
elsif ($value_model->isa('Valiant::HTML::Util::Collection::Item') and $value_model->can('value')) { } 2 0 2
990 8 ▶ push @checked_values, $value_model->$attribute_value_method
Branch: 1 branch
BranchTrueFalseTotal
unless $value_model->can("is_marked_for_deletion") and $value_model->is_marked_for_deletion 8 0 8
Condition33%1/3
$value_model->can("is_marked_for_deletion") and $value_model->is_marked_for_deletion
!ll&&!rl&&r
800
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
991 unless $value_model->can('is_marked_for_deletion') && $value_model->is_marked_for_deletion;
992 ▶ } elsif($value_model->isa('Valiant::HTML::Util::Collection::Item') && $value_model->can('value')) {
Condition33%1/3
$value_model->isa("Valiant::HTML::Util::Collection::Item") and $value_model->can("value")
!ll&&!rl&&r
002
MC/DC0%0/2
$value_model->isa("Valiant::HTML::Util::Collection::Item") and $value_model->can("value")
$value_model->isa("Valiant::HTML::Util::Collection::Item") $value_model->can("value")
Truth table33%1/3
A and B
A= $value_model->isa("Valiant::HTML::Util::Collection::Item")B= $value_model->can("value")
ABresult
0X0
100
111
993 2 ▶ push @checked_values, $value_model->value
Branch: 1 branch
BranchTrueFalseTotal
unless $value_model->can("is_marked_for_deletion") and $value_model->is_marked_for_deletion 2 0 2
Condition33%1/3
$value_model->can("is_marked_for_deletion") and $value_model->is_marked_for_deletion
!ll&&!rl&&r
200
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
994 unless $value_model->can('is_marked_for_deletion') && $value_model->is_marked_for_deletion;
995 }else {
996 0 warn "Can't find value for " . ref($value_model) . " for $attribute";
997 }
998 }
999  
1000 5 my @checkboxes = ();
1001 my $checkbox_builder_options = +{
1002 5 ▶ builder => (exists($options->{builder}) ? delete($options->{builder}) : $self->DEFAULT_COLLECTION_CHECKBOX_BUILDER),
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'builder'} ? : 0 5 5
1003 value_method => $value_method,
1004 label_method => $label_method,
1005 attribute_value_method => $attribute_value_method,
1006 parent_builder => $self,
1007 attribute => $attribute,
1008 tag_helpers => $self->tag_helpers,
1009 is_spec_attribute => $is_spec_attribute,
1010 errors => [$model->errors->where($attribute)],
1011 };
1012 5 ▶ $checkbox_builder_options->{namespace} = $self->namespace if $self->has_namespace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_namespace 0 5 5
1013  
1014 5 ▶ $collection = $model->$collection if (ref(\$collection)||'') eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if (ref \$collection || "") eq "SCALAR" 0 5 5
Condition50%1/2
ref \$collection || ''
l!l
50
MC/DC0%0/1
ref \$collection || ''
ref \$collection
Truth table50%1/2
A
A= ref \$collection
Aresult
11
00
1015 5 ▶ $collection = $self->tag_helpers->array_to_collection(@$collection)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $collection || "") eq "ARRAY" 0 5 5
Condition50%1/2
ref $collection || ''
l!l
50
MC/DC0%0/1
ref $collection || ''
ref $collection
Truth table50%1/2
A
A= ref $collection
Aresult
11
00
1016 if (ref($collection)||'') eq 'ARRAY';
1017  
1018  
1019 5 ▶ while (my $checkbox_model = $collection->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $checkbox_model = $collection->next) 15 5 20
1020 #my $index = $self->nested_child_index($attribute);
1021 15 my $name = "@{[ $self->name ]}.${attribute}";
1022 my $checked = grep {
1023 15 ▶ my $current_value = $checkbox_model->can('read_attribute_for_html') ?
Branch: 1 branch
BranchTrueFalseTotal
$checkbox_model->can('read_attribute_for_html') ? : 0 30 30
1024 $checkbox_model->read_attribute_for_html($value_method)
1025 : $checkbox_model->$value_method;
1026 30 $_ eq $current_value;
1027 } @checked_values;
1028  
1029 15 ▶ if($include_hidden && !scalar(@checkboxes)) { # Add nop as first to handle empty list
Branch: 1 branch
BranchTrueFalseTotal
if ($include_hidden and not scalar @checkboxes) 4 11 15
Condition100%3/3
$include_hidden and not scalar @checkboxes
!ll&&!rl&&r
384
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
1030 4 my $hidden_fb = $self->tag_helpers->_instantiate_builder($name, $value_collection->build, {%$checkbox_builder_options});
1031 4 my $hidden_value = $self->_collection_checkbox_hidden_value($attribute, $is_spec_attribute, $options);
1032 4 push @checkboxes, $hidden_fb->hidden($name, +{name=>$name, id=>$self->tag_id_for_attribute($attribute).'_hidden', value=>$hidden_value});
1033 }
1034  
1035 #$checkbox_builder_options->{index} = $index;
1036 15 $checkbox_builder_options->{checked} = $checked;
1037 15 $checkbox_builder_options->{parent_builder} = $self;
1038 15 my $checkbox_fb = $self->tag_helpers->_instantiate_builder($name, $checkbox_model, $checkbox_builder_options);
1039 15 push @checkboxes, $codeblock->($checkbox_fb);
1040 }
1041 5 ▶ $collection->reset if $collection->can('reset');
Branch: 1 branch
BranchTrueFalseTotal
if $collection->can("reset") 5 0 5
1042 5 my $checkbox_content = $self->view->safe_concat(@checkboxes);
1043  
1044 5 ▶ my $errors_classes = exists($options->{errors_classes}) ? delete($options->{errors_classes}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'errors_classes'} ? : 0 5 5
1045 5 ▶ $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 5 5
Condition33%2/6
$errors_classes and $model->can("errors") and $model->errors->where($attribute)
!ll&&!rl&&r
500
$errors_classes and $model->can("errors")
!ll&&!rl&&r
500
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
1046 if $errors_classes && $model->can('errors') && $model->errors->where($attribute);
1047  
1048 5 return $self->tag_helpers->content_tag($container_tag, $checkbox_content, +{
1049 id => $self->tag_id_for_attribute($attribute),
1050 %$options,
1051 });
1052 }
1053  
1054 sub _collection_checkbox_hidden_value {
1055 4 ▶ my ($self, $attribute, $is_spec_attribute, $options) = @_;
Subroutine _collection_checkbox_hidden_value: called CC=2 SCAR=6.9
1056 4 ▶ my $value = $is_spec_attribute ? '{"_nop":""}' : '';
Branch: 1 branch
BranchTrueFalseTotal
$is_spec_attribute ? : 3 1 4
1057 4 return $value;
1058 }
1059  
1060 sub _default_collection_checkbox_content {
1061 4 ▶ my ($self) = @_;
Subroutine _default_collection_checkbox_content: called CC=1 SCAR=0.0
1062 return sub {
1063 12 ▶ my ($fb) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
1064 12 my $label = $fb->label;
1065 12 my $checkbox = $fb->checkbox;
1066 12 return $self->tag_helpers->join_tags($label, $checkbox);
1067 4 };
1068 }
1069  
1070 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
1071  
1072 sub collection_radio_buttons {
1073 3 ▶ my ($self, $attribute) = (shift, shift);
Subroutine collection_radio_buttons: called CC=31 SCAR=41.8
Pod documented
1074 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
1075 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
1076 3 $options = $self->merge_theme_field_opts('collection_radio_buttons', $attribute, $options);
1077
1078 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
1079  
1080 3 my ($collection, $value_method, $label_method) = @_;
1081 3 ▶ if(!defined $collection) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $collection) 0 3 3
1082 0 ▶ if(ref $attribute) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attribute) { } 0 0 0
1083 0 my ($bridge, $method) = %$attribute;
1084 0 ▶ if($model->can('related_model')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($model->can('related_model')) { } 0 0 0
1085 0 ($collection, $label_method, $value_method) = $model->related_model($bridge)->radio_button_rs_for($method, %$options);
1086 } else {
1087 0 my $local_model = $model->$bridge;
1088 0 ▶ if($local_model->can('next')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($local_model->can("next")) 0 0 0
1089 0 $local_model = $local_model->next;
1090 }
1091 0 ($collection, $label_method, $value_method) = $local_model->radio_button_rs_for($method, %$options);
1092 }
1093 } else {
1094 0 ($collection, $label_method, $value_method) = $model->radio_button_rs_for($attribute, %$options);
1095 }
1096 }
1097 3 ▶ $value_method = 'value' unless defined($value_method);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value_method 0 3 3
1098 3 ▶ $label_method = 'label' unless defined($label_method);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $label_method 0 3 3
1099  
1100 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
1101 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
1102 3 ▶ my $container_tag = exists($options->{container_tag}) ? delete($options->{container_tag}) : 'div';
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'container_tag'} ? : 0 3 3
1103  
1104 3 ▶ $codeblock = $self->_default_collection_radio_buttons_content unless defined($codeblock);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $codeblock 2 1 3
1105  
1106 3 my @radio_buttons = ();
1107 my $radio_buttons_builder_options = +{
1108 3 ▶ builder => (exists($options->{builder}) ? delete($options->{builder}) : $self->DEFAULT_COLLECTION_RADIO_BUTTON_BUILDER),
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'builder'} ? : 0 3 3
1109 value_method => $value_method,
1110 label_method => $label_method,
1111 checked_value => $checked_value,
1112 parent_builder => $self,
1113 attribute => $attribute,
1114 tag_helpers => $self->tag_helpers,
1115 errors => [$model->errors->where($attribute)],
1116 };
1117 3 ▶ $radio_buttons_builder_options->{namespace} = $self->namespace if $self->has_namespace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_namespace 0 3 3
1118  
1119 3 ▶ $collection = $model->$collection if (ref(\$collection)||'') eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if (ref \$collection || "") eq "SCALAR" 0 3 3
Condition50%1/2
ref \$collection || ''
l!l
30
MC/DC0%0/1
ref \$collection || ''
ref \$collection
Truth table50%1/2
A
A= ref \$collection
Aresult
11
00
1120 3 ▶ while (my $radio_button_model = $collection->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $radio_button_model = $collection->next) 9 3 12
1121 9 my $name = "@{[ $self->name ]}.${attribute}";
1122 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
1123 $radio_button_model->read_attribute_for_html($value_method) :
1124 $radio_button_model->$value_method;
1125 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
1126  
1127 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
1128 2 my $hidden_fb = $self->tag_helpers->_instantiate_builder($name, $model, $radio_buttons_builder_options);
1129 2 push @radio_buttons, $hidden_fb->hidden($name, +{name=>$name, id=>$self->tag_id_for_attribute($attribute).'_hidden', value=>''});
1130 }
1131  
1132 9 $radio_buttons_builder_options->{checked} = $checked;
1133  
1134 9 my $radio_button_fb = $self->tag_helpers->_instantiate_builder($name, $radio_button_model, $radio_buttons_builder_options);
1135 9 push @radio_buttons, $codeblock->($radio_button_fb);
1136 }
1137 3 ▶ $collection->reset if $collection->can('reset');
Branch: 1 branch
BranchTrueFalseTotal
if $collection->can("reset") 3 0 3
1138 3 my $radios = $self->view->safe_concat(@radio_buttons);
1139  
1140 3 ▶ my $errors_classes = exists($options->{errors_classes}) ? delete($options->{errors_classes}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'errors_classes'} ? : 0 3 3
1141 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
1142 if $errors_classes && $model->can('errors') && $model->errors->where($attribute);
1143  
1144 3 return $self->tag_helpers->content_tag($container_tag, $radios, +{
1145 id => $self->tag_id_for_attribute($attribute),
1146 %$options,
1147 });
1148 }
1149  
1150 sub _default_collection_radio_buttons_content {
1151 2 ▶ my ($self) = @_;
Subroutine _default_collection_radio_buttons_content: called CC=1 SCAR=0.0
1152 return sub {
1153 6 ▶ my ($fb) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
1154 6 my $label = $fb->label();
1155 6 my $checkbox = $fb->radio_button();
1156 6 return $self->tag_helpers->join_tags($label, $checkbox);
1157 2 };
1158 }
1159  
1160 sub radio_buttons {
1161 0 ▶ my ($self, $attribute) = (shift, shift);
Subroutine radio_buttons: not called CC=13 SCAR=52.0
Pod documented
1162 0 ▶ my $model = $self->model->can('to_model') ? $self->model->to_model : $self->model;
Branch: 1 branch
BranchTrueFalseTotal
$self->model->can('to_model') ? : 0 0 0
1163 0 ▶ my $codeblock = (ref($_[-1])||'') eq 'CODE' ? pop(@_) : undef;
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[-1] || '') eq 'CODE' ? : 0 0 0
Condition0%0/2
ref $_[-1] || ''
l!l
00
MC/DC0%0/1
ref $_[-1] || ''
ref $_[-1]
Truth table0%0/2
A
A= ref $_[-1]
Aresult
11
00
1164 0 ▶ my $options = (ref($_[-1])||'') eq 'HASH' ? pop(@_) : +{};
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[-1] || '') eq 'HASH' ? : 0 0 0
Condition0%0/2
ref $_[-1] || ''
l!l
00
MC/DC0%0/1
ref $_[-1] || ''
ref $_[-1]
Truth table0%0/2
A
A= ref $_[-1]
Aresult
11
00
1165 0 $options = $self->merge_theme_field_opts('radio_buttons', $attribute, $options);
1166  
1167 # If args then its either a string to the model method or an arrayref of option
1168 0 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
1169 0 my ($collection, $value_method, $label_method) = @_;
1170 0 ▶ $collection = do { my $method = shift; $model->$method } if (ref(\$_[0])||'') eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if (ref \$_[0] || "") eq "SCALAR" 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
1171 0 ▶ $collection = $self->tag_helpers->array_to_collection(@{shift(@_)}) if (ref($_[0])||'') eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] || "") eq "ARRAY" 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
1172 0 ▶ $value_method = 'value' unless defined($value_method);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value_method 0 0 0
1173 0 ▶ $label_method = 'label' unless defined($label_method);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $label_method 0 0 0
1174 0 return $self->collection_radio_buttons($attribute, $collection, $value_method, $label_method, $options, $codeblock);
1175 } else {
1176 # If we are here that means we are using the model method to get the collection
1177 0 my @buttons = $model->radio_buttons_for($attribute, %$options);
1178 0 my $collection = $self->tag_helpers->array_to_collection(@buttons);
1179 0 my $value_method = 'value';
1180 0 my $label_method = 'label';
1181 0 return $self->collection_radio_buttons($attribute, $collection, $value_method, $label_method, $options, $codeblock);
1182 }
1183 }
1184  
1185 sub field {
1186 7 ▶ my ($self, $attribute, $cb) = (shift, shift, shift);
Subroutine field: called CC=4 SCAR=13.9
Pod undocumented
1187 7 my $proxy = Valiant::HTML::FormBuilder::Proxy->new(
1188 form_builder => $self,
1189 attribute => $attribute,
1190 );
1191 7 ▶ return $proxy unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 4 3 7
1192  
1193 my @return = map {
1194 3 ▶ Scalar::Util::blessed($_) && $_->can('to_safe_string')
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) && $_->can('to_safe_string') ? : 3 2 5
Condition100%3/3
builtin::blessed($_) and $_->can("to_safe_string")
!ll&&!rl&&r
113
MC/DC100%2/2
builtin::blessed($_) and $_->can("to_safe_string")
builtin::blessed($_) $_->can("to_safe_string")
Truth table100%3/3
A and B
A= builtin::blessed($_)B= $_->can("to_safe_string")
ABresult
0X0
100
111
1195 ? $_->to_safe_string
1196 : $_;
1197 } $cb->($proxy);
1198  
1199 3 return Valiant::HTML::FormBuilder::Proxy->new(
1200 form_builder => $self,
1201 attribute => undef,
1202 _parts => \@return,
1203 );
1204 }
1205  
1206 package Valiant::HTML::FormBuilder::Proxy;
1207  
1208 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1209 use overload
1210 4 ▶ '""' => sub { $_[0]->to_string() },
Subroutine __ANON__: called CC=1 SCAR=0.0
1211 0 ▶ '0+' => sub { $_[0]->to_string() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1212 2 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
1213  
1214 2 has 'attribute' => (is=>'ro', required=>0);
1215 2 has 'form_builder' => (is=>'ro', required=>1);
1216 7 ▶has '_parts' => (is=>'rw', default=>sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
1217  
1218 sub _add_part {
1219 15 ▶ my ($self, @new_parts) = @_;
Subroutine _add_part: called CC=1 SCAR=0.0
1220 15 my @parts = @{$self->_parts};
1221 15 $self->_parts([@parts, @new_parts]);
1222 }
1223  
1224 sub _clear {
1225 0 ▶ my ($self) = @_;
Subroutine _clear: not called CC=1 SCAR=6.9
1226 0 $self->_parts([]);
1227 0 return $self;
1228 }
1229  
1230 sub to_string {
1231 7 ▶ my ($self) = @_;
Subroutine to_string: called CC=2 SCAR=7.0
Pod undocumented
1232 7 ▶ my @parts = map { ref($_) eq 'CODE' ? $_->() : $_ } @{$self->_parts};
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'CODE' ? : 0 20 20
1233 7 return my $safe = $self->form_builder->view->safe_concat(@parts);
1234 }
1235  
1236 # support for Template::EmbeddedPerl safe stringification
1237 sub to_safe_string {
1238 3 ▶ my ($self, $template) = @_;
Subroutine to_safe_string: called CC=1 SCAR=0.0
Pod undocumented
1239 3 return $self->to_string;
1240 }
1241  
1242 sub end {
1243 1 ▶ my ($self) = @_;
Subroutine end: called CC=1 SCAR=0.0
Pod undocumented
1244 1 my $class = ref $self;
1245 1 return $class->new(
1246 form_builder => $self->form_builder,
1247 attribute => undef,
1248 _parts => $self->_parts,
1249 );
1250  
1251 }
1252  
1253 sub AUTOLOAD {
1254 29 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=6 SCAR=18.0
1255 29 my $method = our $AUTOLOAD;
1256 29 $method =~ s/.*:://; # remove package name
1257  
1258 29 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 11 18 29
1259  
1260 # This really needs to do the right thing and delegate the $method call
1261 # based on what $method is. That's why this feature is prototype status
1262 # and undocumented!
1263  
1264 18 ▶ if($self->form_builder->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->form_builder->can($method)) 18 0 18
1265 18 my $fb = $self->form_builder;
1266 18 my $attribute = $self->attribute;
1267 18 ▶ my @args = $attribute ? ($attribute, @_) : (@_);
Branch: 1 branch
BranchTrueFalseTotal
$attribute ? : 14 4 18
1268 18 my $result = $fb->$method(@args);
1269  
1270 18 ▶ if(Scalar::Util::blessed($result) && $result->isa('Valiant::HTML::FormBuilder::Proxy')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($result) and $result->isa("Valiant::HTML::FormBuilder::Proxy")) 3 15 18
Condition66%2/3
builtin::blessed($result) and $result->isa("Valiant::HTML::FormBuilder::Proxy")
!ll&&!rl&&r
0153
MC/DC50%1/2
builtin::blessed($result) and $result->isa("Valiant::HTML::FormBuilder::Proxy")
builtin::blessed($result) $result->isa("Valiant::HTML::FormBuilder::Proxy")
Truth table66%2/3
A and B
A= builtin::blessed($result)B= $result->isa("Valiant::HTML::FormBuilder::Proxy")
ABresult
0X0
100
111
1271 3 $result->_parts([ @{$self->_parts}, @{$result->_parts}]);
1272 3 return $result;
1273 }
1274  
1275 15 $self->_add_part($result);
1276 15 return $self;
1277 }
1278  
1279 0 die "Can't find method $method on "
1280 . ref($self->form_builder)
1281 . " for attribute "
1282 . $self->attribute;
1283 }
1284  
1285 2 1;
1286  
1287 =head1 NAME
1288  
1289 Valiant::HTML::FormBuilder - General HTML Forms
1290  
1291 =head1 SYNOPSIS
1292  
1293 Given a model with the correct API such as:
1294  
1295 package Local::Person;
1296  
1297 use Moo;
1298 use Valiant::Validations;
1299  
1300 has first_name => (is=>'ro');
1301 has last_name => (is=>'ro');
1302  
1303 validates ['first_name', 'last_name'] => (
1304 length => {
1305 maximum => 10,
1306 minimum => 3,
1307 }
1308 );
1309  
1310 Wrap a formbuilder object around it and generate HTML form field controls:
1311  
1312 my $person = Local::Person->new(first_name=>'J', last_name=>'Napiorkowski');
1313 $person->validate;
1314  
1315 my $fb = Valiant::HTML::FormBuilder->new(
1316 model => $person,
1317 name => 'person'
1318 );
1319  
1320 print $fb->input('first_name');
1321 # <input id="person_first_name" name="person.first_name" type="text" value="J"/>
1322  
1323 print $fb->errors_for('first_name');
1324 # <div>First Name is too short (minimum is 3 characters)</div>
1325  
1326 Although you can create a formbuilder instance directly as in the above example you might
1327 find it easier to use the export helper method L<Valiant::HTML::Form/form_for> which encapsulates
1328 the display logic needed for creating the C<form> tags. This builder creates form tag elements
1329 but not the actual C<form> open and close tags.
1330  
1331 =head1 DESCRIPTION
1332  
1333 This class wraps an underlying data model and makes it easy to build HTML form elements based on
1334 the state of that model. Inspiration for this design come from Ruby on Rails Formbuilder as well
1335 as similar designs in the Phoenix Framework.
1336  
1337 You can subclass this to future customize how your form elements display as well as to add more complex
1338 form elements for your templates.
1339  
1340 Documentation here is basically API level, a more detailed tutorial will follow eventually but for now
1341 you'll need to review the source, test cases and example application bundled with this distribution
1342 for for hand holding.
1343  
1344 Currently this is designed to work mostly with the L<Valiant> model validation framework as well as the
1345 glue for L<DBIx:Class>, L<DBIx:Class::Valiant>, although I did take pains to try and make the API
1346 agnostic many of the test cases are assuming that stack and getting that integration working well is
1347 the primary use case for me. Thoughts and code to make this more stand alone are very welcomed.
1348  
1349 =head1 ATTRIBUTES
1350  
1351 This class defines the following attributes used in creating an instance.
1352  
1353 =head2 model
1354  
1355 This is the data model that the formbuilder inspects for field state and error conditions. This should be
1356 a model that does the API described here: L<Valiant::HTML::Form/'REQUIRED MODEL API'>. Required but the API
1357 is pretty flexible (see docs).
1358  
1359 Please note that my initial use case for this is using L<Valiant> for validation and L<DBIx::Class> as the
1360 model (via L<DBIx:Class::Valiant>) so that combination has the most testing and examples. If you are using
1361 a different storage or validation setup you need to complete the API described. Please send test cases
1362 and pull requests to improve interoperability!
1363  
1364 =head2 name
1365  
1366 This is a string which is the internal name given to the model. This is used to set a namespace for form
1367 field C<name> attributes and the default namespace for C<id> attributes. Required.
1368  
1369 =head2 options
1370  
1371 A optional hashref of options used in form field generation. Some of these might become attributes in the
1372 future. Here's a list of the current options
1373  
1374 =over 4
1375  
1376 =item index
1377  
1378 The index of the formbuilder when it is a sub formbuilder with a parent and we are iterating over a collection.
1379  
1380 =item child_index
1381  
1382 When creating a sub formbuilder that is an element in a collection, this is used to pass the index value
1383  
1384 =item builder
1385  
1386 The package name of the current builder
1387  
1388 =item parent_builder
1389  
1390 The parent formbuilder instance to a sub builder.
1391  
1392 =item include_id
1393  
1394 Used to indicated that a sub formbuilder should add hidden fields indicating the storage ID for the current model.
1395  
1396 =item namespace
1397  
1398 The ID namespace; used to populate the C<namespace> attribute.
1399  
1400 =item as
1401  
1402 Used to override how the class and ids are made for your forms.
1403  
1404 =back
1405  
1406 =head2 index
1407  
1408 The current index of a collection for which the current formbuilder is one item in.
1409  
1410 =head2 namespace
1411  
1412 Used to add a prefix to the ID for your form elements.
1413  
1414 =head2 skip_default_ids
1415  
1416 Defaults to false. Generally we create an html C<id> attribute for the field based
1417 on a convention which includes the model name, index and method name. Setting this
1418 to true prevents that so you should set C<id> manually unless you don't want them.
1419 Please note that even if this is false, you can always override the C<id> on a per
1420 field basis by setting it manually.
1421  
1422 =head2 view
1423  
1424 Optional. The view or template object that is using the formbuilder. If available can be used to
1425 influence how the HTML for controls are created.
1426  
1427 Generally used to provide HTML escaping and safe string tagging methods that are compatible with
1428 your template system. For example L<Mojo::Template> provides its own system for marking strings
1429 safe for template display. I you don't provide a view then we will use L<Valiant::HTML::SafeString>
1430 for automatic HTML escaping. If you are not using a view or template system (for example like
1431 L<Template::Toolkit> ) that does automatic escaping then the built in escaping features are probably
1432 fine.
1433  
1434 If you provide a view it should provide the following API methods:
1435  
1436 =over 4
1437  
1438 =item raw
1439  
1440 given a string return a single tagged object which is marked as safe for display. Do not do any HTML
1441 escaping on the string. This is used when you want to pass strings straight to display and that you
1442 know is safe. Be careful with this to avoid HTML injection attacks.
1443  
1444 =item safe
1445  
1446 given a string return a single tagged object which is marked as safe for display. First HTML escape the
1447 string as safe unless its already been done (no double escaping).
1448  
1449 =item safe_concat
1450  
1451 Same as C<safe> but instead works an an array of strings (or mix of strings and safe string objects) and
1452 concatenates them all into one big safe marked string.
1453  
1454 =item html_escape
1455  
1456 Given a string return string that has been HTML escaped.
1457  
1458 =item read_attribute_for_view
1459  
1460 Given an attribute name return the value that the view has defined for it.
1461  
1462 B<NOTE> Optional
1463  
1464 =item attribute_for_view_exists
1465  
1466 Given an attribute name return true if the view has defined a value for it.
1467  
1468 B<NOTE> Optional
1469  
1470 =back
1471  
1472 Both C<raw>, C<safe> and C<safe_concat> should return a 'tagged' object which is specific to your view or
1473 template system. However this object must 'stringify' to the safe version of the string to be displayed. See
1474 L<Valiant::HTML::SafeString> for example API. We use <Valiant::HTML::SafeString> internally to provide
1475 safe escaping if you're view doesn't do automatic escaping, as many older template systems like Template
1476 Toolkit.
1477  
1478 B<NOTE>: In the future the view API might change so keep an eye on this spot.
1479  
1480 =head1 METHODS
1481  
1482 This class defines the following public instance methods.
1483  
1484 =head2 form_action
1485  
1486 The Form action attribute
1487  
1488 =head2 form_method
1489  
1490 The form method attribute
1491  
1492 =head2 form_enctype
1493  
1494 the form enctype attribute
1495  
1496 =head2 csrf_token
1497  
1498 The CSRF token, if there is one
1499  
1500 =head2 allow_method_names_outside_object
1501  
1502 accessor to the 'allow_method_names_outside_object' option. This is true by default. Allow you
1503 to control the behavior of what happens when you try to access a model attribute that doesn't exist
1504  
1505 =head2 tag_errors_for_attribute
1506  
1507 Given an attribute return an array of error messages for that attribute if any.
1508 This calls C<field_errors> on the FormTags object. For this to work you either
1509 need a model that responds to C<read_attribute_errors_for> (which tasks a single
1510 attribute name and returns an array of error messages) or the model has an
1511 C<errors> method that returns a L<Valiant::Errors> object.
1512  
1513 =head2 tag_value_for_attribute
1514  
1515 $fb->tag_value_for_attribute('name');
1516 $fb->tag_value_for_attribute('name', \%options);
1517  
1518 Given an attribute return the value of that attribute on the model.
1519  
1520 Currently the only option is C<no_strict_method_check> which if true will allow you use an attribute
1521 name that strictly isn't a method on the model. This is useful if you want to create a form field
1522 that doesn't correspond to a model attribute but still respects the current namespace and index.
1523  
1524 You can pass this via the options hashref for most tag generating methods.
1525  
1526 =head2 form_action_for
1527  
1528 Usage:
1529  
1530 $fb->form_action_for('path1');
1531 $fb->form_action_for('path1', 'path2');
1532 $fb->form_action_for(\%options);
1533 $fb->form_action_for('path1', \%options);
1534 $fb->form_action_for('path1', 'path2', \%options);
1535  
1536 Used to create additional URLs under the current form action. Useful if you have form
1537 buttons that when activated should go to a different URL than the main form action or
1538 where you want to pass query parameters that influence the form action.
1539  
1540 =head2 form_has_errors
1541  
1542 Display a message if the form has errors. This is a convenience method that you can use in your
1543 template to display a message if there are any errors in the form. This is useful if you want to
1544 display a message at the top of the form. You can also use this method to display a message at the
1545 top of a sub formbuilder if you are using sub formbuilders.
1546  
1547 A form has errors if any of the fields has an error or if the model has errors.
1548  
1549 $fb->form_has_errors();
1550 $fb->form_has_errors(\%attrs);
1551 $fb->form_has_errors(\%attrs, $content);
1552 $fb->form_has_errors(\$content);
1553 $fb->form_has_errors(\&template);
1554  
1555 Examples:
1556  
1557 # with default content
1558 $fb->form_has_errors;
1559  
1560 # with simple content
1561 $fb->form_has_errors('There were errors in the form. Please correct them.');
1562  
1563 # Simple content with attributes
1564 $fb->form_has_errors({ class=>'alert alert-danger', role=>'alert' },
1565 'There were errors in the form. Please correct them and try again.');
1566  
1567 # with complex content
1568 $fb->form_has_errors(sub ($self, $fb, $contact) {
1569 div +{ class=>'alert alert-danger', role=>'alert' }, [
1570 'There were errors in the form. Please correct them and try again.',
1571 ]
1572 });
1573  
1574 If you pass a scalar as content, the scalar can be a string or a translation tag.
1575  
1576 B<Note> Please note this method doesn't show any of the model or field errors,
1577 it just shows a generic 'form has errors' message. You can either call the field or
1578 L</model_errors> methods, or you can loop over errors in the custom complex
1579 content callback. Alternatively if you have model level errors for this object
1580 you might prefer to use the L</model_errors> method with the C<show_message_on_field_errors>
1581 option.
1582  
1583 =head2 model_errors
1584  
1585 $fb->model_errors();
1586 $fb->model_errors(\%attrs);
1587 $fb->model_errors(\%attrs, \&template); # %attrs limited to 'max_errors' and 'show_message_on_field_errors'
1588 $fb->model_errors(\&template);
1589  
1590 Display model level errors, either with a default or custom template. 'Model' errors are
1591 errors that are not associated with a model attribute in particular, but rather the model
1592 as a whole.
1593  
1594 Arguments to this method are optional. "\%attrs" is a hashref which is passed to the tag
1595 builder to create any needed HTML attributes (such as class and style). "\&template" is
1596 a coderef that gets the @errors as an argument and you can use it to customize how the errors
1597 are displayed. Otherwise we use a default template that lists the errors with an HTML ordered
1598 list, or a C<div> if there's only one error.
1599  
1600 "\%attrs" can also contain two options that gives you some additional control over the display
1601  
1602 =over
1603  
1604 =item max_errors
1605  
1606 Don't display more than a certain number of errors
1607  
1608 =item show_message_on_field_errors
1609  
1610 Sometimes you want a global message displayed when there are field errors. L<Valiant> doesn't
1611 add a model error if there's field errors (although it would be easy for you to add this yourself
1612 with a model validation) so this makes it easy to display such a message. If a string or translation
1613 tag then show that, if its a '1' the show the default message, which is "Form has errors" unless
1614 you overide it.
1615  
1616 This can be a useful option when you have a long form and you want a user to know there's errors
1617 possibly off the browser screen.
1618  
1619 =back
1620  
1621 Examples. Assume two model level errors "Trouble 1" and "Trouble 2":
1622  
1623 $fb->model_errors;
1624 # <ol><li>Trouble 1</li><li>Trouble 2</li></ol>
1625  
1626 $fb->model_errors({class=>'foo'});
1627 # <ol class="foo"><li>Trouble 1</li><li>Trouble 2</li></ol>
1628  
1629 $fb->model_errors({max_errors=>1});
1630 # <div>Trouble 1</div>
1631  
1632 $fb->model_errors({max_errors=>1, class=>'foo'})
1633 # <div class="foo">Trouble 1</div>
1634  
1635 $fb->model_errors({show_message_on_field_errors=>1})
1636 # <ol><li>Form has errors</li><li>Trouble 1</li><li>Trouble 2</li></ol>
1637  
1638 $fb->model_errors({show_message_on_field_errors=>"Bad!"})
1639 # <ol><li>Bad!</li><li>Trouble 1</li><li>Trouble 2</li></ol>
1640  
1641 $fb->model_errors(sub {
1642 my (@errors) = @_;
1643 join " | ", @errors;
1644 });
1645 # Trouble 1 | Trouble 2
1646  
1647 =head2 label
1648  
1649 $fb->label($attribute)
1650 $fb->label($attribute, \%options)
1651 $fb->label($attribute, $content)
1652 $fb->label($attribute, \%options, $content)
1653 $fb->label($attribute, \&content); sub content { my ($translated_attribute) = @_; ... }
1654 $fb->label($attribute, \%options, \&content); sub content { my ( $translated_attribute) = @_; ... }
1655  
1656 Creates a HTML form element C<label> with the given "\%options" passed to the tag builder to
1657 create HTML attributes and an optional "$content". If "$content" is not provided we use the
1658 human, translated (if available) version of the "$attribute" for the C<label> content. Alternatively
1659 you can provide a template which is a subroutine reference which recieves the translated attribute
1660 as an argument. Examples:
1661  
1662 $fb->label('first_name');
1663 # <label for="person_first_name">First Name</label>
1664  
1665 $fb->label('first_name', {class=>'foo'});
1666 # <label class="foo" for="person_first_name">First Name</label>
1667  
1668 $fb->label('first_name', 'Your First Name');
1669 # <label for="person_first_name">Your First Name</label>
1670  
1671 $fb->label('first_name', {class=>'foo'}, 'Your First Name');
1672 # <label class="foo" for="person_first_name">Your First Name</label>
1673  
1674 $fb->label('first_name', sub {
1675 my $translated_attribute = shift;
1676 return "$translated_attribute ",
1677 $fb->input('first_name');
1678 });
1679 # <label for="person_first_name">
1680 # First Name
1681 # <input id="person_first_name" name="person.first_name" type="text" value="John"/>
1682 # </label>
1683  
1684 $fb->label('first_name', +{class=>'foo'}, sub {
1685 my $translated_attribute = shift;
1686 return "$translated_attribute ",
1687 $fb->input('first_name');
1688 });
1689 # <label class="foo" for="person_first_name">
1690 # First Name
1691 # <input id="person_first_name" name="person.first_name" type="text" value="John"/>
1692 # </label>
1693  
1694 =head2 errors_for
1695  
1696 $fb->errors_for($attribute)
1697 $fb->errors_for($attribute, \%options)
1698 $fb->errors_for($attribute, \%options, \&template)
1699 $fb->errors_for($attribute, \&template)
1700  
1701 Similar to L</model_errors> but for errors associated with an attribute of a model. Accepts
1702 the $attribute name, a hashref of \%options (used to set options controling the display of
1703 errors as well as used by the tag builder to create HTML attributes for the containing tag) and
1704 lastly an optional \&template which is a subroutine reference that received an array of the
1705 translated errors for when you need very custom error display. If omitted we use a default
1706 template displaying errors in an ordered list (if more than one) or wrapped in a C<div> tag
1707 (if only one error).
1708  
1709 \%options used for error display and which are not passed to the tag builder as HTML attributes:
1710  
1711 =over
1712  
1713 =item max_errors
1714  
1715 Don't display more than a certain number of errors
1716  
1717 =item show_empty
1718  
1719 Defaults to false.
1720  
1721 If true then display an empty container even if there are no errors. By default we don't
1722 display anything if there are no errors. If you enable this option then you can use the
1723 \&template to display a message like "No errors" or something similar. Or you can use CSS
1724 to hide the container if there are no errors but need a container if you are also updating
1725 the container with AJAX.
1726  
1727 B<NOTE> If you set this to true then C<alway_array> is also set to true UNLESS you've specified
1728 a different value.
1729  
1730 =item always_array
1731  
1732 Defaults to false.
1733  
1734 If true then render the default content as if there was more than one error. This is useful
1735 if you want to always render the default content as an ordered list even if there's only one
1736 error.
1737  
1738 =back
1739  
1740 Assume the attribute 'last_name' has the following two errors in the given examples: "first Name
1741 is too short", "First Name contains non alphabetic characters".
1742  
1743 $fb->errors_for('first_name');
1744 # <ol><li>First Name is too short (minimum is 3 characters)</li><li>First Name contains non alphabetic characters</li></ol>
1745  
1746 $fb->errors_for('first_name', {class=>'foo'});
1747 # <ol class="foo"><li>First Name is too short (minimum is 3 characters)</li><li>First Name contains non alphabetic characters</li></ol>
1748  
1749 $fb->errors_for('first_name', {class=>'foo', max_errors=>1});
1750 # <div class="foo">First Name is too short (minimum is 3 characters)</div>
1751  
1752 $fb->errors_for('first_name', sub {
1753 my (@errors) = @_;
1754 join " | ", @errors;
1755 });
1756 # First Name is too short (minimum is 3 characters) | First Name contains non alphabetic characters
1757  
1758 B<NOTE>For this to work the model must either respond to C<read_attribute_errors_for> (which
1759 tasks a single attribute name and returns an array of error messages) or the model has an
1760 C<errors> method that returns a L<Valiant::Errors> object. If the model does neither then this
1761 will always return an empty string.
1762  
1763 =head2 input
1764  
1765 $fb->input($attribute, \%options)
1766 $fb->input($attribute)
1767  
1768 Create an C<input> form tag using the $attribute's value (if any) and optionally passing a hashref of
1769 \%options which are passed to the tag builder to create HTML attributes for the C<input> tag. Optionally
1770 add C<errors_classes> which is a string that is appended to the C<class> attribute when the $attribute has
1771 errors. Examples:
1772  
1773 $fb->input('first_name');
1774 # <input id="person_first_name" name="person.first_name" type="text" value="J"/>
1775  
1776 $fb->input('first_name', {class=>'foo'});
1777 # <input class="foo" id="person_first_name" name="person.first_name" type="text" value="J"/>
1778  
1779 $fb->input('first_name', {errors_classes=>'error'});
1780 # <input class="error" id="person_first_name" name="person.first_name" type="text" value="J"/>
1781  
1782 $fb->input('first_name', {class=>'foo', errors_classes=>'error'});
1783 # <input class="foo error" id="person_first_name" name="person.first_name" type="text" value="J"/>
1784  
1785 Special \%options:
1786  
1787 =over 4
1788  
1789 =item errors_classes
1790  
1791 A string that is appended to the C<class> attribute if the $attribute has errors (as defined by the model API)
1792  
1793 =item force_validity
1794  
1795 Defaults to undefined. Use this if you want to force the field to be either valid or invalid. At
1796 this time the only thing this does is control if C<errors_classes> are added to the field.
1797  
1798 =back
1799  
1800 =head2 password
1801  
1802 $fb->password($attribute, \%options)
1803 $fb->password($attribute)
1804  
1805 Create a C<password> HTML form field. Similar to L</input> but sets the C<type> to 'password' and also
1806 sets C<value> to '' since generally you don't want to show the current password (and if you are doing the
1807 right thing and saving a 1 way hash not the plain text you don't even have it to show anyway).
1808  
1809 Example:
1810  
1811 $fb->password('password');
1812 # <input id="person_password" name="person.password" type="password" value=""/>
1813  
1814 $fb->password('password', {class='foo'});
1815 # <input class="foo" id="person_password" name="person.password" type="password" value=""/>
1816  
1817 $fb->password('password', {class='foo', errors_classes=>'error'});
1818 # <input class="foo error" id="person_password" name="person.password" type="password" value=""/>
1819  
1820 =head2 hidden
1821  
1822 $fb->hidden($attribute, \%options)
1823 $fb->hidden($attribute)
1824  
1825 Create a C<hidden> HTML form field. Similar to L</input> but sets the C<type> to 'hidden'.
1826  
1827 $fb->hidden('id');
1828 # <input id="person_id name="person.id" type="hidden" value="101"/>
1829  
1830 $fb->hidden('id', {class='foo'});
1831 # <input class="foo" id="person_id name="person.id" type="hidden" value="101"/>
1832  
1833 =head2 text_area
1834  
1835 $fb->text_area($attribute);
1836 $fb->text_area($attribute, \%options);
1837  
1838 Create an HTML C<text_area> tag based on the attribute value and with optional \%options
1839 which is a a hashref passed to the tag builder for generating HTML attributes. Can also set
1840 C<errors_classes> that will append a string of additional CSS classes when the $attribute has
1841 errors. Examples:
1842  
1843 $fb->text_area('comments');
1844 # <textarea id="person_comments" name="person.comments">J</textarea>
1845  
1846 $fb->text_area('comments', {class=>'foo'});
1847 # <textarea class="foo" id="person_comments" name="person.comments">J</textarea>
1848  
1849 $fb->text_area('comments', {class=>'foo', errors_classes=>'error'});
1850 # <textarea class="foo error" id="person_comments" name="person.comments">J</textarea>
1851  
1852 Special \%options:
1853  
1854 =over 4
1855  
1856 =item errors_classes
1857  
1858 A string that is appended to the C<class> attribute if the $attribute has errors (as defined by the model API)
1859  
1860 =back
1861  
1862 =head2 checkbox
1863  
1864 $fb->checkbox($attribute);
1865 $fb->checkbox($attribute, \%options);
1866 $fb->checkbox($attribute, $checked_value, $unchecked_value);
1867 $fb->checkbox($attribute, \%options, $checked_value, $unchecked_value);
1868  
1869 Generate an HTML form checkbox element with its state based on evaluating the value of $attribute
1870 in a boolean context. If $attribute is true then the C<checkbox> will be checked. May also pass a
1871 hashref of \%options, which contain render instructions and HTML attributes used by the tag builder.
1872 "$checked_value" and "$unchecked_value" specify the values when the checkbox is checked or not (defaults
1873 to 1 for checked and 0 for unchecked, but $unchecked is ignored if option C<include_hidden> is set to
1874 false; see below).
1875  
1876 Special \%options:
1877  
1878 =over 4
1879  
1880 =item errors_classes
1881  
1882 A string that is appended to the C<class> attribute if the $attribute has errors (as defined by the model API)
1883  
1884 =item include_hidden
1885  
1886 Defaults to true. Since the rules for an HTML form checkbox specify that if the checkbox is 'unchecked' then
1887 nothing is submitted. This can cause issues if you are expecting a submission that somehow indicates 'unchecked'
1888 For example you might have a status field boolean where unchecked should indicate 'false'. So by default we add
1889 a hidden field with the same name as the checkbox, with a value set to $unchecked_value (defaults to 0). In the
1890 case where the field is checked then you'll get two values for the same field name so you should have logic that
1891 in the case that field name is an array then take the last one (if you are using L<Plack::Request> this is using
1892 L<Hash::MultiValue> which does this by default; if you are using L<Catalyst> you can use the C<use_hash_multivalue_in_request>
1893 option or you can use something like L<Catalyst::TraitFor::Request::StructuredParameters> which has options to
1894 help with this. If you are using L<Mojolicious> then the C<param> method works this way as well.
1895  
1896 =item checked
1897  
1898 A boolean value to indicate if the checkbox field is 'checked' when generated. By default a checkbox state is
1899 determined by the value of $attribute for the underlying model. You can use this to override (for example you
1900 might wish a checkbox default state to be checked when creating a new entry when it would otherwise be false).
1901  
1902 =back
1903  
1904 Examples:
1905  
1906 $fb->checkbox('status');
1907 # <input name="person.status" type="hidden" value="0"/>
1908 # <input id="person_status" name="person.status" type="checkbox" value="1"/>
1909  
1910 $fb->checkbox('status', {class=>'foo'});
1911 # <input name="person.status" type="hidden" value="0"/>
1912 # <input class="foo" id="person_status" name="person.status" type="checkbox" value="1"/>
1913  
1914 $fb->checkbox('status', 'active', 'deactive');
1915 # <input name="person.status" type="hidden" value="deactive"/>
1916 # <input id="person_status" name="person.status" type="checkbox" value="active"/>
1917  
1918 $fb->checkbox('status', {include_hidden=>0});
1919 # <input id="person_status" name="person.status" type="checkbox" value="1"/>
1920  
1921 $person->status(1);
1922 $fb->checkbox('status', {include_hidden=>0});
1923 # <input checked id="person_status" name="person.status" type="checkbox" value="1"/>
1924  
1925 $person->status(0);
1926 $fb->checkbox('status', {include_hidden=>0, checked=>1});
1927 # <input checked id="person_status" name="person.status" type="checkbox" value="1"/>
1928  
1929 $fb->checkbox('status', {include_hidden=>0, errors_classes=>'err'});
1930 # <input class="err" id="person_status" name="person.status" type="checkbox" value="1"/>
1931  
1932 =head2 radio_button
1933  
1934 $fb->radio_button($attribute, $value);
1935 $fb->radio_button($attribute, $value, \%options);
1936  
1937 Generate an HTML input type 'radio', typically part of a group including 2 or more controls.
1938 Generated value attributes uses $value, the control is marked 'checked' when $value matches
1939 the value of $attribute (or you can override, see below). \%options are HTML attributes which
1940 are passed to the tag builder unless special as described below.
1941  
1942 Special \%options:
1943  
1944 =over 4
1945  
1946 =item errors_classes
1947  
1948 A string that is appended to the C<class> attribute if the $attribute has errors (as defined by the model API)
1949  
1950 =item checked
1951  
1952 A boolean which determines if the input radio control is marked 'checked'. Used if you want to
1953 override the default.
1954  
1955 =back
1956  
1957 Examples:
1958  
1959 # Example radio group
1960  
1961 $person->type('admin');
1962  
1963 $fb->radio_button('type', 'admin');
1964 $fb->radio_button('type', 'user');
1965 $fb->radio_button('type', 'guest');
1966  
1967 #<input checked id="person_type_admin" name="person.type" type="radio" value="admin"/>
1968 #<input id="person_type_user" name="person.type" type="radio" value="user"/>
1969 #<input id="person_type_guest" name="person.type" type="radio" value="guest"/>
1970
1971 # Example \%options
1972  
1973 $fb->radio_button('type', 'guest', {class=>'foo', errors_classes=>'err'});
1974 # <input class="foo err" id="person_type_guest" name="person.type" type="radio" value="guest"/>
1975  
1976 $fb->radio_button('type', 'guest', {checked=>1});
1977 # <input checked id="person_type_guest" name="person.type" type="radio" value="guest"/>
1978  
1979 =head2 date_field
1980  
1981 $fb->date_field($attribute);
1982 $fb->date_field($attribute, \%options);
1983  
1984 Generates a 'type' C<date> HTML input control. Used when your $attribute value is a L<DateTime>
1985 object to get proper string formatting. Although the C<date> type is considered HTML5 you can
1986 use this for older HTML versions as well when you need to get the object formatting (you just don't
1987 get the date HTML controls).
1988  
1989 When the $attribute value is a L<DateTime> object (or actually is any object) we call '->ymd' to stringify
1990 the object to the expected format. '\%options' as in the input control are passed to the tag builder
1991 to create HTML attributes on the input tag with the exception of the specials ones already documented
1992 (such as C<errors_classes>) and the following special \%options
1993  
1994 =over 4
1995  
1996 =item min
1997  
1998 =item max
1999  
2000 When these are L<DateTime> objects we stringify using ->ymd to get the expected format; otherwise
2001 they are passed as is to the tag builder.
2002  
2003 =back
2004  
2005 Examples:
2006  
2007 $person->birthday(DateTime->new(year=>1969, month=>2, day=>13));
2008  
2009 $fb->date_field('birthday');
2010 # <input id="person_birthday" name="person.birthday" type="date" value="1969-02-13"/>
2011  
2012 $fb->date_field('birthday', {class=>'foo', errors_classes=>'err'});
2013 # <input class="foo err" id="person_birthday" name="person.birthday" type="date" value="1969-02-13"/>
2014  
2015 $fb->date_field('birthday', +{
2016 min => DateTime->new(year=>1900, month=>1, day=>1),
2017 max => DateTime->new(year=>2030, month=>1, day=>1),
2018 });
2019 #<input id="person_birthday" max="2030-01-01" min="1900-01-01" name="person.birthday" type="date" value="1969-02-13"/>
2020  
2021 =head2 datetime_local_field
2022  
2023 =head2 time_field
2024  
2025 Like L</date_field> but sets the input type to C<datetime-local> or C<time> respectively and formats any
2026 <DateTime> values with "->strftime('%Y-%m-%dT%T')" (for C<datetime-local>) or either "->strftime('%H:%M')"
2027 or "->strftime('%T.%3N')" for C<time> depending on the \%options C<include_seconds>, which defaults to true).
2028  
2029 Examples:
2030  
2031 $person->due(DateTime->new(year=>1969, month=>2, day=>13, hour=>10, minute=>45, second=>11, nanosecond=> 500000000));
2032  
2033 $fb->datetime_local_field('due');
2034 # <input id="person_due" name="person.due" type="datetime-local" value="1969-02-13T10:45:11"/>
2035  
2036 $fb->time_field('due');
2037 # <input id="person_due" name="person.due" type="time" value="10:45:11.500"/>
2038  
2039 $fb->time_field('due', +{include_seconds=>0});
2040 # <input id="person_due" name="person.due" type="time" value="10:45"/>
2041  
2042 =head2 submit
2043  
2044 $fb->submit;
2045 $fb->submit(\%options);
2046 $fb->submit($value);
2047 $fb->submit($value, \%options);
2048  
2049 Create an HTML submit C<input> tag with a meaningful default value based on the model name and its state
2050 in storage (if supported by the model). Will also look up the following two translation tag keys:
2051  
2052 "formbuilder.submit.@{[ $self->name ]}.${key}"
2053 "formbuilder.submit.${key}"
2054  
2055 Where $key is by default 'submit' and if the model supports 'in_storage' its either 'update' or 'create'
2056 depending on if the model is new or already existing.
2057  
2058 Examples:
2059  
2060 $fb->submit;
2061 # <input id="commit" name="commit" type="submit" value="Submit Person"/>
2062  
2063 $fb->submit('Login', {class=>'foo'});
2064 # <input class="foo" id="commit" name="commit" type="submit" value="Login"/>
2065  
2066 =head2 button
2067  
2068 $fb->button($name, \%attrs, \&block)
2069 $fb->button($name, \%attrs, $content)
2070 $fb->button($name, \&block)
2071 $fb->button($name, $content)
2072  
2073 Create a C<button> tag with custom attibutes and content. Content be be a string or a coderef if you
2074 need to do complex layout.
2075  
2076 Useful to create submit buttons with fancy formatting or when you need a button that submits in the
2077 form namespace.
2078  
2079 Examples:
2080  
2081 $person->type('admin');
2082  
2083 $fb->button('type');
2084 # <button id="person_type" name="person.type" type="submit" value="admin">Button</button>
2085  
2086 $fb->button('type', {class=>'foo'});
2087 # <button class="foo" id="person_type" name="person.type" type="submit" value="admin">Button</button>
2088  
2089 $fb->button('type', "Press Me")
2090 # <button id="person_type" name="person.type" type="submit" value="admin">Press Me</button>
2091  
2092 $fb->button('type', sub { "Press Me" })
2093 # <button id="person_type" name="person.type" type="submit" value="admin">Press Me</button>
2094  
2095 =head2 legend
2096  
2097 $fb->legend;
2098 $fb->legend(\%options);
2099 $fb->legend($content);
2100 $fb->legend($content, \%options);
2101 $fb->legend(\&template);
2102 $fb->legend(\%options, \&template);
2103  
2104 Create an HTML Form C<legend> element with default content that is based on the model name.
2105 Accepts \%options which are passed to the tag builder and used to create HTML element attributes.
2106 You can override the content with either a $content string or a \&template coderef (which will
2107 receive the default content translation as its first argument).
2108  
2109 The default content will be based on the model name and can be influenced by its storage status
2110 if C<in_storage> is supplied by the model. We attempt to lookup the content string via the
2111 following translation tags (if the body supports ->i18n):
2112  
2113 "formbuilder.legend.@{[ $self->name ]}.${key}"
2114 "formbuilder.legend.${key}"
2115  
2116 Where $key is 'new' if the model doesn't support C<in_storage> else it's either 'update' or 'create'
2117 based on if the current model is already in storage (update) or its new and needs to be created.
2118  
2119 Examples:
2120  
2121 $fb->legend;
2122 # <legend>New Person</legend>
2123  
2124 $fb->legend({class=>'foo'});
2125 # <legend class="foo">New Person</legend>
2126  
2127 $fb->legend("Person");
2128 # <legend>Person</legend>
2129  
2130 $fb->legend("Persons", {class=>'foo'});
2131 # <legend class="foo">Persons</legend>
2132  
2133 $fb->legend(sub { shift . " Info"});
2134 # <legend>New Person Info</legend>
2135  
2136 $fb->legend({class=>'foo'}, sub {"Person"});
2137 # <legend class="foo">Person</legend>
2138  
2139 =head2 legend_for
2140  
2141 $fb->legend_for($attr);
2142 $fb->legend_for($attr, \%options);
2143  
2144 Creates an HTML C<legend> tags with it's content set to the human translated name of the given
2145 $attribute. Allows you to pass some additional HTML attributes to the legend tag. Examples:
2146  
2147 $fb->legend_for('status')
2148 # <legend id="status_legend" >Status</legend>
2149
2150 $fb->legend_for('status', {class=>'foo'})
2151 # <legend id="status_legend" class="foo" >Status</legend>
2152  
2153 =head2 fields_for
2154  
2155 $fb->fields_for($attribute, sub {
2156 my ($nested_fb, $model) = @_;
2157 });
2158  
2159 $fb->fields_for($attribute, \%options, sub {
2160 my ($nested_fb, $model) = @_;
2161 });
2162  
2163 # With a 'finally' block when $attribute is a collection
2164  
2165 $fb->fields_for($attribute, sub {
2166 my ($nested_fb, $model) = @_;
2167 }, sub {
2168 my ($nested_fb, $new_model) = @_;
2169 });
2170  
2171 Where C<$attribute> is a string that refers to a nested object or collection of objects on the model
2172 OR its an object or collection of objects itself.
2173  
2174 Used to create sub form builders under the current one for nested models (either a collection of models
2175 or a single model.) This sub form builder will be passed as the first argument to the enclosing subref
2176 and will encapsulate any indexing or namespacing; its model will be set to the sub model. You also get
2177 a second argument which is the sub model for ease of access. Note that if the $attribute refers to a collection
2178 then $model will be set to the current item model of that collection.
2179  
2180 When the $attribute refers to a collection the collection object must provide a C<next> method which should
2181 iterate thru the collection in the order desired and return C<undef> to indicate all records have been rolled
2182 thru. This collection object may also implement a C<reset> method to return the index to the start of the
2183 collection (which will be called after the final record is processed) and a C<build> method which should
2184 return a new empty record (required if you want a C<finally> block as described below).
2185  
2186 Please see L<Valiant::HTML::Util::Collection> for example. B<NOTE>: If you supply an arrayref instead of
2187 a collection object, we will build one using L<Valiant::HTML::Util::Collection> automatically. This behavior
2188 might change in the future so it would be ideal to not rely on it.
2189  
2190 If the $attribute is a collection you may optionally add a second coderef template which is called after
2191 the collect has been fully iterated thru and it recieves a sub formbuilder with a new blank model as an
2192 argument. This finally block is always called, even if the collection is empty so it can he used to
2193 generate a blank entry for adding new items to the collection (for example) or for any extra code or field
2194 controls that you want under the sub model namespace.
2195  
2196 Available \%options:
2197  
2198 =over 4
2199  
2200 =item builder
2201  
2202 The class name of the formbuilder. Defaults to L<Valiant::HTML::FormBuilder> or whatever the current
2203 builder is (if overridden in the parent).
2204  
2205 =item namespace
2206  
2207 The ID namespace. Will default to the parent formbuilder C<namespace> if there is one.
2208  
2209 =item child_index
2210  
2211 The index of the sub object. Can be a coderef. Used if you need explicit control over the index generated
2212  
2213 =item include_id
2214  
2215 Defaults to true. If the sub model does C<in_storage> and C<primary_columns> then add hidden form fields
2216 with those IDs to the sub model namespace. Often needed to properly match a record to its existing state
2217 in storage (such as a database). If you don't want this behavior then set this to false. Or
2218 if you want to control where the ids are placed you can set this to false and then use C<emit_hidden_ids>
2219  
2220 =item id
2221  
2222 Override the ID namespace for th sub model.
2223  
2224 =item index
2225  
2226 Explicitly override the index of the sub model.
2227  
2228 =item scope
2229  
2230 Explicitly override the scope of the sub model.
2231  
2232 =item as
2233  
2234 Explicitly override the name of the sub model.
2235  
2236 =back
2237  
2238 Example of an attribute that refers to a nested object.
2239  
2240 $person->profile(Local::Profile->new(zip=>'78621', address=>'ab'));
2241  
2242 $fb->fields_for('profile', sub {
2243 my $fb_profile = shift;
2244 return $fb_profile->input('address'),
2245 $fb_profile->errors_for('address'),
2246 $fb_profile->input('zip');
2247 });
2248  
2249 # <input id="person_profile_address" name="person.profile.address" type="text" value="ab"/>
2250 # <div>Address is too short (minimum is 3 characters)</div>
2251 # <input id="person_profile_zip" name="person.profile.zip" type="text" value="78621"/>
2252  
2253 Example of an attribute that refers to a nested collection object (and with a "finally block")
2254  
2255 $person->credit_cards([
2256 Local::CreditCard->new(number=>'234234223444', expiration=>DateTime->now->add(months=>11)),
2257 Local::CreditCard->new(number=>'342342342322', expiration=>DateTime->now->add(months=>11)),
2258 Local::CreditCard->new(number=>'111112222233', expiration=>DateTime->now->subtract(months=>11)), # An expired card
2259 ]);
2260  
2261 $fb->fields_for('credit_cards', sub {
2262 my $fb_cc = shift;
2263 return $fb_cc->input('number'),
2264 $fb_cc->date_field('expiration'),
2265 $fb_cc->errors_for('expiration');
2266 }, sub {
2267 my $fb_finally = shift;
2268 return $fb_finally->button('add', +{value=>1}, 'Add a New Credit Card');
2269 });
2270  
2271 # <input id="person_credit_cards_0_number" name="person.credit_cards[0].number" type="text" value="234234223444"/>
2272 # <input id="person_credit_cards_0_expiration" name="person.credit_cards[0].expiration" type="date" value="2023-01-23"/>
2273 # <input id="person_credit_cards_1_number" name="person.credit_cards[1].number" type="text" value="342342342322"/>
2274 # <input id="person_credit_cards_1_expiration" name="person.credit_cards[1].expiration" type="date" value="2023-01-23"/>
2275 # <input id="person_credit_cards_2_number" name="person.credit_cards[2].number" type="text" value="111112222233"/>
2276 # <input id="person_credit_cards_2_expiration" name="person.credit_cards[2].expiration" type="date" value="2021-03-23"/>
2277 # <div>Expiration chosen date can&#39;t be earlier than 2022-02-23</div>
2278 # <button id="person_credit_cards_3_add" name="person.credit_cards[3].add" type="submit" value="1">Add a New Credit Card</button>
2279  
2280 =head2 select
2281  
2282 $fb->select($attribute_proto, \@options, \%options)
2283 $fb->select($attribute_proto, \@options)
2284 $fb->select($attribute_proto, \%options, \&template)
2285 $fb->select($attribute_proto, \&template)
2286  
2287 Where C<$attribute_proto> is one of:
2288  
2289 $attribute # A scalar value which is an attribute on the underlying $model
2290 { $attribute => $method } # A hashref composed of an $attribute on the underlying $model
2291 # which returns a sub model or a collection of sub models
2292 # and a $method to be called on the value of that sub model (
2293 # or on each item sub model if the $attribute is a collection).
2294  
2295 Used to create a C<select> tag group with option tags. \@options can be anything that can be
2296 accepted by L<Valiant::HTML::FormTags/options_for_select>. The value(s) of $attribute_proto
2297 are automatically marked as C<selected>.
2298  
2299 Since this is built on top if C<select_tag> \%options can be anything supported by that
2300 method. See L<Valiant::HTML::FormTags/select_tag> for more. In addition we have the following
2301 special handling for \%options:
2302  
2303 =over 4
2304  
2305 =item selected
2306  
2307 =item disabled
2308  
2309 Mark C\<option> tags as selected or disabled. If you manual set selected then we ignore
2310 the value of $attribute (or @values when $attribute is a collection)
2311  
2312 =item default_selected
2313  
2314 Use this value to mark selected options ONLY if 'selected' above returns nothing AND the model does
2315 not have any values for the given attribute. Useful shortcut to make things as selected for a create
2316 field. However this can also be done with correct usage of prefetching into your 'empty' model for
2317 create.
2318  
2319 =item unselected_value
2320  
2321 The value to set the hidden 'unselected' field to. No default value. See 'include_hidden' for details.
2322  
2323 =item include_hidden
2324  
2325 Defaults to true for 'multiple' and false for single type selects.
2326  
2327 The rules for an HTML form select field specify that if the no option is 'selected' then
2328 nothing is submitted. This can cause issues if you are expecting a submission that somehow indicates 'nothing selected'
2329 means to unset some settings. So we do one of two things when 'include_hidden' is true. When the select is a
2330 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
2331 always the first value; its value is whatever you set 'unselected_value' to. If you don't set 'unselected_value' this hidden
2332 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
2333 true, or something like L<Catalyst::TraitFor::Request::StructuredParameters>) then the last value of an array
2334 body parameter will be returned which will let you choose between a default value or an actual returned value. Example:
2335  
2336 # $fb->select('state_ids', [map { [$_->label, $_->id] } $roles_collection->all], +{include_hidden=>1, unselected_value=>-1} );
2337 # <input id="person_state_ids_hidden" name="person.state_ids[0]" type="hidden" value="-1"/>
2338 # <select id="person_state_ids" multiple name="person.state_ids[]">
2339 # <option selected value="1">user</option>
2340 # <option value="2">admin</option>
2341 # <option selected value="3">guest</option>
2342 # </select>
2343  
2344  
2345 If you've set the 'multiple' attribute to true, or we detect that multiple values are intended (either when the form value
2346 of the field is an arrayref or a collection) indicting your select drop list allows one to choose more than one option,
2347 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
2348 L<DBIx:Class::Valiant> then that code will automatically handle this for you. Otherwise you'll need to handle it
2349 manually or add code to detect that there is no form submission value under that name. If you don't want this
2350 behavior you can manually turn it off be explicitly setting 'include_hidden' to false.
2351  
2352 =back
2353  
2354 Optionally you can provide a \&template which should return C<option> tags. This coderef
2355 will recieve the $model, $attribute and an array of @selected values based on the $attribute.
2356  
2357 Examples:
2358  
2359 $fb->select('state_id', [1,2,3], +{class=>'foo'} );
2360 # <select class="foo" id="person_state_id" name="person.state_id">
2361 # <option selected value="1">1</option>
2362 # <option value="2">2</option>
2363 # <option value="3">3</option>
2364 # </select>
2365  
2366 $fb->select('state_id', [1,2,3], +{selected=>[3], disabled=>[1]} );
2367 # <select id="person_state_id" name="person.state_id">
2368 # <option disabled value="1">1</option>
2369 # <option value="2">2</option>
2370 # <option selected value="3">3</option>
2371 # </select>
2372  
2373 $fb->select('state_id', [map { [$_->name, $_->id] } $states_collection->all], +{include_blank=>1} );
2374 # <select id="person_state_id" name="person.state_id">
2375 # <option label=" " value=""></option>
2376 # <option selected value="1">TX</option>
2377 # <option value="2">NY</option>
2378 # <option value="3">CA</option>
2379 # </select>
2380  
2381 $fb->select('state_id', sub {
2382 my ($model, $attribute, $value) = @_;
2383 return map {
2384 my $selected = $_->id eq $value ? 1:0;
2385 option_tag($_->name, +{class=>'foo', selected=>$selected, value=>$_->id});
2386 } $states_collection->all;
2387 });
2388 # <select id="person_state_id" name="person.state_id">
2389 # <option class="foo" selected value="1">TX</option>
2390 # <option class="foo" value="2">NY</option>
2391 # <option class="foo" value="3">CA</option>
2392 # </select>
2393  
2394 Examples when $attribute is a collection:
2395  
2396 $fb->select({roles => 'id'}, [map { [$_->label, $_->id] } $roles_collection->all]),
2397 # <input id="person_roles_id_hidden" name="person.roles[0]._nop" type="hidden" value="1"/>
2398 # <select id="person_roles_id" multiple name="person.roles[].id">
2399 # <option selected value="1">user</option>
2400 # <option selected value="2">admin</option>
2401 # <option value="3">guest</option>
2402 # </select>
2403  
2404 Please note when the $attribute is a collection we add a hidden field to cope with case when
2405 no items are selected, you'll need to write form processing code to mark and notice the
2406 C<_nop> field.
2407  
2408 =head2 collection_select
2409  
2410 $fb->collection_select($attribute_proto, $collection, $value_method, $text_method, \%options);
2411 $fb->collection_select($attribute_proto, $collection, $value_method, $text_method);
2412 $fb->collection_select($attribute_proto, $collection);
2413 $fb->collection_select($attribute_proto, \%options);
2414  
2415 Where C<$attribute_proto> is one of:
2416  
2417 $attribute # A string which is an attribute on the underlying $model
2418 # that returns a scalar value.
2419 { $attribute => $method } # A hashref composed of an $attribute on the underlying $model
2420 # which returns a sub model or a collection of sub models
2421 # and a $method to be called on the value of that sub model (
2422 # or on each item sub model if the $attribute is a collection).
2423  
2424 Similar to L</select> but works with a $collection instead of delineated options. The collection can be
2425 an actual collection object, or the string name of a method on the model which provides the actual
2426 collection objection. Its a type of shortcut to reduce boilerplate at the expense of some flexibility (
2427 if you need that you'll need to use L</select>). Examples:
2428  
2429 $fb->collection_select('state_id', $states_collection, id=>'name');
2430 # <select id="person.state_id" name="person.state_id">
2431 # <option selected value="1">TX</option>
2432 # <option value="2">NY</option>
2433 # <option value="3">CA</option>
2434 # </select>
2435  
2436 $fb->collection_select('state_id', $states_collection, id=>'name', {class=>'foo', include_blank=>1});
2437 # <select class="foo" id="person.state_id" name="person.state_id">
2438 # <option label=" " value=""></option>
2439 # <option selected value="1">TX</option>
2440 # <option value="2">NY</option>
2441 # <option value="3">CA</option>
2442 # </select>
2443  
2444 $fb->collection_select('state_id', $states_collection, id=>'name', {selected=>[3], disabled=>[1]});
2445 # <select id="person.state_id" name="person.state_id">
2446 # <option disabled value="1">TX</option>
2447 # <option value="2">NY</option>
2448 # <option selected value="3">CA</option>
2449 # </select>
2450  
2451 is $fb->collection_select({roles => 'id'}, $roles_collection, id=>'label');
2452 # <input id="person_roles_id_hidden" name="person.roles[0]._nop" type="hidden" value="1"/>
2453 # <select id="person_roles_id" multiple name="person.roles[].id">
2454 # <option selected value="1">user</option>
2455 # <option selected value="2">admin</option>
2456 # <option value="3">guest</option>
2457 # </select>
2458  
2459 Please note when the $attribute is a collection value we add a hidden field to allow you to send a signal
2460 to the form processor that this namespace contains no records. Otherwise the form will just send
2461 nothing. If you have a custom way to handle this you can disable the behavior if you wish by explicitly
2462 setting include_hidden=>0
2463  
2464 \%options follows as from 'select' above.
2465  
2466 =head2 collection_checkbox
2467  
2468 $fb->collection_checkbox({$attribute=>$value_method}, $collection, $value_method, $text_method, \%options);
2469 $fb->collection_checkbox({$attribute=>$value_method}, $collection, $value_method, $text_method);
2470 $fb->collection_checkbox({$attribute=>$value_method}, $collection);
2471 $fb->collection_checkbox({$attribute=>$value_method}, $collection, $value_method, $text_method, \%options, \&template);
2472 $fb->collection_checkbox({$attribute=>$value_method}, $collection, $value_method, $text_method, \&template);
2473 $fb->collection_checkbox({$attribute=>$value_method}, $collection, \&template);
2474  
2475 Create a checkbox group for a collection attribute
2476  
2477 Examples:
2478  
2479 Where the $attribute C<roles> refers to a collection of sub models, each of which provides a method C<id>
2480 which is used to fetch a matching value and $roles_collection refers to the full set of available roles
2481 which can be added or removed from the parent model.
2482  
2483 In these examples C<$collection> and C<$roles_collection> can be either a collection object or a string
2484 which is the method name on the current model which provides the collection.
2485  
2486 $fb->collection_checkbox({roles => 'id'}, $roles_collection, id=>'label');
2487 # <div id="person_roles">
2488 # <input id="person_roles_hidden" name="person.roles" type="hidden" value="{'_nop':1}"/>
2489 # <label for="person_roles_1">user</label>
2490 # <input checked id="person_roles_1" name="person.roles" type="checkbox" value="{'id':1}"/>
2491 # <label for="person_roles_2">admin</label>
2492 # <input checked id="person_roles_2" name="person.roles" type="checkbox" value="{'id':2}"/>
2493 # <label for="person_roles_3">guest</label>
2494 # <input id="person_roles_3" name="person.roles" type="checkbox" value="{'id':3}"/>
2495 # </div>
2496  
2497 Please note when the $attribute is a collection value we add a hidden field to allow you to send a signal
2498 to the form processor that this namespace contains no records. Otherwise the form will just send
2499 nothing. If you have a custom way to handle this you can disable the behavior if you wish by explicitly
2500 setting include_hidden=>0
2501  
2502 If you have special needs for formatting or layout you can override the default template with a coderef
2503 that will receive a special type of formbuilder localized to the current value (an instance of
2504 L<Valiant::HTML::FormBuilder::Checkbox>):
2505  
2506 $fb->collection_checkbox({roles => 'id'}, $roles_collection, id=>'label', sub {
2507 my $fb_roles = shift;
2508 return $fb_roles->checkbox({class=>'form-check-input'}),
2509 $fb_roles->label({class=>'form-check-label'});
2510 });
2511  
2512 # <div id="person_roles">
2513 # <input id="person_roles_hidden" name="person.roles" type="hidden" value="{'_nop':1}"/>
2514 # <label for="person_roles_1" class="form-check-label">user</label>
2515 # <input checked class="form-check-input" id="person_roles_1" name="person.roles" type="checkbox" value="{'id':1}"/>
2516 # <label for="person_roles_2" class="form-check-label">admin</label>
2517 # <input checked class="form-check-input" id="person_roles_2" name="person.roles" type="checkbox" value="{'id':2}"/>
2518 # <label for="person_roles_3" class="form-check-label">guest</label>
2519 # <input class="form-check-input" id="person_roles_3" name="person.roles" type="checkbox" value="{'id':3}"/>
2520 # </div>
2521  
2522 In addition to overriding C<checkbox> and C<label> to already contain value and state (if its checked or
2523 not) information. This special builder contains some additional methods of possible use, you should see
2524 the documentation of L<Valiant::HTML::FormBuilder::Checkbox> for more.
2525  
2526 If provided C<%options> is a hashref of the following optional values
2527  
2528 =over 4
2529  
2530 =item include_hidden
2531  
2532 Defaults to whatever the method C<default_collection_checkbox_include_hidden> returns. In the core code
2533 this returns true. If true will include a hidden field set to the name of the collection, which is uses
2534 to indicate 'no checked values' since HTML will send nothing by default if there's no checked values. It
2535 will add this hidden field for each checkbox item to represent the 'none checked' value.
2536  
2537 =item builder
2538  
2539 Defaults to the values of C<DEFAULT_COLLECTION_CHECKBOX_BUILDER> method. In core code this is
2540 L<Valiant::HTML::FormBuilder::Checkbox>. Overide if you need to make a custom builder (tricky work).
2541  
2542 =back
2543  
2544 =head2 collection_radio_buttons
2545  
2546 $fb->collection_radio_buttons($attribute, $collection, $value_method, $text_method, \%options);
2547 $fb->collection_radio_buttons($attribute, $collection, $value_method, $text_method);
2548 $fb->collection_radio_buttons($attribute, $collection);
2549 $fb->collection_radio_buttons($attribute, $collection, $value_method, $text_method, \%options, \&template);
2550 $fb->collection_radio_buttons($attribute, $collection, $value_method, $text_method, \&template);
2551 $fb->collection_radio_buttons($attribute, $collection, \&template);
2552  
2553 A collection of radio buttons. Similar to L<\collection_checkbox> but used one only one value is
2554 permitted. Example:
2555  
2556 $fb->collection_radio_buttons('state_id', $states_collection, id=>'name');
2557 # <input id="person_state_id_hidden" name="person.state_id" type="hidden" value=""/>
2558 # <label for="person_state_id_1">TX</label>
2559 # <input checked id="person_state_id_1_1" name="person.state_id" type="radio" value="1"/>
2560 # <label for="person_state_id_2">NY</label>
2561 # <input id="person_state_id_2_2" name="person.state_id" type="radio" value="2"/>
2562 # <label for="person_state_id_3">CA</label>
2563 # <input id="person_state_id_3_3" name="person.state_id" type="radio" value="3"/>
2564  
2565 Please note when the $attribute is a collection value we add a hidden field to allow you to send a signal
2566 to the form processor that this namespace contains no records. Otherwise the form will just send
2567 nothing. If you have a custom way to handle this you can disable the behavior if you wish by explicitly
2568 setting include_hidden=>0
2569  
2570 If you have special needs for formatting or layout you can override the default template with a coderef
2571 that will receive a special type of formbuilder localized to the current value (an instance of
2572 L<Valiant::HTML::FormBuilder::RadioButton>):
2573  
2574 $fb->collection_radio_buttons('state_id', $states_collection, id=>'name', sub {
2575 my $fb_states = shift;
2576 return $fb_states->radio_button({class=>'form-check-input'}),
2577 $fb_states->label({class=>'form-check-label'});
2578 });
2579 # <div id='person_state_id'>
2580 # <input id="person_state_id_hidden" name="person.state_id" type="hidden" value=""/>
2581 # <input checked class="form-check-input" id="person_state_id_1_1" name="person.state_id" type="radio" value="1"/>
2582 # <label class="form-check-label" for="person_state_id_1">TX</label>
2583 # <input class="form-check-input" id="person_state_id_2_2" name="person.state_id" type="radio" value="2"/>
2584 # <label class="form-check-label" for="person_state_id_2">NY</label>
2585 # <input class="form-check-input" id="person_state_id_3_3" name="person.state_id" type="radio" value="3"/>
2586 # <label class="form-check-label" for="person_state_id_3">CA</label>
2587 # </div>
2588  
2589 In addition to overriding C<radio_button> and C<label> to already contain value and state (if its checked or
2590 not) information. This special builder contains some additional methods of possible use, you should see
2591 the documentation of L<Valiant::HTML::FormBuilder::RadioButton> for more.
2592  
2593 Please note that the generated radio inputs will be wrapped in a containing C<div> tag. You can change this tag
2594 using the C<container_tag> option. For example:
2595  
2596 $fb->collection_radio_buttons('state_id', $states_collection, id=>'name', +{container_tag=>'span'}, sub {
2597 my $fb_states = shift;
2598 return $fb_states->radio_button({class=>'form-check-input'}),
2599 $fb_states->label({class=>'form-check-label'});
2600 });
2601  
2602 Here's all the values for the '%options' argument. Any options that are not one of these will be passed to
2603 the container tag as html attributes:
2604  
2605 =over4
2606  
2607 =item checked_value
2608  
2609 This is the current value of the attribute. By default its the attribute value (via L<\tag_value_for_attribute>)
2610 but you can override as needed.
2611  
2612 =item include_hidden.
2613  
2614 Defaults to true. The value returned if you don't check one of the radio buttons.
2615  
2616 =item container_tag
2617  
2618 The tag that contains the generated radio buttons.
2619  
2620 =item builder
2621  
2622 The builder subclass used in the radio input generator.
2623  
2624 =back
2625  
2626 =head2 radio_buttons
2627  
2628 $fb->radio_buttons($attribute, \@options, \%options);
2629 $fb->radio_buttons($attribute, \@options, \%options, \&template);
2630 $fb->radio_buttons($attribute, \@options);
2631 $fb->radio_buttons($attribute, \@options, \&template);
2632  
2633 Similar to L</collection_radio_buttons> but takes an arrayref of label / values instead of a collection.
2634 Useful when you have a list of radio buttons (like from an ENUM) but you don't want to list each radio
2635 separately. Example:
2636  
2637 $fb_profile->radio_buttons('status', [[Pending=>'pending'],[Active=>'active'],[Inactive=>'inactive']]);
2638  
2639 # <input id="person_profile_status_hidden" name="person.profile.status" type="hidden" value="">
2640 # <input id="person_profile_status_pending_pending" name="person.profile.status" type="radio" value="pending">
2641 # <label for="person_profile_status_pending">Pending</label>
2642 # <input checked="" id="person_profile_status_active_actie" name="person.profile.status" type="radio" value="active">
2643 # <label for="person_profile_status_active">Active</label>
2644 # <input id="person_profile_status_inactive_inactive" name="person.profile.status" type="radio" value="inactive">
2645 # <label for="person_profile_status_inactive">Inactive</label>
2646  
2647 Supports using a template subroutine reference (like L</collection_radio_buttons>) when you need to be
2648 fussy about style and positioning.
2649  
2650 =head2 emit_hidden_ids
2651  
2652 $fb->emit_hidden_ids;
2653  
2654 If the model has C<in_storage> and C<primary_columns> then we will emit hidden fields for each of the
2655 primary columns. This is useful when you are editing an existing record and you want to make sure
2656 the form submission can be matched to the existing record. Example:
2657  
2658 $fb->emit_hidden_ids;
2659  
2660 # <input id="person_id" name="person.id" type="hidden" value="1"/>
2661  
2662 B<NOTE> In general you should not need to call this method directly. It is called automatically by
2663 the form builder when needed. However you might find you need tricky placement of the hidden fields
2664 (for example some tricky CSS) and in that case you can call this method manually in fields that allow
2665 you to disable the automatic placement of the hidden fields (for example in C<fields_for>, you can set
2666 the C<include_id> option to false and then call this method manually).
2667  
2668 =head1 REMOTE FORMS
2669  
2670 Remote forms are forms that submit via AJAX. They are a bit more complex than regular forms because
2671 they need to handle the AJAX response and update the DOM. The form builder has some support for
2672 remote forms but you will need to write some javascript to handle the response. Here's an example
2673  
2674 $fb->remote_form_for(sub {
2675 my $fb = shift;
2676 return $fb->input('name'),
2677 $fb->submit('Save');
2678 }, +{url=>'/person', method=>'POST', remote=>1, success=>'alert("Saved")'});
2679  
2680 See L<https://github.com/rails/jquery-ujs> for more information on how to handle the response.
2681 You can also look at the example application for more examples of how this works.
2682  
2683 When adding C<remote=1> to the options hashref we will automatically add the following attributes
2684 to the form tag:
2685  
2686 =over 4
2687  
2688 =item data-replace
2689  
2690 The ID of the element to replace with the response. If not provided we will replace the form itself.
2691  
2692 =back
2693  
2694 B<NOTE> Remote support is evolving and may change in the future. Please see the release notes for
2695 changes. I consider this a beta feature and will break compatibility if needed to fix bugs. Its
2696 also possible remote support might be moved to a separate module in the future.
2697  
2698 =head1 HELPERS
2699  
2700 The following methods don't make form controls but just useful methods to help you build your form.
2701  
2702 =head2 escape_javascript
2703  
2704 $fb->escape_javascript($string);
2705  
2706 Escapes a string so it can be used in a javascript string. This is a wrapper around
2707 The same method from L<Valiant::HTML::Util::TagBuilder/escape_javascript>.
2708  
2709 Basically this escapes ' and " and \ and newlines and a few other neaten up so that you can
2710 use a string as a javascript value. Helps with injection attackes (but isn't everything
2711 you need).
2712  
2713  
2714 =head1 THEMING
2715  
2716 You can add a method called C<default_theme> to your custom form builder sub class to return a hashref of default
2717 attributes for the various form elements. For example:
2718  
2719 package Example::FormBuilder;
2720  
2721 use Moo;
2722 use Example::Syntax;
2723  
2724 extends 'Valiant::HTML::FormBuilder';
2725  
2726 sub default_theme($self) {
2727 return +{
2728 errors_for => +{ class=>'invalid-feedback' },
2729 label => +{ class=>'form-label' },
2730 input => +{ class=>'form-control', errors_classes=>'is-invalid' },
2731 date_field => +{ class=>'form-control', errors_classes=>'is-invalid' },
2732 password => +{ class=>'form-control', errors_classes=>'is-invalid' },
2733 submit => +{ class=>'btn btn-lg btn-success btn-block' },
2734 button => +{ class=>'btn btn-lg btn-primary btn-block' },
2735 text_area => +{ class=>'form-control' },
2736 checkbox => +{ class=>'form-check-input', errors_classes=>'is-invalid' },
2737 collection_radio_buttons => +{errors_classes=>'is-invalid'},
2738 collection_checkbox => +{errors_classes=>'is-invalid'},
2739 collection_select => +{class=>'form-control', errors_classes=>'is-invalid'},
2740 select => +{class=>'form-control', errors_classes=>'is-invalid'},
2741 radio_buttons => +{errors_classes=>'is-invalid'},
2742 radio_button => +{class=>'custom-control-input', errors_classes=>'is-invalid'},
2743 model_errors => +{ class=>'alert alert-danger', role=>'alert' },
2744 form_has_errors => +{ class=>'alert alert-danger', role=>'alert' },
2745 attributes => {
2746 password => {
2747 password => { autocomplete=>'new-password' }
2748 }
2749 },
2750 };
2751 }
2752  
2753 In the above example you set class defaults for most of the form elements based on the method name. In
2754 addition you can set specific attributes for specific model attributes (as in the 'password' attribute
2755 at the end of the last example.
2756  
2757 Then you you call a method like C<text_field> it will automatically add the default attributes for that
2758 element. For example:
2759  
2760 $fb->input('name');
2761 # <input class="form-control" id="person_name" name="person.name" type="text" value="">
2762  
2763 If you are using a CSS framework you can use this to setup default (but overridable) classes for the various
2764 form elements. For example:
2765  
2766 $fb->input('name', {class=>'form-control form-control-lg'});
2767 # <input class="form-control form-control-lg" id="person_name" name="person.name" type="text" value="">
2768  
2769 You can instead add a method called C<formbuilder_theme> to your view class which does the same thing but
2770 allows you to make local view specific customatizations. If you use both the formbuilder default theme is
2771 added first and the view theme would override it.
2772  
2773 Please note that you are not limited to passing HTML attributes here, you an pass anything that is a valid
2774 attribute for the form field you are generating.
2775  
2776 B<NOTE:> I'm still working out some of the rules around how we merge or override the various themes so if you
2777 go wild here you will need to follow the release notes for following versions carefully. I consider theming
2778 a beta feature subject to breaking changes if that's what I need to do to fix bugs or make it more flexible.
2779  
2780 1;
2781  
2782 =head1 SEE ALSO
2783  
2784 L<Valiant>
2785  
2786 =head1 AUTHOR
2787
2788 See L<Valiant>
2789  
2790 =head1 COPYRIGHT & LICENSE
2791
2792 See L<Valiant>
2793  
2794 =cut
« Catalyst/View/Valiant/HTMLBuilder.pm ↑ Summary Valiant/Validator/Collection.pm »