Valiant/HTML/Util/FormTags.pm

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