Yancy/Plugin/Form/Bootstrap4.pm

filter: Statementstmt 93.9% 77 / 82 Branchbran 88.6% 39 / 44 Conditioncond 79.5% 35 / 44 MC/DCmcdc 66.6% 16 / 24 Subroutinesub 100% 7 / 7 Podpod 16.6% 1 / 6 totaltotal 84.5% 175 / 207 CC 42 SCAR 26.7
CC
42
Cov
89%
input
22.0
form_for
12.1
input_for
7.5
CRAP 14.5
« Yancy/Plugin/Editor.pm ↑ Summary Yancy/Backend/Memory.pm »
1 package Yancy::Plugin::Form::Bootstrap4;
2 20 our $VERSION = '1.088';
3 # ABSTRACT: Generate forms using Bootstrap 4
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod use Mojolicious::Lite;
8 #pod plugin Yancy => {
9 #pod backend => 'pg://localhost/mysite',
10 #pod read_schema => 1,
11 #pod };
12 #pod app->yancy->plugin( 'Form::Bootstrap4' );
13 #pod # See Yancy::Controller::Yancy for routing
14 #pod app->routes->get( '/people/:id/edit' )->to(
15 #pod 'yancy#set',
16 #pod schema => 'people',
17 #pod template => 'edit_people',
18 #pod );
19 #pod app->start;
20 #pod __DATA__
21 #pod @@ edit_people.html.ep
22 #pod %= $c->yancy->form->form_for( 'people' );
23 #pod
24 #pod =head1 DESCRIPTION
25 #pod
26 #pod This plugin generates forms using the L<Bootstrap
27 #pod 4.0|https://getbootstrap.com/docs/4.0/>.
28 #pod
29 #pod For details on the helpers added by this plugin, see
30 #pod L<Yancy::Plugin::Form>.
31 #pod
32 #pod =head1 TEMPLATES
33 #pod
34 #pod To override these templates, add your own at the designated path inside
35 #pod your app's C<templates/> directory.
36 #pod
37 #pod =head2 yancy/form/bootstrap4/form.html.ep
38 #pod
39 #pod This template surrounds the form to create the C<< <form> >> element,
40 #pod list all the fields, and add a submit button. Also includes a CSRF token.
41 #pod
42 #pod =head2 yancy/form/bootstrap4/field.html.ep
43 #pod
44 #pod This template surrounds a single form field to add a C<< <label> >>
45 #pod element, the appropriate input element(s), and the optional description.
46 #pod
47 #pod =head2 yancy/form/bootstrap4/input.html.ep
48 #pod
49 #pod This template is for single C<< <input> >> elements and the attributes
50 #pod they need. This is used by field types C<string>, C<number>, C<integer>,
51 #pod and string fields with formats C<email> and C<date-time>.
52 #pod
53 #pod =head2 yancy/form/bootstrap4/textarea.html.ep
54 #pod
55 #pod This template is for single C<< <textarea> >> elements and the
56 #pod attributes they need. This is used by fields with type C<string> and
57 #pod format C<textarea>.
58 #pod
59 #pod =head2 yancy/form/bootstrap4/select.html.ep
60 #pod
61 #pod This template is for single C<< <select> >> elements and the attributes
62 #pod they need. This is used by fields with an C<enum> property.
63 #pod
64 #pod =head2 yancy/form/bootstrap4/yesno.html.ep
65 #pod
66 #pod This template displays a yes/no toggle used for fields of type
67 #pod C<boolean>.
68 #pod
69 #pod =head2 yancy/form/bootstrap4/readonly.html.ep
70 #pod
71 #pod This template displays all read-only fields. This template must not include
72 #pod any kind of C<< <input> >>, C<< <select> >>, or other form fields.
73 #pod
74 #pod =head1 SEE ALSO
75 #pod
76 #pod L<Yancy::Plugin::Form>, L<Yancy>
77 #pod
78 #pod =cut
79  
80 20 ▶use Mojo::Base 'Yancy::Plugin::Form';
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 53 ▶sub register {
Subroutine register: called CC=1 SCAR=0.0
Pod documented
83 53 my ( $self, $app, $conf ) = @_;
84 53 $self->SUPER::register( $app, $conf );
85 53 push @{$app->renderer->classes}, __PACKAGE__;
86 }
87  
88 90 ▶sub input {
Subroutine input: called CC=22 SCAR=30.9
Pod undocumented
89 90 my ( $self, $c, %opt ) = @_;
90  
91 90 ▶ die "input name is required" unless $opt{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"name"} 1 89 90
92 89 ▶ $opt{value} //= $c->req->param( $opt{name} );
Condition66%2/3
$opt{"value"} //= $c->req->param($opt{"name"})
l!l&&r!l&&!r
39050
MC/DC50%1/2
$opt{"value"} //= $c->req->param($opt{"name"})
$opt{"value"} $c->req->param($opt{"name"})
Truth table66%2/3
A //= B
A= $opt{"value"}B= $c->req->param($opt{"name"})
ABresult
1X1
011
000
93  
94 89 my %attr;
95 89 my $template = 'input';
96 my $type = !$opt{type} ? 'string'
97 : ref $opt{type} eq 'ARRAY' ? $opt{type}[0]
98 89 ▶ : $opt{type};
Branch: 2 branches
BranchTrueFalseTotal
!$opt{'type'} ? : 12 77 89
ref $opt{'type'} eq 'ARRAY' ? : 9 68 77
99 89 ▶ my $format = delete $opt{format} // '';
Condition100%2/2
delete $opt{"format"} // ""
l!l
4148
MC/DC100%1/1
delete $opt{"format"} // ""
delete $opt{"format"}
Truth table100%2/2
A
A= delete $opt{"format"}
Aresult
11
00
100  
101 89 ▶ if ( $opt{ enum } ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($opt{'enum'}) { } 8 81 89
elsif ($type eq 'boolean') { } 2 79 81
elsif ($type eq 'integer' or $type eq 'number') { } 12 67 79
elsif ($type eq 'string') { } 67 0 67
102 8 $template = 'select';
103 }
104 elsif ( $type eq 'boolean' ) {
105 2 $template = 'yesno';
106 }
107 ▶ elsif ( $type eq 'integer' || $type eq 'number' ) {
Condition100%3/3
$type eq "integer" or $type eq "number"
l!l&&r!l&&!r
11167
MC/DC100%2/2
$type eq "integer" or $type eq "number"
$type eq "integer" $type eq "number"
Truth table100%3/3
A or B
A= $type eq "integer"B= $type eq "number"
ABresult
1X1
011
000
108 12 $attr{ type } = 'number';
109 12 $attr{ inputmode } = 'decimal';
110 12 ▶ if ( $type eq 'integer' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "integer") 11 1 12
111 # Enable numeric input mode on iOS with pattern attribute
112 # https://css-tricks.com/finger-friendly-numerical-inputs-with-inputmode/
113 11 ▶ $opt{ pattern } ||= '[0-9]*';
Condition50%1/2
$opt{"pattern"} ||= "[0-9]*"
l!l
011
MC/DC0%0/1
$opt{"pattern"} ||= "[0-9]*"
$opt{"pattern"}
Truth table50%1/2
A
A= $opt{"pattern"}
Aresult
11
00
114 11 $attr{ inputmode } = 'numeric';
115 }
116 }
117 elsif ( $type eq 'string' ) {
118 67 ▶ $attr{ type } = $format || 'text';
Condition100%2/2
$format || 'text'
l!l
4126
MC/DC100%1/1
$format || 'text'
$format
Truth table100%2/2
A
A= $format
Aresult
11
00
119 67 ▶ if ( $format eq 'textarea' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($format eq 'textarea') { } 4 63 67
elsif ($format eq 'email') { } 14 49 63
elsif ($format eq 'date-time') { } 7 42 49
elsif ($format eq 'markdown') { } 1 41 42
elsif ($format eq 'password') { } 9 32 41
120 4 $template = 'textarea';
121 }
122 elsif ( $format eq 'email' ) {
123 14 ▶ $opt{ pattern } ||= '[^@]+@[^@]+';
Condition100%2/2
$opt{"pattern"} ||= "[^\@]+\@[^\@]+"
l!l
131
MC/DC100%1/1
$opt{"pattern"} ||= "[^\@]+\@[^\@]+"
$opt{"pattern"}
Truth table100%2/2
A
A= $opt{"pattern"}
Aresult
11
00
124 14 $attr{ inputmode } = 'email';
125 }
126 elsif ( $format eq 'date-time' ) {
127 7 $attr{ type } = 'datetime-local';
128 }
129 elsif ( $format eq 'markdown' ) {
130 1 $template = 'markdown';
131 }
132 elsif ( $format eq 'password' ) {
133 9 delete $opt{ value };
134 }
135 }
136  
137 89 ▶ if ( my $readonly = ( delete $opt{ readOnly } or delete $opt{ readonly } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $readonly = delete $opt{"readOnly"} || delete $opt{"readonly"}) 5 84 89
Condition100%3/3
delete $opt{'readOnly'} || delete $opt{'readonly'}
l!l&&r!l&&!r
4184
MC/DC100%2/2
delete $opt{'readOnly'} || delete $opt{'readonly'}
delete $opt{'readOnly'} delete $opt{'readonly'}
Truth table100%3/3
A || B
A= delete $opt{'readOnly'}B= delete $opt{'readonly'}
ABresult
1X1
011
000
138 5 $template = 'readonly';
139 }
140  
141 # ; use Data::Dumper;
142 # ; say Dumper \%opt;
143 # ; say Dumper \%attr;
144  
145 89 my $path = join '/', qw( yancy form bootstrap4 ), $template;
146 89 my $html = $c->render_to_string(
147 template => $path,
148 input => { %opt, %attr },
149 );
150  
151 # ; say $html;
152  
153 89 return $html;
154 }
155  
156 4 ▶sub input_for {
Subroutine input_for: called CC=5 SCAR=20.2
Pod undocumented
157 4 my ( $self, $c, $coll, $prop, %opt ) = @_;
158 4 my $schema = $c->yancy->schema( $coll );
159 0 ▶ my $required = exists $opt{required} ? $opt{required} : !!grep { $_ eq $prop } @{ $schema->{required} // [] };
Branch: 1 branch
BranchTrueFalseTotal
exists $opt{'required'} ? : 4 0 4
Condition0%0/2
$schema->{"required"} // []
l!l
00
MC/DC0%0/1
$schema->{"required"} // []
$schema->{"required"}
Truth table0%0/2
A
A= $schema->{"required"}
Aresult
11
00
160  
161 4 my $field = $schema->{properties}{ $prop };
162 my $input = $self->input( $c,
163 %$field,
164 ( type => $opt{type} )x!!exists $opt{type},
165 ( enum => $opt{enum} )x!!exists $opt{enum},
166 ( format => $opt{format} )x!!exists $opt{format},
167 ( enum_labels => $opt{ enum_labels } )x!!exists $opt{enum_labels},
168 name => $prop,
169 value => $opt{value},
170 ( $required ? ( required => $required ) : () ),
171 4 ▶ ( $opt{class} ? ( class => $opt{class} ) : () ),
Branch: 2 branches
BranchTrueFalseTotal
$required ? : 0 4 4
$opt{'class'} ? : 0 4 4
172 );
173 }
174  
175 4 ▶sub filter_for {
Subroutine filter_for: called CC=3 SCAR=11.9
Pod undocumented
176 4 my ( $self, $c, $coll, $prop, %opt ) = @_;
177 4 my $schema = $c->yancy->schema( $coll );
178 4 my $field = $schema->{properties}{ $prop };
179 4 ▶ if ( $field->{type} eq 'boolean' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->{"type"} eq "boolean") 0 4 4
180 0 $opt{ type } = 'string';
181 0 $opt{ enum } = [ undef, 1, 0 ];
182 0 $opt{ enum_labels } = [ undef, 'Yes', 'No' ];
183 }
184 4 $opt{format} = undef;
185 4 ▶ $opt{ value } //= $c->req->param( $prop );
Condition66%2/3
$opt{"value"} //= $c->req->param($prop)
l!l&&r!l&&!r
013
MC/DC50%1/2
$opt{"value"} //= $c->req->param($prop)
$opt{"value"} $c->req->param($prop)
Truth table66%2/3
A //= B
A= $opt{"value"}B= $c->req->param($prop)
ABresult
1X1
011
000
186 4 $opt{required} = 0;
187 4 return $self->input_for( $c, $coll, $prop, %opt );
188 }
189  
190 68 ▶sub field_for {
Subroutine field_for: called CC=4 SCAR=13.9
Pod undocumented
191 68 my ( $self, $c, $coll, $prop, %opt ) = @_;
192  
193 # ; use Data::Dumper;
194 # ; say Dumper \%opt;
195 68 my $schema = $c->yancy->schema( $coll );
196 68 ▶ my $required = !!grep { $_ eq $prop } @{ $schema->{required} // [] };
Condition50%1/2
$schema->{"required"} // []
l!l
680
MC/DC0%0/1
$schema->{"required"} // []
$schema->{"required"}
Truth table50%1/2
A
A= $schema->{"required"}
Aresult
11
00
197  
198 68 my $field = $schema->{properties}{ $prop };
199 my $input = $self->input( $c,
200 %$field,
201 name => $prop,
202 value => $opt{value},
203 id => "field-$coll-$prop",
204 ( $required ? ( required => $required ) : () ),
205 68 ▶ ( $opt{class} ? ( class => $opt{class} ) : () ),
Branch: 2 branches
BranchTrueFalseTotal
$required ? : 33 35 68
$opt{'class'} ? : 1 67 68
206 );
207  
208 68 my $path = join '/', qw( yancy form bootstrap4 field );
209 68 my $html = $c->render_to_string(
210 template => $path,
211 field => {
212 %$field,
213 %opt,
214 id => "field-$coll-$prop",
215 name => $prop,
216 required => $required,
217 content => $input,
218 },
219 );
220  
221 # ; say $html;
222  
223 68 return $html;
224 }
225  
226 8 ▶sub form_for {
Subroutine form_for: called CC=12 SCAR=24.9
Pod undocumented
227 8 my ( $self, $c, $coll, %opt ) = @_;
228  
229 8 ▶ my $item = $opt{ item } || $c->stash( 'item' ) || {};
Condition100%5/5
$opt{'item'} || $c->stash('item') || {}
l!l
53
$opt{"item"} or $c->stash("item")
l!l&&r!l&&!r
233
MC/DC100%2/2
$opt{'item'} || $c->stash('item') || {}
$opt{"item"} $c->stash("item")
Truth table100%3/3
A or B
A= $opt{"item"}B= $c->stash("item")
ABresult
1X1
011
000
230 8 my $props = $c->yancy->schema( $coll )->{properties};
231  
232 my $prop_names = $opt{properties} // $c->stash( 'properties' )
233 # By default, do not show read-only fields
234 8 ▶ // [ grep !$props->{$_}{readOnly}, keys %$props ];
Condition100%5/5
($opt{"properties"} // $c->stash("properties")) // [grep((!$props->{$_}{"readOnly"}), keys %$props)]
l!l
26
$opt{"properties"} // $c->stash("properties")
l!l&&r!l&&!r
116
MC/DC100%2/2
($opt{"properties"} // $c->stash("properties")) // [grep((!$props->{$_}{"readOnly"}), keys %$props)]
$opt{"properties"} $c->stash("properties")
Truth table100%3/3
A // B
A= $opt{"properties"}B= $c->stash("properties")
ABresult
1X1
011
000
235 $props = {
236 8 map { $_ => $props->{ $_ } }
237 @$prop_names
238 };
239  
240 my @sorted_props
241 = sort {
242 8 ▶ ( $props->{$a}{'x-order'}//2**31 ) <=> ( $props->{$b}{'x-order'}//2**31 )
Condition50%2/4
$props->{$a}{"x-order"} // "2147483648"
l!l
990
$props->{$b}{"x-order"} // "2147483648"
l!l
990
MC/DC0%0/1
$props->{$a}{"x-order"} // "2147483648"
$props->{$a}{"x-order"}
MC/DC0%0/1
$props->{$b}{"x-order"} // "2147483648"
$props->{$b}{"x-order"}
Truth table50%1/2
A
A= $props->{$a}{"x-order"}
Aresult
11
00
Truth table50%1/2
A
A= $props->{$b}{"x-order"}
Aresult
11
00
243 }
244 keys %$props;
245 8 my @fields;
246 8 for my $field ( @sorted_props ) {
247 my %field_opt = (
248 56 ▶ value => $c->req->param( $field ) // $item->{ $field },
Condition100%3/3
$c->req->param($field) // $item->{$field}
l!l&&r!l&&!r
12530
MC/DC100%2/2
$c->req->param($field) // $item->{$field}
$c->req->param($field) $item->{$field}
Truth table100%3/3
A // B
A= $c->req->param($field)B= $item->{$field}
ABresult
1X1
011
000
249 );
250 56 push @fields, $self->field_for( $c, $coll, $field, %field_opt );
251 }
252  
253 8 ▶ if ( !exists $opt{csrf} || $opt{csrf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $opt{"csrf"} or $opt{"csrf"}) 7 1 8
Condition66%2/3
not exists $opt{"csrf"} or $opt{"csrf"}
l!l&&r!l&&!r
701
MC/DC50%1/2
not exists $opt{"csrf"} or $opt{"csrf"}
not exists $opt{"csrf"} $opt{"csrf"}
Truth table66%2/3
not(A) or B
A= not exists $opt{"csrf"}B= $opt{"csrf"}
ABresult
1X1
011
000
254 # Verify that this form is being built with a real request
255 # so the CSRF field can work correctly
256 7 ▶ if ( !$c->tx->remote_address ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->tx->remote_address) 1 6 7
257 1 $c->log->warn( 'form_for() called with incomplete request. CSRF token may not validate!');
258 }
259 }
260  
261 8 my $path = join '/', qw( yancy form bootstrap4 form);
262 8 my $html = $c->render_to_string(
263 template => $path,
264 form => {
265 method => 'POST',
266 csrf => 1,
267 %opt,
268 fields => \@fields,
269 },
270 );
271  
272 # ; say $html;
273  
274 8 return $html;
275 }
276  
277 =pod
278  
279 =head1 NAME
280  
281 Yancy::Plugin::Form::Bootstrap4 - Generate forms using Bootstrap 4
282  
283 =head1 VERSION
284  
285 version 1.088
286  
287 =head1 SYNOPSIS
288  
289 use Mojolicious::Lite;
290 plugin Yancy => {
291 backend => 'pg://localhost/mysite',
292 read_schema => 1,
293 };
294 app->yancy->plugin( 'Form::Bootstrap4' );
295 # See Yancy::Controller::Yancy for routing
296 app->routes->get( '/people/:id/edit' )->to(
297 'yancy#set',
298 schema => 'people',
299 template => 'edit_people',
300 );
301 app->start;
302 __DATA__
303 @@ edit_people.html.ep
304 %= $c->yancy->form->form_for( 'people' );
305  
306 =head1 DESCRIPTION
307  
308 This plugin generates forms using the L<Bootstrap
309 4.0|https://getbootstrap.com/docs/4.0/>.
310  
311 For details on the helpers added by this plugin, see
312 L<Yancy::Plugin::Form>.
313  
314 =head1 TEMPLATES
315  
316 To override these templates, add your own at the designated path inside
317 your app's C<templates/> directory.
318  
319 =head2 yancy/form/bootstrap4/form.html.ep
320  
321 This template surrounds the form to create the C<< <form> >> element,
322 list all the fields, and add a submit button. Also includes a CSRF token.
323  
324 =head2 yancy/form/bootstrap4/field.html.ep
325  
326 This template surrounds a single form field to add a C<< <label> >>
327 element, the appropriate input element(s), and the optional description.
328  
329 =head2 yancy/form/bootstrap4/input.html.ep
330  
331 This template is for single C<< <input> >> elements and the attributes
332 they need. This is used by field types C<string>, C<number>, C<integer>,
333 and string fields with formats C<email> and C<date-time>.
334  
335 =head2 yancy/form/bootstrap4/textarea.html.ep
336  
337 This template is for single C<< <textarea> >> elements and the
338 attributes they need. This is used by fields with type C<string> and
339 format C<textarea>.
340  
341 =head2 yancy/form/bootstrap4/select.html.ep
342  
343 This template is for single C<< <select> >> elements and the attributes
344 they need. This is used by fields with an C<enum> property.
345  
346 =head2 yancy/form/bootstrap4/yesno.html.ep
347  
348 This template displays a yes/no toggle used for fields of type
349 C<boolean>.
350  
351 =head2 yancy/form/bootstrap4/readonly.html.ep
352  
353 This template displays all read-only fields. This template must not include
354 any kind of C<< <input> >>, C<< <select> >>, or other form fields.
355  
356 =head1 SEE ALSO
357  
358 L<Yancy::Plugin::Form>, L<Yancy>
359  
360 =head1 AUTHOR
361  
362 Doug Bell <preaction@cpan.org>
363  
364 =head1 COPYRIGHT AND LICENSE
365  
366 This software is copyright (c) 2021 by Doug Bell.
367  
368 This is free software; you can redistribute it and/or modify it under
369 the same terms as the Perl 5 programming language system itself.
370  
371 =cut
372  
373 __DATA__
« Yancy/Plugin/Editor.pm ↑ Summary Yancy/Backend/Memory.pm »