Valiant/HTML/Util/FormTags.pm

filter: Statementstmt 95.1% 276 / 290 Branchbran 84.5% 181 / 214 Conditioncond 77.6% 87 / 112 MC/DCmcdc 55.0% 33 / 60 Subroutinesub 100% 38 / 38 Podpod 91.3% 21 / 23 totaltotal 86.3% 636 / 737 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/Error.pm ↑ Summary Valiant/Validator/Length.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 88 ▶sub _sanitize_to_id {
Subroutine _sanitize_to_id: called CC=2 SCAR=7.0
18 88 my ($self, $value) = @_;
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 64 ▶sub _humanize {
Subroutine _humanize: called CC=2 SCAR=7.0
26 64 my ($self, $value) = @_;
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 4 ▶sub _prepend_block {
Subroutine _prepend_block: called CC=1 SCAR=0.0
34 4 my ($self, $block, @bits) = @_;
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 389 ▶sub _merge_attrs {
Subroutine _merge_attrs: called CC=19 SCAR=30.7
44 389 my ($self, $attrs1, $attrs2, @list) = @_;
45 389 ▶ my $overide = @list ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
@list ? : 77 312 389
46 389 ▶ @list = keys %{$attrs2||{}} unless @list;
Branch: 1 branch
BranchTrueFalseTotal
unless @list 312 77 389
Condition50%1/2
$attrs2 || {}
l!l
3120
MC/DC0%0/1
$attrs2 || {}
$attrs2
Truth table50%1/2
A
A= $attrs2
Aresult
11
00
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 9 ▶sub array_to_collection {
Subroutine array_to_collection: called CC=1 SCAR=0.0
Pod documented
75 9 my $self = shift;
76 9 return Valiant::HTML::Util::Collection->new(@_);
77 }
78  
79 14 ▶sub button_tag {
Subroutine button_tag: called CC=9 SCAR=22.0
Pod documented
80 14 my ($self, $content, $attrs) = (shift(), undef, +{});
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 32 ▶sub checkbox_tag {
Subroutine checkbox_tag: called CC=6 SCAR=17.9
Pod documented
93 32 my $self = shift;
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 4 ▶sub fieldset_tag {
Subroutine fieldset_tag: called CC=5 SCAR=16.1
Pod documented
108 4 my $self = shift;
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 20 ▶sub form_tag {
Subroutine form_tag: called CC=4 SCAR=14.0
Pod documented
123 20 my $self = shift;
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 20 ▶sub _process_form_attrs {
Subroutine _process_form_attrs: called CC=5 SCAR=16.2
134 20 my ($self, $url_info, $attrs) = @_;
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 20 ▶sub _process_method {
Subroutine _process_method: called CC=7 SCAR=19.9
145 20 my ($self, $attrs) = @_;
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 20 ▶sub _process_content {
Subroutine _process_content: called CC=2 SCAR=6.9
159 20 my ($self, $content, $attrs) = @_;
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 50 ▶sub label_tag {
Subroutine label_tag: called CC=8 SCAR=20.9
Pod documented
170 50 my ($self, $name) = (shift(), shift());
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 6 ▶sub text_area_tag {
Subroutine text_area_tag: called CC=4 SCAR=13.9
Pod documented
182 6 my $self = shift @_;
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 107 ▶sub input_tag {
Subroutine input_tag: called CC=10 SCAR=23.0
Pod documented
192 107 my $self = shift;
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 2 ▶sub radio_button_tag {
Subroutine radio_button_tag: called CC=5 SCAR=16.4
Pod documented
207 2 my $self = shift;
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 4 ▶sub password_tag {
Subroutine password_tag: called CC=3 SCAR=11.0
Pod documented
224 4 my $self = shift;
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 6 ▶sub hidden_tag {
Subroutine hidden_tag: called CC=3 SCAR=11.0
Pod documented
231 6 my $self = shift;
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 6 ▶sub submit_tag {
Subroutine submit_tag: called CC=4 SCAR=13.9
Pod documented
238 6 my $self = shift;
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 13 ▶sub legend_tag {
Subroutine legend_tag: called CC=4 SCAR=13.9
Pod documented
251 13 my $self = shift;
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 95 ▶sub option_tag {
Subroutine option_tag: called CC=2 SCAR=6.9
Pod documented
258 95 my $self = shift;
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 20 ▶sub select_tag {
Subroutine select_tag: called CC=13 SCAR=26.0
Pod documented
265 20 my ($self, $name) = (shift(), shift());
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 27 ▶sub options_for_select {
Subroutine options_for_select: called CC=12 SCAR=24.9
Pod documented
299 27 my ($self, $options_proto) = (shift(), shift());
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 27 ▶sub _normalize_options_for_select {
Subroutine _normalize_options_for_select: called CC=6 SCAR=17.9
325 27 my ($self, $options_proto) = (shift(), shift());
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 86 ▶sub option_for_select {
Subroutine option_for_select: called CC=3 SCAR=11.0
Pod undocumented
337 86 my ($self, $option_info, $selected, $disabled, $attrs) = @_;
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 11 ▶sub options_from_collection_for_select {
Subroutine options_from_collection_for_select: called CC=19 SCAR=29.5
Pod documented
348 11 my ($self, $collection, $value_method, $label_method, $selected_proto) = (@_);
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: 1 branch
BranchTrueFalseTotal
if (ref $selected_proto || "") eq "HASH" 7 4 11
Condition75%3/4
ref $selected_proto || ''
l!l
92
delete $selected_proto->{'disabled'} || []
l!l
70
MC/DC100%1/1
ref $selected_proto || ''
ref $selected_proto
MC/DC0%0/1
delete $selected_proto->{'disabled'} || []
delete $selected_proto->{'disabled'}
Truth table100%2/2
A
A= ref $selected_proto
Aresult
11
00
Truth table50%1/2
A
A= delete $selected_proto->{'disabled'}
Aresult
11
00
356 11 ▶ @selected = @{ delete($selected_proto->{selected})||[] } if (ref($selected_proto)||'') eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $selected_proto || "") eq "HASH" 7 4 11
Condition75%3/4
ref $selected_proto || ''
l!l
92
delete $selected_proto->{'selected'} || []
l!l
70
MC/DC100%1/1
ref $selected_proto || ''
ref $selected_proto
MC/DC0%0/1
delete $selected_proto->{'selected'} || []
delete $selected_proto->{'selected'}
Truth table100%2/2
A
A= ref $selected_proto
Aresult
11
00
Truth table50%1/2
A
A= delete $selected_proto->{'selected'}
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 228 ▶sub _sanitize {
Subroutine _sanitize: called CC=3 SCAR=11.0
372 228 my ($self, $value) = (shift(), shift());
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 265 ▶sub field_value {
Subroutine field_value: called CC=11 SCAR=29.3
Pod documented
384 265 my ($self, $model, $attribute, $options) = @_;
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 228 ▶sub field_id {
Subroutine field_id: called CC=6 SCAR=18.3
Pod documented
405 228 my ($self, $model_or_model_name, $attribute, $options, @extra) = @_;
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 125 ▶sub field_name {
Subroutine field_name: called CC=5 SCAR=16.8
Pod documented
426 125 my ($self, $model_name, $attribute, $options, @names) = @_;
427 0 ▶ 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 19 ▶sub field_errors {
Subroutine field_errors: called CC=4 SCAR=18.9
Pod documented
443 19 my ($self, $model, $attribute, $strict) = @_;
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 3 1;
452  
453 =head1 NAME
454  
455  
456  
457 Valiant::HTML::Util::FormTags - HTML Form Tags
458  
459 =head1 SYNOPSIS
460  
461 my $view = Valiant::HTML::Util::View->new(aaa => 1,bbb => 2);
462 my $tb = Valiant::HTML::Util::FormTags->new(view => $view);
463  
464 =head1 DESCRIPTION
465  
466 Functions that generate HTML tags, specifically those around HTML forms. Not all
467 HTML tags are in this library, this focuses on things that would be useful for building
468 HTML forms. In general this is a support libary for L<Valiant::HTML::FormBuilder> but
469 there's nothing preventing you from using these stand alone, particularly when you have
470 very complex form layout needs.
471  
472 Requiers a 'view' or template object that supports methods for created 'safe' strings that
473 are properly marked as safe for HTML display.
474  
475 =head1 ATTRIBUTES
476  
477 This class has the following initialization attributes
478  
479 =head2 view
480  
481 Object, Required. This should be an object that provides methods for creating escaped
482 strings for HTML display. Many template systems provide a way to mark strings as safe
483 for display, such as L<Mojo::Template>. You will need to add the following proxy methods
484 to your view / template to adapt it for use in creating safe strings.
485  
486 =over
487  
488 =item raw
489  
490 given a string return a single tagged object which is marked as safe for display. Do not do any HTML
491 escaping on the string. This is used when you want to pass strings straight to display and that you
492 know is safe. Be careful with this to avoid HTML injection attacks.
493  
494 =item safe
495  
496 given a string return a single tagged object which is marked as safe for display. First HTML escape the
497 string as safe unless its already been done (no double escaping).
498  
499 =item safe_concat
500  
501 Same as C<safe> but instead works an an array of strings (or mix of strings and safe string objects) and
502 concatenates them all into one big safe marked string.
503  
504 =item html_escape
505  
506 Given a string return string that has been HTML escaped.
507  
508 =item read_attribute_for_html
509  
510 Given an attribute name return the value that the view has defined for it.
511  
512 =item attribute_exists_for_html
513  
514 Given an attribute name return true if the view has defined it.
515  
516 =back
517  
518 Both C<raw>, C<safe> and C<safe_concat> should return a 'tagged' object which is specific to your view or
519 template system. However this object must 'stringify' to the safe version of the string to be displayed. See
520 L<Valiant::HTML::SafeString> for example API. We use L<Valiant::HTML::SafeString> internally to provide
521 safe escaping if you're view doesn't do automatic escaping, as many older template systems like Template
522 Toolkit.
523  
524 See L<Valiant::HTML::Util::View> for a simple view object that provides these methods.
525  
526 =head1 INHERITANCE
527  
528 This class extends L<Valiant::HTML::Util::TagBuilder> and inherits all methods from that class.
529  
530 =head1 METHODS
531  
532 The following instance methods are supported by this class
533  
534 =head2 field_value
535  
536 my $model_value = $tb->field_value($model, $attribute, \%options);
537  
538 For a given model return the value for an attribute. This finds the value by first looking for
539 a method on the model called C<read_attribute_for_html> and if that doesn't exist it tries
540 looking for a method called C<$attribute> on the model.
541  
542 If neither work the value is '' (empty string) if option C<allow_method_names_outside_object>
543 is passed to the C<$tb> object and is true or if its false (or {strict=>1} is $passed to
544 %options) you get an exception.
545  
546 =head2 field_id
547  
548 The field_id method is used within form builders to generate a consistent and unique id attribute
549 for form fields based on the object's name and the attribute's name. This method is particularly
550 helpful when you need to create custom form fields or labels manually and want to ensure they
551 follow conventions.
552  
553 my $id = $tb->field_id($model_or_model_name, $attribute, \%options, @extra);
554  
555 Argument are:
556  
557 =over
558  
559 =item $model_or_model_name
560  
561 Either a model object or the string name of the model.
562  
563 =item $attribute
564  
565 The attribute on the model we are creating an id for.
566  
567 =item \%options
568  
569 A hashref of options. Two are supported. C<namespace> which prepends a string to the start of
570 the id, and C<index> which is a number indicating the index of a nested form.
571  
572 =item @extra
573  
574 An array of extra strings to be added to the end of the id.
575  
576 =back
577  
578 =head2 field_name
579  
580 Similar to C<field_id> but for creating a unique form field name
581  
582 =head3 field_errors
583  
584 my @errors = $tb->field_errors($model, $attribute, $strict);
585  
586 An array of error messages for a field. Requires the model to either respond to
587 C<read_attribute_errors_for> or have an C<errors> method (which is a L<Valiant::Errors> object)
588 for retrieving errors associated with the specified attribute. If the model does neither, it will
589 always return an empty string unless C<$strict> is true in which case it will throw an exception.
590  
591 =head2 button_tag
592  
593 $tb->button_tag($content_string, \%attrs)
594 $tb->button_tag($content_string)
595 $tb->button_tag(\%attrs, \&content_code_block)
596 $tb->button_tag(\&content_code_block)
597  
598 Creates a button element that defines a submit button, reset button or a generic button which
599 can be used in JavaScript, for example. You can use the button tag as a regular submit tag but
600 it isn't supported in legacy browsers. However, the button tag does allow for richer labels
601 such as images and emphasis, so this helper will also accept a block. By default, it will create
602 a button tag with type submit, if type is not given. HTML attribute C<name> defaults to
603 'Button' if not supplied. Inner content also defaults to 'Button' if not supplied.
604  
605 =head2 checkbox_tag
606  
607 $tb->checkbox_tag($name)
608 $tb->checkbox_tag($name, $value)
609 $tb->checkbox_tag($name, $value, $checked)
610 $tb->checkbox_tag($name, \%attrs)
611 $tb->checkbox_tag($name, $value, \%attrs)
612 $tb->checkbox_tag($name, $value, $checked, \%attrs)
613  
614 Creates a check box form input tag. C<id> will be generated from $name if not passed in \%attrs. C<value>
615 attribute will default to '1' and the control is unchecked by default.
616  
617 =head2 fieldset_tag
618  
619 $tb->fieldset_tag(\%content_block)
620 $tb->fieldset_tag(\%attrs, \%content_block)
621 $tb->fieldset_tag($legend, \%attrs, \%content_block)
622 $tb->fieldset_tag($legend, \%content_block)
623  
624 Create a C<fieldset> with inner content. Example:
625  
626 $tb->fieldset_tag(sub {
627 $tb->button_tag('username');
628 });
629  
630 # <fieldset><button name="button">username</button></fieldset>
631
632 $tb->fieldset_tag('Info', sub {
633 $tb->button_tag('username');
634 });
635  
636 # <fieldset><legend>Info</legend><button name="button">username</button></fieldset>
637  
638 =head2 legend_tag
639  
640 $tb->legend_tag($legend, \%html_attrs);
641 $tb->legend_tag($legend);
642 $tb->legend_tag(\%html_attrs, \&content_block);
643 $tb->legend_tag(\&content_block);
644  
645 Create an HTML form legend tag and content. Examples:
646  
647 $tb->legend_tag('test', +{class=>'foo'});
648 # <legend class="foo">test</legend>
649  
650 $tb->legend_tag('test');
651 # <legend>test</legend>
652  
653 $tb->legend_tag({class=>'foo'}, sub { 'test' });
654 # <legend class="foo">test</legend>
655  
656 $tb->legend_tag(sub { 'test' });
657 # <legend>test</legend>
658  
659 =head2 form_tag
660  
661 $tb->form_tag('/signup', \%attrs, \&content)
662 $tb->form_tag(\@args, \%attrs, \&content)
663  
664 Create a form tag with inner content. Example:
665  
666 $tb->form_tag('/user', +{ class=>'form' }, sub {
667 $tb->checkbox_tag('person[1]username', +{class=>'aaa'});
668 });
669  
670 Produces:
671  
672 <form accept-charset="UTF-8" action="/user" class="form" method="POST">
673 <input class="aaa" id="person_1username" name="person[1]username" type="checkbox" value="1"/>
674 </form>';
675  
676 In general C<\%attrs> are expected to be HTML attributes. However, the following special attributes
677 are supported:
678  
679 =over 4
680  
681 =item csrf_token
682  
683 If set, will use the value given to generate a hidden input tag with the name C<csrf_token> and the value.
684 Example:
685  
686 my $csrf_token = "1234567890";
687 $tb->form_tag('/user', +{ csrf_token=>$csrf_token }, sub {
688 $tb->checkbox_tag('username', +{class=>'aaa'});
689 });
690  
691 Produces:
692
693 <form accept-charset="UTF-8" action="/user" class="form" method="POST">
694 <input class="aaa" id="username" name="username" type="checkbox" value="1"/>
695 <input name="csrf_token" type="hidden" value="1234567890"/>
696 </form>';
697  
698 =item tunneled_method
699  
700 If set, will change any C<method> attribute to C<POST> and add a query parameter to
701 the action URL with the name C<_method> and the value of the C<method> attribute. Example:
702  
703 $tb->form_tag('/user', +{ method=>'DELETE', tunneled_method=>1 }, sub {
704 $tb->checkbox_tag('username', +{class=>'aaa'});
705 });
706  
707 Produces:
708
709 <form accept-charset="UTF-8" action="/user?_method=DELETE" class="form" method="POST">
710 <input class="aaa" id="username" name="username" type="checkbox" value="1"/>
711 </form>';
712  
713 Useful for browsers that don't support C<PUT> or C<DELETE> methods in forms (which is most of them).
714  
715 =back
716  
717 These special attributes will be removed from the attributes list before generating the HTML tag.
718  
719 =head2 label_tag
720  
721 $tb->label_tag($name, $content, \%attrs);
722 $tb->label_tag($name, $content);
723 $tb->label_tag($name, \%attrs, \&content);
724 $tb->label_tag($name, \&content);
725  
726 Create a label tag where $name is set to the C<for> attribute. Can contain string or block contents. Label
727 contents default to something based on $name;
728  
729 $tb->label_tag('user_name', "User", +{id=>'userlabel'}); # <label id='userlabel' for='user_name'>User</label>
730 $tb->label_tag('user_name'); # <label for='user_name'>User Name</label>
731  
732 Example with block content:
733  
734 $tb->label_tag('user_name', sub {
735 'User Name Active',
736 $tb->checkbox_tag('active', 'yes', 1);
737 });
738  
739 <label for='user_name'>
740 User Name Active<input checked value="yes" id="user_name" name="user_name" type="checkbox"/>
741 </label>
742  
743 Produce a label tag, often linked to an input tag. Can accept a block coderef. Examples:
744  
745 =head2 radio_button_tag
746  
747 $tb->radio_button_tag($name, $value)
748 $tb->radio_button_tag($name, $value, $checked)
749 $tb->radio_button_tag($name, $value, $checked, \%attrs)
750 $tb->radio_button_tag($name, $value, \%attrs)
751  
752 Creates a radio button; use groups of radio buttons named the same to allow users to select from a
753 group of options. Examples:
754  
755 $tb->radio_button_tag('role', 'admin', 0, +{ class=>'radio' });
756 # <input class="radio" id="role_admin" name="role" type="radio" value="admin"/>
757  
758 $tb->radio_button_tag('role', 'user', 1, +{ class=>'radio' });
759 # <input checked class="radio" id="role_user" name="role" type="radio" value="user"/>'
760  
761 =head2 option_tag
762  
763 $tb->option_tag($text, \%attributes)
764 $tb->option_tag($text)
765  
766 Create a single HTML option. C<value> attribute is inferred from $text if not in \%attributes. Examples:
767  
768 $tb->option_tag('test', +{class=>'foo', value=>'100'});
769 # <option class="foo" value="100">test</option>
770  
771 $tb->option_tag('test'):
772 #<option value="test">test</option>
773  
774 =head2 text_area_tag
775  
776 $tb->text_area_tag($name, \%attrs)
777 $tb->text_area_tag($name, $content, \%attrs)
778  
779 Create a named text_area form field. Examples:
780  
781 $tb->text_area_tag("user", "hello", +{ class=>'foo' });
782 # <textarea class="foo" id="user" name="user">hello</textarea>
783  
784 $tb->text_area_tag("user", +{ class=>'foo' });
785 # <textarea class="foo" id="user" name="user"></textarea>
786  
787 =head2 input_tag
788  
789 $tb->input_tag($name, $value, \%attrs)
790 $tb->input_tag($name, $value)
791 $tb->input_tag($name)
792 $tb->input_tag($name, \%attrs)
793 $tb->input_tag(\%attrs)
794  
795 Create a HTML input tag. If $name and/or $value are set, they are used to populate the 'name' and
796 'value' attributes of the C<input>. Anything passed in the \%attrs hashref overrides. Examples:
797  
798 $tb->input_tag('username', 'jjn', +{class=>'aaa'});
799 # <input class="aaa" id="username" name="username" type="text" value="jjn"/>
800  
801 $tb->input_tag('username', 'jjn');
802 # <input id="username" name="username" type="text" value="jjn"/>
803  
804 $tb->input_tag('username');
805 # <input id="username" name="username" type="text"/>
806  
807 $tb->input_tag('username', +{class=>'foo'});
808 # <input class="foo" id="username" name="username" type="text"/>
809  
810 $tb->input_tag(+{class=>'foo'});
811 # <input class="foo" type="text"/>
812  
813 =head2 password_tag
814  
815 Creates an password input tag with the given type. Example:
816  
817 $tb->password_tag('password', +{class=>'foo'});
818 # <input class="foo" id="password" name="password" type="password"/>
819  
820 =head2 hidden_tag
821  
822 Creates an input tag with the given type. Example:
823  
824 $tb->hidden_tag('user_id', 100, +{class=>'foo'});
825 # <input class="foo" id="user_id" name="user_id" type="hidden" value="100"/>
826
827 =head2 submit_tag
828  
829 $tb->submit_tag
830 $tb->submit_tag($value)
831 $tb->submit_tag(\%attrs)
832 $tb->submit_tag \($value, \%attrs)
833  
834 Create a submit tag. Examples:
835  
836 $tb->submit_tag;
837 # <input id="commit" name="commit" type="submit" value="Save changes"/>
838  
839 $tb->submit_tag('person');
840 # <input id="commit" name="commit" type="submit" value="person"/>
841  
842 $tb->submit_tag('Save', +{name=>'person'});
843 # <input id="person" name="person" type="submit" value="Save"/>
844  
845 $tb->submit_tag(+{class=>'person'});
846 # <input class="person" id="commit" name="commit" type="submit" value="Save changes"/>
847  
848 =head2 select_tag
849  
850 $tb->select_tag($name, $option_tags, \%attrs)
851 $tb->select_tag($name, $option_tags)
852 $tb->select_tag($name, \%attrs)
853  
854 Create a select tag group with options. Examples:
855  
856 $tb->select_tag("people", raw("<option>David</option>"));
857 # <select id="people" name="people"><option>David</option></select>
858  
859 $tb->select_tag("people", raw("<option>David</option>"), +{include_blank=>1});
860 # <select id="people" name="people"><option label=" " value=""></option><option>David</option></select>
861  
862 $tb->select_tag("people", raw("<option>David</option>"), +{include_blank=>'empty'});
863 # <select id="people" name="people"><option value="">empty</option><option>David</option></select>
864
865 $tb->select_tag("prompt", raw("<option>David-prompt</option>"), +{prompt=>'empty-prompt', class=>'foo'});
866 # <select class="foo" id="prompt" name="prompt"><option value="">empty-prompt</option><option>David-prompt</option></select>
867  
868 =head2 options_for_select
869  
870 $tb->options_for_select([$value1, $value2, ...], $selected_value)
871 $tb->options_for_select([$value1, $value2, ...], \@selected_values)
872 $tb->options_for_select([$value1, $value2, ...], +{ selected => $selected_value, disabled => \@disabled_values, %global_options_attributes })
873 $tb->options_for_select([ [$label, $value], [$label, $value, \%attrs], ...])
874  
875 Create a string of HTML option tags suitable for using with C<select_tag>. Accepts two arguments the
876 first of whuch is required. The first argument is an arrayref of values used for the options. Each value
877 can be one of a scalar (in which case the value is used as both the text label for the option as well as
878 its actual value attribute) or a arrayref where the first item is the option label, the second is the value
879 and an option third is a hashref used to add custom attributes to the option.
880  
881 The second (optional) argument lets you set which options are marked C<selected> and possible C<disabled>
882 If the second argument is a scalar value then it is used to mark that value as selected. If its an arrayref
883 then all matching values are selected. If its a hashref we look for a key C<selected> and key <disabled>
884 and expect those (if exists) to be an arrayref of matching values. Any additional keys in the hash will be
885 passed as global HTML attributes to the options. Examples:
886  
887 $tb->options_for_select(['A','B','C']);
888 # <option value="A">A</option>
889 # <option value="B">B</option>
890 # <option value="C">C</option>
891  
892 $tb->options_for_select(['A','B','C'], 'B');
893 # <option value="A">A</option>
894 # <option selected value="B">B</option>
895 # <option value="C">C</option>
896  
897 $tb->options_for_select(['A','B','C'], ['A', 'C']);
898 #<option selected value="A">A</option>
899 #<option value="B">B</option>
900 #<option selected value="C">C</option>
901  
902 $tb->options_for_select(['A','B','C'], ['A', 'C']);
903 # <option selected value="A">A</option>
904 # <option value="B">B</option>
905 # <option selected value="C">C</option>
906  
907 $tb->options_for_select([[a=>'A'],[b=>'B'], [c=>'C']]);
908 # <option value="A">a</option>
909 # <option value="B">b</option>
910 # <option value="C">c</option>
911  
912 $tb->options_for_select([[a=>'A'],[b=>'B'], [c=>'C']], 'B');
913 # <option value="A">a</option>
914 # <option selected value="B">b</option>
915 # <option value="C">c</option>
916  
917 $tb->options_for_select(['A',[b=>'B', {class=>'foo'}], [c=>'C']], ['A','C']);
918 # <option selected value="A">A</option>
919 # <option class="foo" value="B">b</option>
920 # <option selected value="C">c</option>
921  
922 $tb->options_for_select(['A','B','C'], +{selected=>['A','C'], disabled=>['B'], class=>'foo'});
923 # <option class="foo" selected value="A">A</option>
924 # <option class="foo" disabled value="B">B</option>
925 # <option class="foo" selected value="C">C</option>
926  
927 This function is useful with the C<select_tag>:
928  
929 $tb->select_tag("state", options_for_select(['A','B','C'], 'A'), +{include_blank=>1});
930 # <select id="state" name="state">
931 # <option label=" " value=""></option>
932 # <option selected value="A">A</option>
933 # <option value="B">B</option>
934 # <option value="C">C</option>
935 # </select>
936  
937 Please note that since C<options_for_select> returns a L<Valiant::HTML::SafeString> or safe string object
938 as supported by your view/template system of choice you don't need to add any additional escaping.
939  
940 =head2 options_from_collection_for_select
941  
942 Given a collection (an object that does the interface defined by L<Valiant::HTML::Util::Collection> return
943 a string of options suitable for C<select_tag>. Optionally you can pass additional arguments, like with
944 C<options_for_select> to mark individual options as selected, disabled and to pass additional HTML attributes.
945 Examples:
946  
947 my $collection = Valiant::HTML::Util::Collection->new([label=>'value'], [A=>'a'], [B=>'b'], [C=>'c']);
948  
949 $tb->options_from_collection_for_select($collection, 'value', 'label');
950 # <option value="value">label</option>
951 # <option value="a">A</option>
952 # <option value="b">B</option>
953 # <option value="c">C</option>
954  
955 $tb->options_from_collection_for_select($collection, 'value', 'label', 'a');
956 # <option value="value">label</option>
957 # <option selected value="a">A</option>
958 # <option value="b">B</option>
959 # <option value="c">C</option>
960  
961 $tb->options_from_collection_for_select($collection, 'value', 'label', ['a', 'c']);
962 # <option value="value">label</option>
963 # <option selected value="a">A</option>
964 # <option value="b">B</option>
965 # <option selected value="c">C</option>
966  
967 $tb->options_from_collection_for_select($collection, 'value', 'label', +{selected=>['a','c'], disabled=>['b'], class=>'foo'})
968 # <option class="foo" value="value">label</option>
969 # <option class="foo" selected value="a">A</option>
970 # <option class="foo" disabled value="b">B</option>
971 # <option class="foo" selected value="c">C</option>
972  
973 If you have a properly formed array you can use the C<array_to_collection> helper method:
974  
975 my $collection = $tb->array_to_collection([label=>'value'], [A=>'a'], [B=>'b'], [C=>'c']);
976 $tb->options_from_collection_for_select($collection, 'value', 'label');
977  
978 Additionally you can pass a coderef for dynamic selecting. Example:
979  
980 $tb->options_from_collection_for_select($collection, 'value', 'label', sub { shift->value eq 'a'} );
981 # <option value="value">label</option>
982 # <option selected value="a">A</option>
983 # <option value="b">B</option>
984 # <option value="c">C</option>
985  
986 The collection object must at minimum provide a method C<next> which returns the next object in the collection.
987 This method C<next> should return false when all the item objects have been iterated thru in turn. Optionally
988 you can provide a C<reset> method which will be called to return the collection to the first index.
989  
990 You can see L<Valiant::HTML::Util::Collection> source for example minimal code.
991  
992 =head2 array_to_collection
993  
994 Given a array of option labels as described above, create a collection
995  
996 =head1 SEE ALSO
997
998 L<Valiant>, L<Valiant::HTML::FormBuilder>
999  
1000 =head1 AUTHOR
1001
1002 See L<Valiant>
1003  
1004 =head1 COPYRIGHT & LICENSE
1005
1006 See L<Valiant>
1007  
1008 =cut
1009  
1010 3 1;
« Valiant/Error.pm ↑ Summary Valiant/Validator/Length.pm »