Valiant/HTML/Util/FormTags.pm

filter: Statementstmt 97.7% 266 / 272 Branchbran 87.1% 176 / 202 Conditioncond 81.0% 81 / 100 MC/DCmcdc 60.3% 32 / 53 Subroutinesub 100% 36 / 36 Podpod 77.2% 17 / 22 totaltotal 88.7% 608 / 685 CC 147 SCAR 22.9
CC
147
Cov
91%
_merge_attrs
25.2
options_from_collection_for_select
19.1
select_tag
13.5
CRAP 9.9
« Valiant/HTML/Tag/Label.pm ↑ Summary Valiant/I18N.pm »
1 package Valiant::HTML::Util::FormTags;
2  
3 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Valiant::HTML::Util::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 extends 'Valiant::HTML::Util::TagBuilder';
9  
10 4 our $DEFAULT_BUTTON_CONTENT = 'Button';
11 4 our $DEFAULT_SUBMIT_TAG_VALUE = 'Save changes';
12 4 our $DEFAULT_OPTIONS_DELIM = '';
13  
14 # private
15  
16 sub _sanitize_to_id {
17 79 ▶ my ($self, $value) = @_;
Subroutine _sanitize_to_id: called CC=2 SCAR=7.0
18 79 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 79 79
19 79 $value =~ s/\]//g;
20 79 $value =~ s/[^a-zA-Z0-9:.-]/_/g;
21 79 return $value;
22 }
23  
24 sub _humanize {
25 44 ▶ my ($self, $value) = @_;
Subroutine _humanize: called CC=2 SCAR=7.0
26 44 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 44 44
27 44 $value =~s/_id$//; # remove trailing _id
28 44 $value =~s/_/ /g;
29 44 return ucfirst($value);
30 }
31  
32 sub _prepend_block {
33 4 ▶ my ($self, $block, @bits) = @_;
Subroutine _prepend_block: called CC=1 SCAR=0.0
34 4 ▶ return sub { return @bits, $block->(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
35 }
36  
37 # _merge_attrs does a smart merge of two hashrefs that represent HTML tag attributes. This
38 # needs special processing since we need to merge 'data' and 'class' attributes with special
39 # rules. For merging in general key/values in the second hashref will override those in the
40 # first (unless its a special attributes like 'data' or 'class'
41  
42 sub _merge_attrs {
43 334 ▶ my ($self, $attrs1, $attrs2, @list) = @_;
Subroutine _merge_attrs: called CC=19 SCAR=32.3
44 334 ▶ my $overide = @list ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
@list ? : 57 277 334
45 334 ▶ @list = keys %{$attrs2||{}} unless @list;
Branch: 2 branches
BranchTrueFalseTotal
unless @list 277 57 334
$attrs2 or {} 0 277 277
46 334 foreach my $key (@list) {
47 1168 ▶ next unless exists $attrs2->{$key}; # Don't create from @list, don't bother if not existing
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attrs2->{$key} 250 918 1168
48 918 ▶ if( ($key eq 'data') || ($key eq 'aria')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'data' or $key eq 'aria') { } 0 918 918
elsif ($key eq 'class' or $key eq 'style') { } 90 828 918
Condition33%1/3
$key eq "data" or $key eq "aria"
l!l&&r!l&&!r
00918
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
49 0 ▶ my $data1 = exists($attrs1->{$key}) ? $attrs1->{$key} : +{};
Branch: 1 branch
BranchTrueFalseTotal
exists $attrs1->{$key} ? : 0 0 0
50 0 ▶ my $data2 = exists($attrs2->{$key}) ? $attrs2->{$key} : +{};
Branch: 1 branch
BranchTrueFalseTotal
exists $attrs2->{$key} ? : 0 0 0
51 0 $attrs1->{$key} = +{ %$data1, %$data2 };
52 ▶ } elsif($key eq 'class' || $key eq 'style') {
Condition66%2/3
$key eq "class" or $key eq "style"
l!l&&r!l&&!r
900828
MC/DC50%1/2
$key eq "class" or $key eq "style"
$key eq "class" $key eq "style"
Truth table66%2/3
A or B
A= $key eq "class"B= $key eq "style"
ABresult
1X1
011
000
53 90 my $data = $attrs2->{$key};
54 90 ▶ my @data = ref($data) ? @$data : ($data);
Branch: 1 branch
BranchTrueFalseTotal
ref $data ? : 1 89 90
55 90 ▶ if(exists $attrs1->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $attrs1->{$key}) { } 2 88 90
56 2 ▶ if(ref $attrs1->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attrs1->{$key}) { } 1 1 2
57 1 push @{$attrs1->{$key}}, @data;
58 } else {
59 1 $attrs1->{$key} = [$attrs1->{$key}, @data];
60 }
61 } else {
62 88 ▶ $attrs1->{$key} = $attrs2->{$key} if($overide || !exists $attrs1->{$key});
Branch: 1 branch
BranchTrueFalseTotal
if $overide or not exists $attrs1->{$key} 88 0 88
Condition33%1/3
$overide or not exists $attrs1->{$key}
l!l&&r!l&&!r
8800
MC/DC0%0/2
$overide or not exists $attrs1->{$key}
$overide not exists $attrs1->{$key}
Truth table33%1/3
A or not(B)
A= $overideB= not exists $attrs1->{$key}
ABresult
1X1
011
000
63 }
64 } else {
65 828 ▶ $attrs1->{$key} = $attrs2->{$key} if($overide || !exists $attrs1->{$key});
Branch: 1 branch
BranchTrueFalseTotal
if $overide or not exists $attrs1->{$key} 826 2 828
Condition100%3/3
$overide or not exists $attrs1->{$key}
l!l&&r!l&&!r
782442
MC/DC100%2/2
$overide or not exists $attrs1->{$key}
$overide not exists $attrs1->{$key}
Truth table100%3/3
A or not(B)
A= $overideB= not exists $attrs1->{$key}
ABresult
1X1
011
000
66 }
67 }
68 334 return $attrs1;
69 }
70  
71 # public
72  
73 sub array_to_collection {
74 7 ▶ my $self = shift;
Subroutine array_to_collection: called CC=1 SCAR=0.0
Pod documented
75 7 return Valiant::HTML::Util::Collection->new(@_);
76 }
77  
78 sub button_tag {
79 14 ▶ my ($self, $content, $attrs) = (shift(), undef, +{});
Subroutine button_tag: called CC=9 SCAR=22.0
Pod documented
80  
81 14 ▶ $attrs = shift @_ if (ref($_[0])||'') eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] || "") eq "HASH" 2 12 14
Condition100%2/2
ref $_[0] || ''
l!l
311
MC/DC100%1/1
ref $_[0] || ''
ref $_[0]
Truth table100%2/2
A
A= ref $_[0]
Aresult
11
00
82 14 ▶ $content = shift @_ if (ref($_[0])||'') eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] || "") eq "CODE" 3 11 14
Condition100%2/2
ref $_[0] || ''
l!l
311
MC/DC100%1/1
ref $_[0] || ''
ref $_[0]
Truth table100%2/2
A
A= ref $_[0]
Aresult
11
00
83 14 ▶ $content = shift @_ unless defined $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 11 3 14
84 14 ▶ $content = $DEFAULT_BUTTON_CONTENT unless defined $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 3 11 14
85 14 ▶ $attrs = shift @_ if (ref($_[0])||'') eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] || "") eq "HASH" 9 5 14
Condition100%2/2
ref $_[0] || ''
l!l
95
MC/DC100%1/1
ref $_[0] || ''
ref $_[0]
Truth table100%2/2
A
A= ref $_[0]
Aresult
11
00
86 14 $attrs = $self->_merge_attrs(+{name => 'button'}, $attrs);
87  
88 14 return $self->tags->button($attrs, $content);
89 }
90  
91 sub checkbox_tag {
92 26 ▶ my $self = shift;
Subroutine checkbox_tag: called CC=6 SCAR=17.9
Pod documented
93 26 my $name = shift;
94 26 my ($value, $checked, $attrs) = (1, 0, +{});
95 26 ▶ $attrs = pop(@_) if (ref($_[-1])||'') eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[-1] || "") eq "HASH" 22 4 26
Condition100%2/2
ref $_[-1] || ''
l!l
224
MC/DC100%1/1
ref $_[-1] || ''
ref $_[-1]
Truth table100%2/2
A
A= ref $_[-1]
Aresult
11
00
96 26 ▶ $value = shift(@_) if defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 20 6 26
97 26 ▶ $checked = shift(@_) if defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 19 7 26
98 26 ▶ $attrs->{checked} = 1 if $checked;
Branch: 1 branch
BranchTrueFalseTotal
if $checked 11 15 26
99 26 $attrs = $self->_merge_attrs(
100 +{type => 'checkbox', name=>$name, id=>$self->_sanitize_to_id($name), value=>$value},
101 $attrs
102 );
103 26 return $self->tags->input($attrs);
104 }
105  
106 sub fieldset_tag {
107 4 ▶ my $self = shift;
Subroutine fieldset_tag: called CC=5 SCAR=16.1
Pod documented
108 4 my ($legend, $attrs, $content) = (undef, +{}, undef);
109 4 $content = pop @_; # Required
110 4 ▶ $attrs = pop @_ if (ref($_[-1])||'') eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[-1] || "") eq "HASH" 2 2 4
Condition100%2/2
ref $_[-1] || ''
l!l
22
MC/DC100%1/1
ref $_[-1] || ''
ref $_[-1]
Truth table100%2/2
A
A= ref $_[-1]
Aresult
11
00
111 4 ▶ $legend = shift @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 2 4
112  
113 4 ▶ my $block = $legend ? $self->_prepend_block($content, $self->legend_tag($legend)) : $content;
Branch: 1 branch
BranchTrueFalseTotal
$legend ? : 2 2 4
114  
115 4 return $self->tags->fieldset($attrs, $block);
116 }
117  
118 # $tb->form_tag($url_info, \%attrs, \&content);
119 # $tb->form_tag($url_info, \&content);
120  
121 sub form_tag {
122 14 ▶ my $self = shift;
Subroutine form_tag: called CC=4 SCAR=14.0
Pod documented
123 14 my $content = pop @_; # required
124 14 ▶ my $attrs = ref($_[-1])||'' eq 'HASH' ? pop(@_) : +{};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] || !1 ? : 14 0 14
Condition50%1/2
ref $_[-1] or
l!l
140
MC/DC0%0/1
ref $_[-1] or
ref $_[-1]
Truth table50%1/2
A
A= ref $_[-1]
Aresult
11
00
125 14 ▶ my $url_info = @_ ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 3 11 14
126 14 $attrs = $self->_process_form_attrs($url_info, $attrs);
127 14 $content = $self->_process_content($content, $attrs);
128  
129 14 return $self->tags->form($attrs, $content);
130 }
131  
132 sub _process_form_attrs {
133 14 ▶ my ($self, $url_info, $attrs) = @_;
Subroutine _process_form_attrs: called CC=5 SCAR=16.2
134 14 ▶ $attrs->{action} = $url_info if $url_info;
Branch: 1 branch
BranchTrueFalseTotal
if $url_info 3 11 14
135 14 ▶ $attrs->{method} ||= 'post';
Condition100%2/2
$attrs->{"method"} ||= "post"
l!l
131
MC/DC100%1/1
$attrs->{"method"} ||= "post"
$attrs->{"method"}
Truth table100%2/2
A
A= $attrs->{"method"}
Aresult
11
00
136 14 ▶ $attrs->{'accept-charset'} ||= 'UTF-8';
Condition50%1/2
$attrs->{"accept-charset"} ||= "UTF-8"
l!l
014
MC/DC0%0/1
$attrs->{"accept-charset"} ||= "UTF-8"
$attrs->{"accept-charset"}
Truth table50%1/2
A
A= $attrs->{"accept-charset"}
Aresult
11
00
137 14 ▶ $attrs->{enctype} ||= 'application/x-www-form-urlencoded';
Condition50%1/2
$attrs->{"enctype"} ||= "application/x-www-form-urlencoded"
l!l
014
MC/DC0%0/1
$attrs->{"enctype"} ||= "application/x-www-form-urlencoded"
$attrs->{"enctype"}
Truth table50%1/2
A
A= $attrs->{"enctype"}
Aresult
11
00
138 14 $self->_process_method($attrs);
139  
140 14 return $attrs;
141 }
142  
143 sub _process_method {
144 14 ▶ my ($self, $attrs) = @_;
Subroutine _process_method: called CC=7 SCAR=19.9
145 14 ▶ return unless delete $attrs->{tunneled_method};
Branch: 1 branch
BranchTrueFalseTotal
unless delete $attrs->{"tunneled_method"} 2 12 14
146 12 ▶ return if (lc($attrs->{method}) eq 'post') || (lc($attrs->{method}) eq 'get');
Branch: 1 branch
BranchTrueFalseTotal
if lc $attrs->{"method"} eq "post" or lc $attrs->{"method"} eq "get" 7 5 12
Condition66%2/3
lc $attrs->{"method"} eq "post" or lc $attrs->{"method"} eq "get"
l!l&&r!l&&!r
705
MC/DC50%1/2
lc $attrs->{"method"} eq "post" or lc $attrs->{"method"} eq "get"
lc $attrs->{"method"} eq "post" lc $attrs->{"method"} eq "get"
Truth table66%2/3
A or B
A= lc $attrs->{"method"} eq "post"B= lc $attrs->{"method"} eq "get"
ABresult
1X1
011
000
147  
148 5 ▶ my $uri = Scalar::Util::blessed($attrs->{action}||'') ? $attrs->{action} : URI->new( $attrs->{action}||'');
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($attrs->{'action'} || '') ? : 0 5 5
Condition50%2/4
$attrs->{'action'} || ''
l!l
50
$attrs->{'action'} || ''
l!l
50
MC/DC0%0/1
$attrs->{'action'} || ''
$attrs->{'action'}
MC/DC0%0/1
$attrs->{'action'} || ''
$attrs->{'action'}
Truth table50%1/2
A
A= $attrs->{'action'}
Aresult
11
00
Truth table50%1/2
A
A= $attrs->{'action'}
Aresult
11
00
149 5 my $params = $uri->query_form_hash;
150  
151 5 $params->{'x-tunneled-method'} = $attrs->{method};
152 5 $uri->query_form($params);
153 5 $attrs->{action} = $uri;
154 5 $attrs->{method} = 'post';
155 }
156  
157 sub _process_content {
158 14 ▶ my ($self, $content, $attrs) = @_;
Subroutine _process_content: called CC=2 SCAR=6.9
159 14 ▶ return $content unless my $csrf_token = delete $attrs->{csrf_token};
Branch: 1 branch
BranchTrueFalseTotal
unless my $csrf_token = delete $attrs->{"csrf_token"} 12 2 14
160 2 return $self->_prepend_block($content, $self->hidden_tag('csrf_token', $csrf_token));
161 }
162  
163 sub label_tag {
164 37 ▶ my ($self, $name) = (shift(), shift());
Subroutine label_tag: called CC=8 SCAR=20.9
Pod documented
165 37 my ($content, $attrs) = ($self->_humanize($name), +{});
166  
167 37 ▶ $content = pop @_ if (ref($_[-1])||'') eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[-1] || "") eq "CODE" 4 33 37
Condition100%2/2
ref $_[-1] || ''
l!l
352
MC/DC100%1/1
ref $_[-1] || ''
ref $_[-1]
Truth table100%2/2
A
A= ref $_[-1]
Aresult
11
00
168 37 ▶ $attrs = pop @_ if (ref($_[-1])||'') eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[-1] || "") eq "HASH" 34 3 37
Condition100%2/2
ref $_[-1] || ''
l!l
343
MC/DC100%1/1
ref $_[-1] || ''
ref $_[-1]
Truth table100%2/2
A
A= ref $_[-1]
Aresult
11
00
169 37 ▶ $content = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 31 6 37
170 37 ▶ $attrs->{for} ||= $self->_sanitize_to_id($name) if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 37 0 37
Condition66%2/3
$attrs->{"for"} ||= $self->_sanitize_to_id($name)
l!l&&r!l&&!r
3250
MC/DC0%0/2
$attrs->{"for"} ||= $self->_sanitize_to_id($name)
$attrs->{"for"} $self->_sanitize_to_id($name)
Truth table66%2/3
A ||= B
A= $attrs->{"for"}B= $self->_sanitize_to_id($name)
ABresult
1X1
011
000
171  
172 37 return $self->tags->label($attrs, $content);
173 }
174  
175 sub text_area_tag {
176 6 ▶ my $self = shift @_;
Subroutine text_area_tag: called CC=4 SCAR=13.9
Pod documented
177 6 my $name = shift @_;
178 6 ▶ my $attrs = (ref($_[-1])||'') eq 'HASH' ? pop @_ : +{};
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[-1] || '') eq 'HASH' ? : 5 1 6
Condition100%2/2
ref $_[-1] || ''
l!l
51
MC/DC100%1/1
ref $_[-1] || ''
ref $_[-1]
Truth table100%2/2
A
A= ref $_[-1]
Aresult
11
00
179 6 ▶ my $content = @_ ? shift @_ : '';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 5 1 6
180  
181 6 $attrs = $self->_merge_attrs(+{ name=>$name, id=>"@{[ $self->_sanitize_to_id($name) ]}" }, $attrs);
182 6 return $self->tags->textarea($attrs, $content);
183 }
184  
185 sub input_tag {
186 95 ▶ my $self = shift;
Subroutine input_tag: called CC=10 SCAR=23.0
Pod documented
187 95 my ($name, $value, $attrs) = (undef, undef, +{});
188 95 ▶ $attrs = pop @_ if (ref($_[-1])||'') eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[-1] || "") eq "HASH" 92 3 95
Condition100%2/2
ref $_[-1] || ''
l!l
923
MC/DC100%1/1
ref $_[-1] || ''
ref $_[-1]
Truth table100%2/2
A
A= ref $_[-1]
Aresult
11
00
189 95 ▶ $name = shift @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 83 12 95
190 95 ▶ $value = shift @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 9 86 95
191  
192 95 $attrs = $self->_merge_attrs(+{type => "text"}, $attrs);
193 95 ▶ $attrs = $self->_merge_attrs(+{name => $name}, $attrs) if defined($name);
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 83 12 95
194 95 ▶ $attrs = $self->_merge_attrs(+{value => $value}, $attrs) if defined($value);
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 9 86 95
195 95 ▶ $attrs->{id} = $self->_sanitize_to_id($attrs->{name}) if exists($attrs->{name}) && defined($attrs->{name}) && !exists($attrs->{id});
Branch: 1 branch
BranchTrueFalseTotal
if exists $attrs->{"name"} and defined $attrs->{"name"} and not exists $attrs->{"id"} 20 75 95
Condition83%5/6
exists $attrs->{"name"} and defined $attrs->{"name"} and not exists $attrs->{"id"}
!ll&&!rl&&r
37220
exists $attrs->{"name"} and defined $attrs->{"name"}
!ll&&!rl&&r
3092
MC/DC66%2/3
exists $attrs->{"name"} and defined $attrs->{"name"} and not exists $attrs->{"id"}
exists $attrs->{"name"} defined $attrs->{"name"} not exists $attrs->{"id"}
Truth table75%3/4
A and B and not(C)
A= exists $attrs->{"name"}B= defined $attrs->{"name"}C= not exists $attrs->{"id"}
ABCresult
0XX0
10X0
1100
1111
196  
197 95 return $self->tags->input($attrs);
198 }
199  
200 sub radio_button_tag {
201 2 ▶ my $self = shift;
Subroutine radio_button_tag: called CC=5 SCAR=16.4
Pod documented
202 2 my ($name, $value) = (shift @_, shift @_);
203 2 ▶ my $attrs = (ref($_[-1])||'') eq 'HASH' ? pop(@_) : +{};
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[-1] || '') eq 'HASH' ? : 2 0 2
Condition50%1/2
ref $_[-1] || ''
l!l
20
MC/DC0%0/1
ref $_[-1] || ''
ref $_[-1]
Truth table50%1/2
A
A= ref $_[-1]
Aresult
11
00
204 2 ▶ my $checked = @_ ? shift(@_) : 0;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 0 2
205  
206 2 $attrs = $self->_merge_attrs(+{
207 type=>'radio',
208 name=>$name,
209 value=>$value,
210 2 id=>"@{[ $self->_sanitize_to_id($name) ]}_@{[ $self->_sanitize_to_id($value) ]}"
211 }, $attrs);
212 2 ▶ $attrs->{checked} = 'checked' if $checked;
Branch: 1 branch
BranchTrueFalseTotal
if $checked 1 1 2
213  
214 2 return $self->input_tag($attrs);
215 }
216  
217 sub password_tag {
218 4 ▶ my $self = shift;
Subroutine password_tag: called CC=3 SCAR=11.0
Pod documented
219 4 ▶ my $attrs = (ref($_[-1])||'' eq 'HASH') ? pop : +{};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] || !1 ? : 2 2 4
Condition100%2/2
ref $_[-1] or
l!l
22
MC/DC100%1/1
ref $_[-1] or
ref $_[-1]
Truth table100%2/2
A
A= ref $_[-1]
Aresult
11
00
220 4 $attrs = $self->_merge_attrs(+{ type=>'password' }, $attrs);
221 4 return $self->input_tag(@_, $attrs);
222 }
223  
224 sub hidden_tag {
225 6 ▶ my $self = shift;
Subroutine hidden_tag: called CC=3 SCAR=11.0
Pod documented
226 6 ▶ my $attrs = (ref($_[-1])||'' eq 'HASH') ? pop : +{};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] || !1 ? : 2 4 6
Condition100%2/2
ref $_[-1] or
l!l
24
MC/DC100%1/1
ref $_[-1] or
ref $_[-1]
Truth table100%2/2
A
A= ref $_[-1]
Aresult
11
00
227 6 $attrs = $self->_merge_attrs(+{ type=>'hidden' }, $attrs);
228 6 return $self->input_tag(@_, $attrs);
229 }
230  
231 sub submit_tag {
232 6 ▶ my $self = shift;
Subroutine submit_tag: called CC=4 SCAR=13.9
Pod documented
233 6 my ($value, $attrs);
234 6 ▶ $attrs = (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
235 6 ▶ $value = @_ ? shift(@_) : $DEFAULT_SUBMIT_TAG_VALUE;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 4 2 6
236  
237 6 $attrs = $self->_merge_attrs(+{ type=>'submit', name=>'commit', value=>$value }, $attrs);
238 6 return $self->input_tag($attrs);
239 }
240  
241 # legend_tag $value, \%attrs
242 # legend_tag \%attrs, \&block
243  
244 sub legend_tag {
245 13 ▶ my $self = shift;
Subroutine legend_tag: called CC=4 SCAR=13.9
Pod documented
246 13 ▶ my $content = (ref($_[-1])||'') eq 'CODE' ? pop(@_) : shift(@_);
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[-1] || '') eq 'CODE' ? : 3 10 13
Condition100%2/2
ref $_[-1] || ''
l!l
103
MC/DC100%1/1
ref $_[-1] || ''
ref $_[-1]
Truth table100%2/2
A
A= ref $_[-1]
Aresult
11
00
247 13 ▶ my $attrs = @_ ? shift(@_) : +{};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 9 4 13
248 13 return $self->tags->legend($attrs, $content)
249 }
250  
251 sub option_tag {
252 89 ▶ my $self = shift;
Subroutine option_tag: called CC=2 SCAR=6.9
Pod documented
253 89 my ($text, $attrs) = (@_, +{});
254 89 ▶ $attrs->{value} = $text unless exists $attrs->{value};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attrs->{"value"} 2 87 89
255 89 return $self->tags->option($attrs, $text);
256 }
257  
258 sub select_tag {
259 18 ▶ my ($self, $name) = (shift(), shift());
Subroutine select_tag: called CC=13 SCAR=26.0
Pod documented
260 18 ▶ my $attrs = (ref($_[-1])||'') eq 'HASH' ? pop(@_) : +{};
Branch: 1 branch
BranchTrueFalseTotal
(ref $_[-1] || '') eq 'HASH' ? : 16 2 18
Condition50%1/2
ref $_[-1] || ''
l!l
180
MC/DC0%0/1
ref $_[-1] || ''
ref $_[-1]
Truth table50%1/2
A
A= ref $_[-1]
Aresult
11
00
261 18 ▶ my $option_tags = @_ ? shift(@_) : "";
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 18 0 18
262 18 my $html_name = $name;
263 18 ▶ if($attrs->{multiple}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs->{"multiple"}) 4 14 18
264 4 ▶ $html_name = "${name}[]" unless ( ($name =~ m/\[\]$/) || ($name =~ m/\[\]\.\w+$/) );
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /\[\]$/ or $name =~ /\[\]\.\w+$/ 2 2 4
Condition66%2/3
$name =~ /\[\]$/ or $name =~ /\[\]\.\w+$/
l!l&&r!l&&!r
022
MC/DC50%1/2
$name =~ /\[\]$/ or $name =~ /\[\]\.\w+$/
$name =~ /\[\]$/ $name =~ /\[\]\.\w+$/
Truth table66%2/3
A or B
A= $name =~ /\[\]$/B= $name =~ /\[\]\.\w+$/
ABresult
1X1
011
000
265 }
266  
267 18 ▶ my $include_hidden = exists($attrs->{include_hidden}) ? delete($attrs->{include_hidden}) : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $attrs->{'include_hidden'} ? : 4 14 18
268 18 ▶ if(my $include_blank = delete $attrs->{include_blank}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $include_blank = delete $attrs->{"include_blank"}) 5 13 18
269 5 my $options_for_blank_options_tag = +{ value => '' };
270 5 ▶ if($include_blank eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($include_blank eq 1) 4 1 5
271 4 $include_blank = '';
272 4 $options_for_blank_options_tag->{label} = ' ';
273 }
274 5 my $blank_options = $self->content_tag('option', $include_blank, $options_for_blank_options_tag);
275 5 $option_tags = $self->join_tags($blank_options, $option_tags);
276 }
277 18 ▶ if(my $prompt = delete $attrs->{prompt}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prompt = delete $attrs->{"prompt"}) 1 17 18
278 1 my $prompt = $self->content_tag('option', $prompt, +{value=>''});
279 1 $option_tags = $self->join_tags($prompt, $option_tags);
280 }
281  
282 18 $attrs = $self->_merge_attrs(+{ name=>$html_name, id=>"@{[ $self->_sanitize_to_id($name) ]}" }, $attrs);
283 18 my $select_tag = $self->content_tag('select', $option_tags, $attrs);
284  
285 18 ▶ if($attrs->{multiple} && $include_hidden) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs->{"multiple"} and $include_hidden) 0 18 18
Condition66%2/3
$attrs->{"multiple"} and $include_hidden
!ll&&!rl&&r
1440
MC/DC0%0/2
$attrs->{"multiple"} and $include_hidden
$attrs->{"multiple"} $include_hidden
Truth table66%2/3
A and B
A= $attrs->{"multiple"}B= $include_hidden
ABresult
0X0
100
111
286 0 my $hidden = $self->hidden_tag($html_name, '', +{id=>$attrs->{id}.'_hidden', value=>1});
287 0 $select_tag = $self->join_tags($hidden, $select_tag, $attrs);
288 }
289 18 return $select_tag;
290 }
291  
292 sub options_for_select {
293 25 ▶ my ($self, $options_proto) = (shift(), shift());
Subroutine options_for_select: called CC=12 SCAR=24.9
Pod documented
294 25 ▶ return $options_proto unless( (ref($options_proto)||'') eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $options_proto || "") eq "ARRAY" 0 25 25
Condition50%1/2
ref $options_proto || ''
l!l
250
MC/DC0%0/1
ref $options_proto || ''
ref $options_proto
Truth table50%1/2
A
A= ref $options_proto
Aresult
11
00
295 25 my @options = $self->_normalize_options_for_select($options_proto);
296  
297 25 ▶ my $attrs_proto = $_[0] ? shift(@_) : [];
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 23 2 25
298 25 my (@selected_values, @disabled_values, %global_attributes) = ();
299 25 ▶ if( (ref($attrs_proto)||'') eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ((ref $attrs_proto || '') eq 'ARRAY') { } 6 19 25
elsif ((ref $attrs_proto || '') eq 'HASH') { } 16 3 19
Condition100%2/2
ref $attrs_proto || ''
l!l
223
MC/DC100%1/1
ref $attrs_proto || ''
ref $attrs_proto
Truth table100%2/2
A
A= ref $attrs_proto
Aresult
11
00
300 6 @selected_values = @$attrs_proto;
301 ▶ } elsif( (ref($attrs_proto)||'') eq 'HASH') {
Condition100%2/2
ref $attrs_proto || ''
l!l
163
MC/DC100%1/1
ref $attrs_proto || ''
ref $attrs_proto
Truth table100%2/2
A
A= ref $attrs_proto
Aresult
11
00
302 16 ▶ @selected_values = @{delete $attrs_proto->{selected}} if exists($attrs_proto->{selected});
Branch: 1 branch
BranchTrueFalseTotal
if exists $attrs_proto->{"selected"} 16 0 16
303 16 ▶ @disabled_values = @{delete $attrs_proto->{disabled}} if exists($attrs_proto->{disabled});
Branch: 1 branch
BranchTrueFalseTotal
if exists $attrs_proto->{"disabled"} 16 0 16
304 16 %global_attributes = %{$attrs_proto};
305 } else {
306 3 @selected_values = ($attrs_proto);
307 }
308
309 25 ▶ my %selected_lookup = @selected_values ? (map { $_=>1 } @selected_values) : ();
Branch: 1 branch
BranchTrueFalseTotal
@selected_values ? : 22 3 25
310 25 ▶ my %disabled_lookup = @disabled_values ? (map { $_=>1 } @disabled_values) : ();
Branch: 1 branch
BranchTrueFalseTotal
@disabled_values ? : 4 21 25
311 my (@options_for_select) = map {
312 25 $self->option_for_select($_, \%selected_lookup, \%disabled_lookup, \%global_attributes);
313 } @options;
314  
315 25 return $self->join_tags(@options_for_select);
316 }
317  
318 sub _normalize_options_for_select {
319 25 ▶ my ($self, $options_proto) = (shift(), shift());
Subroutine _normalize_options_for_select: called CC=6 SCAR=17.9
320 my @options = map {
321 80 ▶ push @$_, +{} unless (ref($_->[-1])||'') eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_->[-1] || "") eq "HASH" 17 63 80
Condition100%2/2
ref $_->[-1] || ''
l!l
6317
MC/DC100%1/1
ref $_->[-1] || ''
ref $_->[-1]
Truth table100%2/2
A
A= ref $_->[-1]
Aresult
11
00
322 80 ▶ unshift @$_, $_->[0] unless scalar(@$_) == 3;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$_ == 3 0 80 80
323 80 $_;
324 } map {
325 25 ▶ (ref($_)||'') eq 'ARRAY' ? $_ : [$_, $_, +{}];
Branch: 1 branch
BranchTrueFalseTotal
(ref $_ || '') eq 'ARRAY' ? : 50 30 80
Condition100%2/2
ref $_ || ''
l!l
5030
MC/DC100%1/1
ref $_ || ''
ref $_
Truth table100%2/2
A
A= ref $_
Aresult
11
00
326 } @$options_proto;
327 25 return @options;
328 }
329  
330 sub option_for_select {
331 80 ▶ my ($self, $option_info, $selected, $disabled, $attrs) = @_;
Subroutine option_for_select: called CC=3 SCAR=11.0
Pod undocumented
332 80 my %attrs = (value=>$option_info->[1], %{$option_info->[2]}, %$attrs);
333 80 ▶ $attrs{selected} = 'selected' if $selected->{$option_info->[1]};
Branch: 1 branch
BranchTrueFalseTotal
if $$selected{$option_info->[1]} 31 49 80
334 80 ▶ $attrs{disabled} = 'disabled' if $disabled->{$option_info->[1]};
Branch: 1 branch
BranchTrueFalseTotal
if $$disabled{$option_info->[1]} 4 76 80
335  
336 80 return $self->option_tag($option_info->[0], \%attrs);
337 }
338  
339 32 ▶sub option_html_attributes { return +{} }
Subroutine option_html_attributes: called CC=1 SCAR=0.0
Pod undocumented
340  
341 sub options_from_collection_for_select {
342 9 ▶ my ($self, $collection, $value_method, $label_method, $selected_proto) = (@_);
Subroutine options_from_collection_for_select: called CC=19 SCAR=29.5
Pod documented
343  
344 9 my @options = ();
345 9 my @selected = ();
346 9 my @disabled = ();
347 9 my %global_attributes = ();
348  
349 9 ▶ @disabled = @{ delete($selected_proto->{disabled})||[] } if (ref($selected_proto)||'') eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
if (ref $selected_proto || "") eq "HASH" 5 4 9
delete $selected_proto->{"disabled"} or [] 0 5 5
Condition100%2/2
ref $selected_proto || ''
l!l
72
MC/DC100%1/1
ref $selected_proto || ''
ref $selected_proto
Truth table100%2/2
A
A= ref $selected_proto
Aresult
11
00
350 9 ▶ @selected = @{ delete($selected_proto->{selected})||[] } if (ref($selected_proto)||'') eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
if (ref $selected_proto || "") eq "HASH" 5 4 9
delete $selected_proto->{"selected"} or [] 0 5 5
Condition100%2/2
ref $selected_proto || ''
l!l
72
MC/DC100%1/1
ref $selected_proto || ''
ref $selected_proto
Truth table100%2/2
A
A= ref $selected_proto
Aresult
11
00
351 9 ▶ %global_attributes = %{$selected_proto} if (ref($selected_proto)||'') eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $selected_proto || "") eq "HASH" 5 4 9
Condition100%2/2
ref $selected_proto || ''
l!l
72
MC/DC100%1/1
ref $selected_proto || ''
ref $selected_proto
Truth table100%2/2
A
A= ref $selected_proto
Aresult
11
00
352 9 ▶ @selected = @$selected_proto if (ref($selected_proto)||'') eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $selected_proto || "") eq "ARRAY" 1 8 9
Condition100%2/2
ref $selected_proto || ''
l!l
72
MC/DC100%1/1
ref $selected_proto || ''
ref $selected_proto
Truth table100%2/2
A
A= ref $selected_proto
Aresult
11
00
353 9 ▶ @selected = ($selected_proto) if ((ref(\$selected_proto)||'') eq 'SCALAR') && defined($selected_proto);
Branch: 1 branch
BranchTrueFalseTotal
if (ref \$selected_proto || "") eq "SCALAR" and defined $selected_proto 1 8 9
Condition80%4/5
(ref \$selected_proto || "") eq "SCALAR" and defined $selected_proto
!ll&&!rl&&r
711
ref \$selected_proto || ''
l!l
90
MC/DC50%1/2
(ref \$selected_proto || "") eq "SCALAR" and defined $selected_proto
ref \$selected_proto defined $selected_proto
Truth table66%2/3
A and B
A= ref \$selected_protoB= defined $selected_proto
ABresult
0X0
100
111
354  
355 9 ▶ while(my $item = $collection->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = $collection->next) 32 9 41
356 32 push @options, [ $self->field_value($item,$label_method), $self->field_value($item, $value_method), $self->option_html_attributes($item) ];
357 32 ▶ push @selected, $item->$value_method if ((ref($selected_proto)||'') eq 'CODE') && $selected_proto->($item);
Branch: 1 branch
BranchTrueFalseTotal
if (ref $selected_proto || "") eq "CODE" and &$selected_proto($item) 1 31 32
Condition100%5/5
(ref $selected_proto || "") eq "CODE" and &$selected_proto($item)
!ll&&!rl&&r
2831
ref $selected_proto || ''
l!l
248
MC/DC100%2/2
(ref $selected_proto || "") eq "CODE" and &$selected_proto($item)
ref $selected_proto &$selected_proto($item)
Truth table100%3/3
A and B
A= ref $selected_protoB= &$selected_proto($item)
ABresult
0X0
100
111
358 }
359  
360 9 ▶ $collection->reset if $collection->can('reset');
Branch: 1 branch
BranchTrueFalseTotal
if $collection->can("reset") 9 0 9
361 9 return $self->options_for_select(\@options, +{ selected=>\@selected, disabled=>\@disabled, %global_attributes});
362 }
363  
364 4 my %_sanitized_name_cache = ();
365 sub _sanitize {
366 169 ▶ my ($self, $value) = (shift(), shift());
Subroutine _sanitize: called CC=3 SCAR=11.0
367 169 ▶ return unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 167 169
368 167 ▶ return $_sanitized_name_cache{$value} if exists $_sanitized_name_cache{$value};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_sanitized_name_cache{$value} 155 12 167
369  
370 12 my $original_value = $value;
371 12 $value =~ s/\]\[|[^a-zA-Z0-9:-]/_/g;
372 12 $value =~s/_$//;
373 12 $_sanitized_name_cache{$original_value} = $value;
374 12 return $value;
375 }
376  
377 sub field_value {
378 198 ▶ my ($self, $model, $attribute) = @_;
Subroutine field_value: called CC=3 SCAR=11.0
Pod undocumented
379 198 ▶ return $model->read_attribute_for_html($attribute) if $model->can('read_attribute_for_html');
Branch: 1 branch
BranchTrueFalseTotal
if $model->can("read_attribute_for_html") 0 198 198
380 198 ▶ return $model->$attribute if $model->can($attribute);
Branch: 1 branch
BranchTrueFalseTotal
if $model->can($attribute) 192 6 198
381 6 return ''; # TODO should look at $formbuilder->options->{allow_method_names_outside_object}
382 }
383  
384 sub field_id {
385 169 ▶ my ($self, $model_or_model_name, $attribute, $options, @extra) = @_;
Subroutine field_id: called CC=6 SCAR=17.9
Pod undocumented
386 169 ▶ my $model_name = Scalar::Util::blessed($model_or_model_name) ?
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($model_or_model_name) ? : 0 169 169
387 $model_or_model_name->model_name->singular :
388 $model_or_model_name;
389  
390 169 my $sanitized_object_name = $self->_sanitize($model_name);
391 169 ▶ my $id = exists($options->{namespace}) ? $options->{namespace} . '_' : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'namespace'} ? : 1 168 169
392  
393 169 ▶ if($sanitized_object_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sanitized_object_name) { } 167 2 169
394 167 ▶ $id .= exists($options->{index}) ?
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'index'} ? : 29 138 167
395 29 "@{[ $sanitized_object_name ]}_@{[ $options->{index} ]}_${attribute}" :
396 138 "@{[ $sanitized_object_name ]}_${attribute}";
397 } else {
398 2 $id .= $attribute;
399 }
400  
401 169 ▶ $id = join('_', $id, @extra) if scalar @extra;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @extra 5 164 169
402 169 return $id;
403 }
404  
405 sub field_name {
406 115 ▶ my ($self, $model_name, $attribute, $options, @names) = @_;
Subroutine field_name: called CC=5 SCAR=16.1
Pod undocumented
407 115 ▶ my $names = @names ? join("", map { "[$_]" } @names) : '';
Branch: 1 branch
BranchTrueFalseTotal
@names ? : 0 115 115
408  
409 115 my $name;
410 115 ▶ if($model_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($model_name) { } 113 2 115
411 113 ▶ $name = exists($options->{index}) ?
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'index'} ? : 20 93 113
412 20 "@{[ $model_name ]}\[@{[ $options->{index}]}\].${attribute}${names}" :
413 93 "@{[ $model_name ]}.${attribute}${names}";
414 } else {
415 2 $name = "${attribute}${names}";
416 }
417 115 ▶ $name .= '[]' if $options->{multiple};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"multiple"} 2 113 115
418  
419 115 return $name;
420 }
421  
422 4 1;
423  
424 =head1 NAME
425  
426  
427  
428 Valiant::HTML::Util::FormTags - HTML Form Tags
429  
430 =head1 SYNOPSIS
431  
432 my $view = Valiant::HTML::Util::View->new(aaa => 1,bbb => 2);
433 my $tb = Valiant::HTML::Util::FormTags->new(view => $view);
434  
435 =head1 DESCRIPTION
436  
437 Functions that generate HTML tags, specifically those around HTML forms. Not all
438 HTML tags are in this library, this focuses on things that would be useful for building
439 HTML forms. In general this is a support libary for L<Valiant::HTML::FormBuilder> but
440 there's nothing preventing you from using these stand alone, particularly when you have
441 very complex form layout needs.
442  
443 Requiers a 'view' or template object that supports methods for created 'safe' strings that
444 are properly marked as safe for HTML display.
445  
446 =head1 ATTRIBUTES
447  
448 This class has the following initialization attributes
449  
450 =head2 view
451  
452 Object, Required. This should be an object that provides methods for creating escaped
453 strings for HTML display. Many template systems provide a way to mark strings as safe
454 for display, such as L<Mojo::Template>. You will need to add the following proxy methods
455 to your view / template to adapt it for use in creating safe strings.
456  
457 =over
458  
459 =item raw
460  
461 given a string return a single tagged object which is marked as safe for display. Do not do any HTML
462 escaping on the string. This is used when you want to pass strings straight to display and that you
463 know is safe. Be careful with this to avoid HTML injection attacks.
464  
465 =item safe
466  
467 given a string return a single tagged object which is marked as safe for display. First HTML escape the
468 string as safe unless its already been done (no double escaping).
469  
470 =item safe_concat
471  
472 Same as C<safe> but instead works an an array of strings (or mix of strings and safe string objects) and
473 concatenates them all into one big safe marked string.
474  
475 =item html_escape
476  
477 Given a string return string that has been HTML escaped.
478  
479 =item read_attribute_for_html
480  
481 Given an attribute name return the value that the view has defined for it.
482  
483 =item attribute_exists_for_html
484  
485 Given an attribute name return true if the view has defined it.
486  
487 =back
488  
489 Both C<raw>, C<safe> and C<safe_concat> should return a 'tagged' object which is specific to your view or
490 template system. However this object must 'stringify' to the safe version of the string to be displayed. See
491 L<Valiant::HTML::SafeString> for example API. We use L<Valiant::HTML::SafeString> internally to provide
492 safe escaping if you're view doesn't do automatic escaping, as many older template systems like Template
493 Toolkit.
494  
495 See L<Valiant::HTML::Util::View> for a simple view object that provides these methods.
496  
497 =head1 INHERITANCE
498  
499 This class extends L<Valiant::HTML::Util::TagBuilder> and inherits all methods from that class.
500  
501 =head1 METHODS
502  
503 The following instance methods are supported by this class
504  
505 =head2 button_tag
506  
507 $tb->button_tag($content_string, \%attrs)
508 $tb->button_tag($content_string)
509 $tb->button_tag(\%attrs, \&content_code_block)
510 $tb->button_tag(\&content_code_block)
511  
512 Creates a button element that defines a submit button, reset button or a generic button which
513 can be used in JavaScript, for example. You can use the button tag as a regular submit tag but
514 it isn't supported in legacy browsers. However, the button tag does allow for richer labels
515 such as images and emphasis, so this helper will also accept a block. By default, it will create
516 a button tag with type submit, if type is not given. HTML attribute C<name> defaults to
517 'Button' if not supplied. Inner content also defaults to 'Button' if not supplied.
518  
519 =head2 checkbox_tag
520  
521 $tb->checkbox_tag($name)
522 $tb->checkbox_tag($name, $value)
523 $tb->checkbox_tag($name, $value, $checked)
524 $tb->checkbox_tag($name, \%attrs)
525 $tb->checkbox_tag($name, $value, \%attrs)
526 $tb->checkbox_tag($name, $value, $checked, \%attrs)
527  
528 Creates a check box form input tag. C<id> will be generated from $name if not passed in \%attrs. C<value>
529 attribute will default to '1' and the control is unchecked by default.
530  
531 =head2 fieldset_tag
532  
533 $tb->fieldset_tag(\%content_block)
534 $tb->fieldset_tag(\%attrs, \%content_block)
535 $tb->fieldset_tag($legend, \%attrs, \%content_block)
536 $tb->fieldset_tag($legend, \%content_block)
537  
538 Create a C<fieldset> with inner content. Example:
539  
540 $tb->fieldset_tag(sub {
541 $tb->button_tag('username');
542 });
543  
544 # <fieldset><button name="button">username</button></fieldset>
545
546 $tb->fieldset_tag('Info', sub {
547 $tb->button_tag('username');
548 });
549  
550 # <fieldset><legend>Info</legend><button name="button">username</button></fieldset>
551  
552 =head2 legend_tag
553  
554 $tb->legend_tag($legend, \%html_attrs);
555 $tb->legend_tag($legend);
556 $tb->legend_tag(\%html_attrs, \&content_block);
557 $tb->legend_tag(\&content_block);
558  
559 Create an HTML form legend tag and content. Examples:
560  
561 $tb->legend_tag('test', +{class=>'foo'});
562 # <legend class="foo">test</legend>
563  
564 $tb->legend_tag('test');
565 # <legend>test</legend>
566  
567 $tb->legend_tag({class=>'foo'}, sub { 'test' });
568 # <legend class="foo">test</legend>
569  
570 $tb->legend_tag(sub { 'test' });
571 # <legend>test</legend>
572  
573 =head2 form_tag
574  
575 $tb->form_tag('/signup', \%attrs, \&content)
576 $tb->form_tag(\@args, \%attrs, \&content)
577  
578 Create a form tag with inner content. Example:
579  
580 $tb->form_tag('/user', +{ class=>'form' }, sub {
581 $tb->checkbox_tag('person[1]username', +{class=>'aaa'});
582 });
583  
584 Produces:
585  
586 <form accept-charset="UTF-8" action="/user" class="form" method="POST">
587 <input class="aaa" id="person_1username" name="person[1]username" type="checkbox" value="1"/>
588 </form>';
589  
590 In general C<\%attrs> are expected to be HTML attributes. However, the following special attributes
591 are supported:
592  
593 =over 4
594  
595 =item csrf_token
596  
597 If set, will use the value given to generate a hidden input tag with the name C<csrf_token> and the value.
598 Example:
599  
600 my $csrf_token = "1234567890";
601 $tb->form_tag('/user', +{ csrf_token=>$csrf_token }, sub {
602 $tb->checkbox_tag('username', +{class=>'aaa'});
603 });
604  
605 Produces:
606
607 <form accept-charset="UTF-8" action="/user" class="form" method="POST">
608 <input class="aaa" id="username" name="username" type="checkbox" value="1"/>
609 <input name="csrf_token" type="hidden" value="1234567890"/>
610 </form>';
611  
612 =item tunneled_method
613  
614 If set, will change any C<method> attribute to C<POST> and add a query parameter to
615 the action URL with the name C<_method> and the value of the C<method> attribute. Example:
616  
617 $tb->form_tag('/user', +{ method=>'DELETE', tunneled_method=>1 }, sub {
618 $tb->checkbox_tag('username', +{class=>'aaa'});
619 });
620  
621 Produces:
622
623 <form accept-charset="UTF-8" action="/user?_method=DELETE" class="form" method="POST">
624 <input class="aaa" id="username" name="username" type="checkbox" value="1"/>
625 </form>';
626  
627 Useful for browsers that don't support C<PUT> or C<DELETE> methods in forms (which is most of them).
628  
629 =back
630  
631 These special attributes will be removed from the attributes list before generating the HTML tag.
632  
633 =head2 label_tag
634  
635 $tb->label_tag($name, $content, \%attrs);
636 $tb->label_tag($name, $content);
637 $tb->label_tag($name, \%attrs, \&content);
638 $tb->label_tag($name, \&content);
639  
640 Create a label tag where $name is set to the C<for> attribute. Can contain string or block contents. Label
641 contents default to something based on $name;
642  
643 $tb->label_tag('user_name', "User", +{id=>'userlabel'}); # <label id='userlabel' for='user_name'>User</label>
644 $tb->label_tag('user_name'); # <label for='user_name'>User Name</label>
645  
646 Example with block content:
647  
648 $tb->label_tag('user_name', sub {
649 'User Name Active',
650 $tb->checkbox_tag('active', 'yes', 1);
651 });
652  
653 <label for='user_name'>
654 User Name Active<input checked value="yes" id="user_name" name="user_name" type="checkbox"/>
655 </label>
656  
657 Produce a label tag, often linked to an input tag. Can accept a block coderef. Examples:
658  
659 =head2 radio_button_tag
660  
661 $tb->radio_button_tag($name, $value)
662 $tb->radio_button_tag($name, $value, $checked)
663 $tb->radio_button_tag($name, $value, $checked, \%attrs)
664 $tb->radio_button_tag($name, $value, \%attrs)
665  
666 Creates a radio button; use groups of radio buttons named the same to allow users to select from a
667 group of options. Examples:
668  
669 $tb->radio_button_tag('role', 'admin', 0, +{ class=>'radio' });
670 # <input class="radio" id="role_admin" name="role" type="radio" value="admin"/>
671  
672 $tb->radio_button_tag('role', 'user', 1, +{ class=>'radio' });
673 # <input checked class="radio" id="role_user" name="role" type="radio" value="user"/>'
674  
675 =head2 option_tag
676  
677 $tb->option_tag($text, \%attributes)
678 $tb->option_tag($text)
679  
680 Create a single HTML option. C<value> attribute is inferred from $text if not in \%attributes. Examples:
681  
682 $tb->option_tag('test', +{class=>'foo', value=>'100'});
683 # <option class="foo" value="100">test</option>
684  
685 $tb->option_tag('test'):
686 #<option value="test">test</option>
687  
688 =head2 text_area_tag
689  
690 $tb->text_area_tag($name, \%attrs)
691 $tb->text_area_tag($name, $content, \%attrs)
692  
693 Create a named text_area form field. Examples:
694  
695 $tb->text_area_tag("user", "hello", +{ class=>'foo' });
696 # <textarea class="foo" id="user" name="user">hello</textarea>
697  
698 $tb->text_area_tag("user", +{ class=>'foo' });
699 # <textarea class="foo" id="user" name="user"></textarea>
700  
701 =head2 input_tag
702  
703 $tb->input_tag($name, $value, \%attrs)
704 $tb->input_tag($name, $value)
705 $tb->input_tag($name)
706 $tb->input_tag($name, \%attrs)
707 $tb->input_tag(\%attrs)
708  
709 Create a HTML input tag. If $name and/or $value are set, they are used to populate the 'name' and
710 'value' attributes of the C<input>. Anything passed in the \%attrs hashref overrides. Examples:
711  
712 $tb->input_tag('username', 'jjn', +{class=>'aaa'});
713 # <input class="aaa" id="username" name="username" type="text" value="jjn"/>
714  
715 $tb->input_tag('username', 'jjn');
716 # <input id="username" name="username" type="text" value="jjn"/>
717  
718 $tb->input_tag('username');
719 # <input id="username" name="username" type="text"/>
720  
721 $tb->input_tag('username', +{class=>'foo'});
722 # <input class="foo" id="username" name="username" type="text"/>
723  
724 $tb->input_tag(+{class=>'foo'});
725 # <input class="foo" type="text"/>
726  
727 =head2 password_tag
728  
729 Creates an password input tag with the given type. Example:
730  
731 $tb->password_tag('password', +{class=>'foo'});
732 # <input class="foo" id="password" name="password" type="password"/>
733  
734 =head2 hidden_tag
735  
736 Creates an input tag with the given type. Example:
737  
738 $tb->hidden_tag('user_id', 100, +{class=>'foo'});
739 # <input class="foo" id="user_id" name="user_id" type="hidden" value="100"/>
740
741 =head2 submit_tag
742  
743 $tb->submit_tag
744 $tb->submit_tag($value)
745 $tb->submit_tag(\%attrs)
746 $tb->submit_tag \($value, \%attrs)
747  
748 Create a submit tag. Examples:
749  
750 $tb->submit_tag;
751 # <input id="commit" name="commit" type="submit" value="Save changes"/>
752  
753 $tb->submit_tag('person');
754 # <input id="commit" name="commit" type="submit" value="person"/>
755  
756 $tb->submit_tag('Save', +{name=>'person'});
757 # <input id="person" name="person" type="submit" value="Save"/>
758  
759 $tb->submit_tag(+{class=>'person'});
760 # <input class="person" id="commit" name="commit" type="submit" value="Save changes"/>
761  
762 =head2 select_tag
763  
764 $tb->select_tag($name, $option_tags, \%attrs)
765 $tb->select_tag($name, $option_tags)
766 $tb->select_tag($name, \%attrs)
767  
768 Create a select tag group with options. Examples:
769  
770 $tb->select_tag("people", raw("<option>David</option>"));
771 # <select id="people" name="people"><option>David</option></select>
772  
773 $tb->select_tag("people", raw("<option>David</option>"), +{include_blank=>1});
774 # <select id="people" name="people"><option label=" " value=""></option><option>David</option></select>
775  
776 $tb->select_tag("people", raw("<option>David</option>"), +{include_blank=>'empty'});
777 # <select id="people" name="people"><option value="">empty</option><option>David</option></select>
778
779 $tb->select_tag("prompt", raw("<option>David-prompt</option>"), +{prompt=>'empty-prompt', class=>'foo'});
780 # <select class="foo" id="prompt" name="prompt"><option value="">empty-prompt</option><option>David-prompt</option></select>
781  
782 =head2 options_for_select
783  
784 $tb->options_for_select([$value1, $value2, ...], $selected_value)
785 $tb->options_for_select([$value1, $value2, ...], \@selected_values)
786 $tb->options_for_select([$value1, $value2, ...], +{ selected => $selected_value, disabled => \@disabled_values, %global_options_attributes })
787 $tb->options_for_select([ [$label, $value], [$label, $value, \%attrs], ...])
788  
789 Create a string of HTML option tags suitable for using with C<select_tag>. Accepts two arguments the
790 first of whuch is required. The first argument is an arrayref of values used for the options. Each value
791 can be one of a scalar (in which case the value is used as both the text label for the option as well as
792 its actual value attribute) or a arrayref where the first item is the option label, the second is the value
793 and an option third is a hashref used to add custom attributes to the option.
794  
795 The second (optional) argument lets you set which options are marked C<selected> and possible C<disabled>
796 If the second argument is a scalar value then it is used to mark that value as selected. If its an arrayref
797 then all matching values are selected. If its a hashref we look for a key C<selected> and key <disabled>
798 and expect those (if exists) to be an arrayref of matching values. Any additional keys in the hash will be
799 passed as global HTML attributes to the options. Examples:
800  
801 $tb->options_for_select(['A','B','C']);
802 # <option value="A">A</option>
803 # <option value="B">B</option>
804 # <option value="C">C</option>
805  
806 $tb->options_for_select(['A','B','C'], 'B');
807 # <option value="A">A</option>
808 # <option selected value="B">B</option>
809 # <option value="C">C</option>
810  
811 $tb->options_for_select(['A','B','C'], ['A', 'C']);
812 #<option selected value="A">A</option>
813 #<option value="B">B</option>
814 #<option selected value="C">C</option>
815  
816 $tb->options_for_select(['A','B','C'], ['A', 'C']);
817 # <option selected value="A">A</option>
818 # <option value="B">B</option>
819 # <option selected value="C">C</option>
820  
821 $tb->options_for_select([[a=>'A'],[b=>'B'], [c=>'C']]);
822 # <option value="A">a</option>
823 # <option value="B">b</option>
824 # <option value="C">c</option>
825  
826 $tb->options_for_select([[a=>'A'],[b=>'B'], [c=>'C']], 'B');
827 # <option value="A">a</option>
828 # <option selected value="B">b</option>
829 # <option value="C">c</option>
830  
831 $tb->options_for_select(['A',[b=>'B', {class=>'foo'}], [c=>'C']], ['A','C']);
832 # <option selected value="A">A</option>
833 # <option class="foo" value="B">b</option>
834 # <option selected value="C">c</option>
835  
836 $tb->options_for_select(['A','B','C'], +{selected=>['A','C'], disabled=>['B'], class=>'foo'});
837 # <option class="foo" selected value="A">A</option>
838 # <option class="foo" disabled value="B">B</option>
839 # <option class="foo" selected value="C">C</option>
840  
841 This function is useful with the C<select_tag>:
842  
843 $tb->select_tag("state", options_for_select(['A','B','C'], 'A'), +{include_blank=>1});
844 # <select id="state" name="state">
845 # <option label=" " value=""></option>
846 # <option selected value="A">A</option>
847 # <option value="B">B</option>
848 # <option value="C">C</option>
849 # </select>
850  
851 Please note that since C<options_for_select> returns a L<Valiant::HTML::SafeString> or safe string object
852 as supported by your view/template system of choice you don't need to add any additional escaping.
853  
854 =head2 options_from_collection_for_select
855  
856 Given a collection (an object that does the interface defined by L<Valiant::HTML::Util::Collection> return
857 a string of options suitable for C<select_tag>. Optionally you can pass additional arguments, like with
858 C<options_for_select> to mark individual options as selected, disabled and to pass additional HTML attributes.
859 Examples:
860  
861 my $collection = Valiant::HTML::Util::Collection->new([label=>'value'], [A=>'a'], [B=>'b'], [C=>'c']);
862  
863 $tb->options_from_collection_for_select($collection, 'value', 'label');
864 # <option value="value">label</option>
865 # <option value="a">A</option>
866 # <option value="b">B</option>
867 # <option value="c">C</option>
868  
869 $tb->options_from_collection_for_select($collection, 'value', 'label', 'a');
870 # <option value="value">label</option>
871 # <option selected value="a">A</option>
872 # <option value="b">B</option>
873 # <option value="c">C</option>
874  
875 $tb->options_from_collection_for_select($collection, 'value', 'label', ['a', 'c']);
876 # <option value="value">label</option>
877 # <option selected value="a">A</option>
878 # <option value="b">B</option>
879 # <option selected value="c">C</option>
880  
881 $tb->options_from_collection_for_select($collection, 'value', 'label', +{selected=>['a','c'], disabled=>['b'], class=>'foo'})
882 # <option class="foo" value="value">label</option>
883 # <option class="foo" selected value="a">A</option>
884 # <option class="foo" disabled value="b">B</option>
885 # <option class="foo" selected value="c">C</option>
886  
887 If you have a properly formed array you can use the C<array_to_collection> helper method:
888  
889 my $collection = $tb->array_to_collection([label=>'value'], [A=>'a'], [B=>'b'], [C=>'c']);
890 $tb->options_from_collection_for_select($collection, 'value', 'label');
891  
892 Additionally you can pass a coderef for dynamic selecting. Example:
893  
894 $tb->options_from_collection_for_select($collection, 'value', 'label', sub { shift->value eq 'a'} );
895 # <option value="value">label</option>
896 # <option selected value="a">A</option>
897 # <option value="b">B</option>
898 # <option value="c">C</option>
899  
900 The collection object must at minimum provide a method C<next> which returns the next object in the collection.
901 This method C<next> should return false when all the item objects have been iterated thru in turn. Optionally
902 you can provide a C<reset> method which will be called to return the collection to the first index.
903  
904 You can see L<Valiant::HTML::Util::Collection> source for example minimal code.
905  
906 =head2 array_to_collection
907  
908 Given a array of option labels as described above, create a collection
909  
910 =head1 SEE ALSO
911
912 L<Valiant>, L<Valiant::HTML::FormBuilder>
913  
914 =head1 AUTHOR
915
916 See L<Valiant>
917  
918 =head1 COPYRIGHT & LICENSE
919
920 See L<Valiant>
921  
922 =cut
923  
924 4 1;
« Valiant/HTML/Tag/Label.pm ↑ Summary Valiant/I18N.pm »