lib/HTML/FormFu/Role/Element/Input.pm

filter: Statementstmt 94.6% 123 / 130 Branchbran 65.3% 34 / 52 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 21 / 21 Podpod n/a totaltotal 86.0% 179 / 208 SCAR 35.6
CC
32
Cov
85%
__ANON__
10.1
_parse_option_hashref
9.2
datalist_options
6.3
CRAP 35.2
« lib/HTML/FormFu/Element/Repeatable.pm ↑ Summary lib/HTML/FormFu/Element/Checkboxgroup.pm »
1 322 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Role::Element::Input;
4 322 $HTML::FormFu::Role::Element::Input::VERSION = '2.06';
5 # ABSTRACT: Role for input fields
6  
7 322 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 322 with 'HTML::FormFu::Role::Element::Field',
10 'HTML::FormFu::Role::Element::FieldMethods' =>
11 { -excludes => 'nested_name' },
12 'HTML::FormFu::Role::Element::Coercible';
13  
14 322 ▶use HTML::FormFu::Util qw( literal xml_escape );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 322 ▶use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 322 ▶use List::Util 1.33 qw( none );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 322 ▶use Scalar::Util qw( reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 322 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 322 use HTML::FormFu::Attribute qw(
21 mk_attr_accessors
22 mk_attr_bool_accessors
23 322 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 322 ▶use HTML::FormFu::Constants qw( $EMPTY_STR );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 322 ▶use HTML::FormFu::Util qw( process_attrs xml_escape );
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 322 has field_type => ( is => 'rw', );
28  
29 322 has datalist_id => ( is => 'rw' );
30  
31 has _datalist_options => (
32 is => 'rw',
33 417 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 322 lazy => 1,
35 isa => 'ArrayRef',
36 );
37  
38 322 __PACKAGE__->mk_attr_accessors( qw(
39 alt autocomplete
40 checked maxlength
41 pattern placeholder
42 size
43 ) );
44  
45 322 __PACKAGE__->mk_attr_bool_accessors( qw(
46 autofocus
47 multiple
48 required
49 ) );
50  
51 322 my @ALLOWED_OPTION_KEYS = qw(
52 value
53 value_xml
54 value_loc
55 label
56 label_xml
57 label_loc
58 );
59  
60 sub datalist_options {
61 1 ▶ my ( $self, $arg ) = @_;
Subroutine datalist_options: called CC=6 SCAR=18.5
62 1 my ( @options, @new );
63  
64 1 ▶ return $self->_datalist_options if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 1 1
65  
66 1 ▶ croak "datalist_options argument must be a single array-ref" if @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 1 1
67  
68 1 ▶ if ( defined $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) 1 0 1
69 1 ▶ croak "datalist_options argument must be an array-ref"
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($arg) ne "ARRAY" 0 1 1
70 if reftype($arg) ne 'ARRAY';
71  
72 1 @options = @$arg;
73  
74 1 for my $item (@options) {
75 4 push @new, $self->_parse_option($item);
76 }
77 }
78  
79 1 $self->_datalist_options( \@new );
80  
81 1 return $self;
82 }
83  
84 sub _parse_option {
85 4 ▶ my ( $self, $item ) = @_;
Subroutine _parse_option: called CC=3 SCAR=11.3
86  
87 4 ▶ if ( reftype($item) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::reftype($item) eq 'HASH') { } 2 2 4
elsif (builtin::reftype($item) eq 'ARRAY') { } 2 0 2
88 2 return $self->_parse_option_hashref($item);
89 }
90 elsif ( reftype($item) eq 'ARRAY' ) {
91 return {
92 2 value => $item->[0],
93 label => $item->[1],
94 };
95 }
96 else {
97 0 croak "each datalist_options argument must be a hash-ref or array-ref";
98 }
99 }
100  
101 sub _parse_option_hashref {
102 2 ▶ my ( $self, $item ) = @_;
Subroutine _parse_option_hashref: called CC=8 SCAR=22.2
103  
104 # sanity check options
105 2 my @keys = keys %$item;
106  
107 2 for my $key (@keys) {
108 croak "unknown option argument: '$key'"
109 4 ▶ if none { $key eq $_ } @ALLOWED_OPTION_KEYS;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $key eq $_; } , @ALLOWED_OPTION_KEYS) 0 4 4
110 }
111  
112 2 ▶ if ( defined $item->{label_xml} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $item->{'label_xml'}) { } 0 2 2
elsif (defined $item->{'label_loc'}) { } 1 1 2
113 0 $item->{label} = literal( $item->{label_xml} );
114 }
115 elsif ( defined $item->{label_loc} ) {
116 1 $item->{label} = $self->form->localize( $item->{label_loc} );
117 }
118  
119 2 ▶ if ( defined $item->{value_xml} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $item->{'value_xml'}) { } 1 1 2
elsif (defined $item->{'value_loc'}) { } 0 1 1
120 1 $item->{value} = literal( $item->{value_xml} );
121 }
122 elsif ( defined $item->{value_loc} ) {
123 0 $item->{value} = $self->form->localize( $item->{value_loc} );
124 }
125  
126 2 ▶ if ( !defined $item->{value} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $item->{"value"}) 0 2 2
127 0 $item->{value} = '';
128 }
129  
130 2 return $item;
131 }
132  
133 sub datalist_values {
134 1 ▶ my ( $self, $arg ) = @_;
Subroutine datalist_values: called CC=4 SCAR=14.1
135  
136 1 ▶ croak "datalist_values argument must be a single array-ref of values"
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 1 1
137 if @_ > 2;
138  
139 1 my @values;
140  
141 1 ▶ if ( defined $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) 1 0 1
142 1 ▶ croak "datalist_values argument must be an array-ref"
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($arg) ne "ARRAY" 0 1 1
143 if reftype($arg) ne 'ARRAY';
144  
145 1 @values = @$arg;
146 }
147  
148 1 my @new = map { { value => $_, label => ucfirst $_, } } @values;
149  
150 1 $self->_datalist_options( \@new );
151  
152 1 return $self;
153 }
154  
155 around prepare_id => sub {
156 734 ▶ my ( $orig, $self, $render ) = @_;
Subroutine __ANON__: called CC=8 SCAR=23.2
157  
158 734 $self->$orig($render);
159  
160 734 ▶ return if !@{ $self->_datalist_options };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->_datalist_options;} 732 2 734
161  
162 2 ▶ if ( defined $render->{datalist_id} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $render->{'datalist_id'}) { } 0 2 2
elsif (defined $self->auto_datalist_id and length $self->auto_datalist_id) { } 2 0 2
163 0 $render->{attributes}{list} = $render->{datalist_id};
164 }
165 ▶ elsif ( defined $self->auto_datalist_id
Condition33%1/3
defined $self->auto_datalist_id and length $self->auto_datalist_id
!ll&&!rl&&r
002
MC/DC0%0/2
defined $self->auto_datalist_id and length $self->auto_datalist_id
defined $self->auto_datalist_id length $self->auto_datalist_id
Truth table33%1/3
A and B
A= defined $self->auto_datalist_idB= length $self->auto_datalist_id
ABresult
0X0
100
111
166 && length $self->auto_datalist_id )
167 {
168 2 ▶ my $form_name
Branch: 1 branch
BranchTrueFalseTotal
defined $self->form->id ? : 2 0 2
169 = defined $self->form->id
170 ? $self->form->id
171 : $EMPTY_STR;
172  
173 my $field_name
174 = defined $render->{nested_name}
175 ? $render->{nested_name}
176 2 ▶ : $EMPTY_STR;
Branch: 1 branch
BranchTrueFalseTotal
defined $render->{'nested_name'} ? : 2 0 2
177  
178 2 my %string = (
179 f => $form_name,
180 n => $field_name,
181 );
182  
183 2 my $id = $self->auto_datalist_id;
184 2 $id =~ s/%([fn])/$string{$1}/g;
185  
186 2 ▶ if ( defined( my $count = $self->repeatable_count ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $count = $self->repeatable_count)) 0 2 2
187 0 $id =~ s/%r/$count/g;
188 }
189  
190 2 $render->{attributes}{list} = $id;
191 }
192 else {
193 0 croak
194 "either 'datalist_id' or 'auto_datalist_id' must be set when using a datalist";
195 }
196  
197 2 return;
198 322 };
199  
200 around render_data_non_recursive => sub {
201 734 ▶ my ( $orig, $self, $args ) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
202  
203 734 ▶ my $render = $self->$orig(
Branch: 1 branch
BranchTrueFalseTotal
$args ? : 0 734 734
204 { field_type => $self->field_type,
205 placeholder => $self->placeholder,
206 error_attributes => xml_escape( $self->error_attributes ),
207 error_container_attributes => xml_escape( $self->error_attributes ),
208 $args ? %$args : (),
209 } );
210  
211 734 ▶ if ( @{ $self->_datalist_options } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->_datalist_options;}) 2 732 734
212 2 $render->{datalist_options} = Clone::clone( $self->_datalist_options );
213 }
214  
215 734 $self->_quote_options( $render->{datalist_options} );
216  
217 734 return $render;
218 322 };
219  
220 sub _quote_options {
221 734 ▶ my ( $self, $options ) = @_;
Subroutine _quote_options: called CC=2 SCAR=6.9
222  
223 734 foreach my $opt (@$options) {
224 7 $opt->{label} = xml_escape( $opt->{label} );
225 7 $opt->{value} = xml_escape( $opt->{value} );
226 }
227 }
228  
229 sub _string_field {
230 581 ▶ my ( $self, $render ) = @_;
Subroutine _string_field: called CC=5 SCAR=16.1
231  
232 581 my $html = "";
233  
234 581 ▶ if ( $render->{datalist_options} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($render->{"datalist_options"}) 2 579 581
235 2 $html .= sprintf qq{<datalist id="%s">\n}, $render->{attributes}{list};
236 2 for my $option ( @{ $render->{datalist_options} } ) {
237 $html .= sprintf qq{<option value="%s">%s</option>\n},
238 $option->{value},
239 7 $option->{label};
240 }
241 2 $html .= sprintf qq{</datalist>\n};
242 }
243  
244 581 $html .= "<input";
245  
246 581 ▶ if ( defined $render->{nested_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $render->{"nested_name"}) 578 3 581
247 578 $html .= sprintf qq{ name="%s"}, $render->{nested_name};
248 }
249  
250 581 $html .= sprintf qq{ type="%s"}, $render->{field_type};
251  
252 581 ▶ if ( defined $render->{value} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $render->{"value"}) 358 223 581
253 358 $html .= sprintf qq{ value="%s"}, $render->{value};
254 }
255  
256 581 $html .= sprintf "%s />", process_attrs( $render->{attributes} );
257  
258 581 return $html;
259 }
260  
261 around clone => sub {
262 171 ▶ my ( $orig, $self ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
263  
264 171 my $clone = $self->$orig(@_);
265  
266 171 $clone->_datalist_options( Clone::clone( $self->_datalist_options ) );
267  
268 171 return $clone;
269 322 };
270  
271 322 1;
272  
273 __END__
274  
275 =pod
276  
277 =encoding UTF-8
278  
279 =head1 NAME
280  
281 HTML::FormFu::Role::Element::Input - Role for input fields
282  
283 =head1 VERSION
284  
285 version 2.06
286  
287 =head1 DESCRIPTION
288  
289 Base-class for L<HTML::FormFu::Element::Button>,
290 L<HTML::FormFu::Element::Checkbox>,
291 L<HTML::FormFu::Element::File>,
292 L<HTML::FormFu::Element::Hidden>,
293 L<HTML::FormFu::Element::Password>,
294 L<HTML::FormFu::Element::Radio>,
295 L<HTML::FormFu::Element::Text>.
296  
297 =head1 METHODS
298  
299 =head2 datalist_options
300  
301 Arguments: none
302  
303 Arguments: \@options
304  
305 Use either L</datalist_options> or L</datalist_values> to generate a
306 HTML5-compatible C<datalist> group of C<option> tags. This will be associated
307 with the C<input> element via a C<list> attribute on the C<input> tag.
308  
309 The C<datalist> ID attribute B<must> be set using either L</datalist_id>
310 or L</auto_datalist_id>.
311  
312 ---
313 elements:
314 - type: Text
315 name: foo
316 options:
317 - [ 01, January ]
318 - [ 02, February ]
319 - [ 03, March ]
320 - [ 04, April ]
321  
322 The syntax is similar to L<HTML::FormFu::Role::Element::Group/options>,
323 except hash-ref items only accept C<value> and C<label> keys (and their variants).
324  
325 If passed no arguments, it returns an arrayref of the currently set datalist options.
326  
327 Its arguments must be an array-ref of items. Each item may be an array ref
328 of the form C<[ $value, $label ]> or a hash-ref of the form
329 C<< { value => $value, label => $label } >>.
330  
331 When using the hash-ref construct, the C<label_xml> and C<label_loc>
332 variants of C<label> are supported, as are the C<value_xml> and C<value_loc>
333 variants of C<value>.
334  
335 =head2 datalist_values
336  
337 Arguments: \@values
338  
339 ---
340 elements:
341 - type: Radiogroup
342 name: foo
343 values:
344 - jan
345 - feb
346 - mar
347 - apr
348  
349 A more concise alternative to L</datalist_options>.
350  
351 Its arguments must be an array-ref of values. The labels used are the
352 result of C<ucfirst($value)>.
353  
354 =head2 datalist_id
355  
356 Arguments: [$string]
357  
358 Sets the C<datalist> ID attribute, and automatically sets this C<input> element's
359 C<list> ID to the same.
360  
361 Either L</datalist_id> or L</auto_datalist_id> is required,
362 if either L</datalist_options> or L</datalist_values> are set.
363  
364 =head2 auto_datalist_id
365  
366 See L<HTML::FormFu/auto_datalist_id> for details.
367  
368 =head1 ATTRIBUTE ACCESSORS
369  
370 Get / set input attributes directly with these methods.
371  
372 Arguments: [$string]
373  
374 Return Value: $string
375  
376 =head2 alt
377  
378 =head2 autocomplete
379  
380 =head2 checked
381  
382 =head2 maxlength
383  
384 =head2 pattern
385  
386 =head2 placeholder
387  
388 =head2 size
389  
390 =head1 BOOLEAN ATTRIBUTE ACCESSORS
391  
392 Arguments: [$bool]
393  
394 Return Value: $self
395 Return Value: $string
396 Return Value: undef
397  
398 Get / set boolean XHTML attributes such as C<required="required">.
399  
400 If given any true argument, the attribute value will be set equal to the attribute
401 key name. E.g. C<< $element->required(1) >> will set the attribute C<< required="required" >>.
402  
403 If given a false argument, the attribute key will be deleted.
404  
405 When used as a setter, the return value is C<< $self >> to allow chaining.
406  
407 =head2 autofocus
408  
409 =head2 multiple
410  
411 =head2 required
412  
413 =head1 SEE ALSO
414  
415 Is a sub-class of, and inherits methods from
416 L<HTML::FormFu::Role::Element::Field>, L<HTML::FormFu::Element>
417  
418 L<HTML::FormFu>
419  
420 =head1 AUTHOR
421  
422 Carl Franks, C<cfranks@cpan.org>
423  
424 =head1 LICENSE
425  
426 This library is free software, you can redistribute it and/or modify it under
427 the same terms as Perl itself.
428  
429 =head1 AUTHOR
430  
431 Carl Franks <cpan@fireartist.com>
432  
433 =head1 COPYRIGHT AND LICENSE
434  
435 This software is copyright (c) 2018 by Carl Franks.
436  
437 This is free software; you can redistribute it and/or modify it under
438 the same terms as the Perl 5 programming language system itself.
439  
440 =cut
« lib/HTML/FormFu/Element/Repeatable.pm ↑ Summary lib/HTML/FormFu/Element/Checkboxgroup.pm »