File Coverage

blib/lib/Data/HTML/Form.pm
Criterion Covered Total %
statement 29 29 100.0
branch 8 8 100.0
condition n/a
subroutine 9 9 100.0
pod 0 1 0.0
total 46 47 97.8


line stmt bran cond sub pod time code
1              
2             use strict;
3 9     9   94256 use warnings;
  9         71  
  9         266  
4 9     9   50  
  9         18  
  9         311  
5             use Error::Pure qw(err);
6 9     9   4016 use List::Util qw(none);
  9         94998  
  9         197  
7 9     9   4101 use Mo qw(build is);
  9         24  
  9         9262  
8 9     9   5534 use Readonly;
  9         4459  
  9         48  
9 9     9   12790  
  9         20  
  9         2857  
10             Readonly::Array our @METHODS => qw(get post);
11             Readonly::Array our @ENCTYPES => (
12             'application/x-www-form-urlencoded',
13             'multipart/form-data',
14             'text/plain',
15             );
16              
17             our $VERSION = 0.05;
18              
19             has action => (
20             is => 'ro',
21             );
22              
23             has css_class => (
24             is => 'ro',
25             );
26              
27             has enctype => (
28             is => 'ro',
29             );
30              
31             has id => (
32             is => 'ro',
33             );
34              
35             has label => (
36             is => 'ro',
37             );
38              
39             has method => (
40             is => 'ro',
41             );
42              
43             my $self = shift;
44              
45 16     16 0 25866 # Check enctype.
46             if (defined $self->{'enctype'}) {
47             if (none { $self->{'enctype'} eq $_ } @ENCTYPES) {
48 16 100       93 err "Parameter 'enctype' has bad value.",
49 3 100   7   24 'Value', $self->{'enctype'},
  7         78  
50             ;
51 1         5 }
52             }
53              
54             # Check method.
55             if (! defined $self->{'method'}) {
56             $self->{'method'} = 'get';
57 15 100       59 }
58 12         33 if (none { $self->{'method'} eq $_ } @METHODS) {
59             err "Parameter 'method' has bad value.",
60 15 100   18   116 'Value', $self->{'method'};
  18         338  
61             }
62 1         30  
63             return;
64             }
65 14         68  
66             1;
67              
68              
69             =pod
70              
71             =encoding utf8
72              
73             =head1 NAME
74              
75             Data::HTML::Form - Data object for HTML form.
76              
77             =head1 SYNOPSIS
78              
79             use Data::HTML::Form;
80              
81             my $obj = Data::HTML::Form->new(%params);
82             my $action = $obj->action;
83             my $css_class = $obj->css_class;
84             my $enctype = $obj->enctype;
85             my $id = $obj->id;
86             my $label = $obj->label;
87             my $method = $obj->method;
88              
89             =head1 METHODS
90              
91             =head2 C<new>
92              
93             my $obj = Data::HTML::Form->new(%params);
94              
95             Constructor.
96              
97             Returns instance of object.
98              
99             =over 8
100              
101             =item * C<action>
102              
103             Form action.
104              
105             Default value is undef.
106              
107             =item * C<css_class>
108              
109             Form CSS class.
110              
111             Default value is undef.
112              
113             =item * C<enctype>
114              
115             Form enctype, attribute which specifies how the form-data should be encoded when
116             submitting it to the server.
117              
118             Possible values are:
119              
120             =over
121              
122             =item * (undefined - same as application/x-www-form-urlencoded)
123              
124             =item * application/x-www-form-urlencoded
125              
126             =item * multipart/form-data
127              
128             =item * text/plain
129              
130             =back
131              
132             Default value is undef.
133              
134             =item * C<id>
135              
136             Form identifier.
137              
138             Default value is undef.
139              
140             =item * C<label>
141              
142             Form label.
143              
144             Default value is undef.
145              
146             =item * C<method>
147              
148             Form method.
149              
150             Default value is 'get'.
151              
152             Possible methods are: get and post
153              
154             =back
155              
156             =head2 C<action>
157              
158             my $action = $obj->action;
159              
160             Get form action.
161              
162             Returns string.
163              
164             =head2 C<css_class>
165              
166             my $css_class = $obj->css_class;
167              
168             Get CSS class for form.
169              
170             Returns string.
171              
172             =head2 C<enctype>
173              
174             my $enctype = $obj->enctype;
175              
176             Get enctype, attribute which specifies how the form-data should be encoded when
177             submitting it to the server.
178              
179             Returns string.
180              
181             =head2 C<id>
182              
183             Get form identifier.
184              
185             Returns string.
186              
187             =head2 C<label>
188              
189             Get form label.
190              
191             Returns string.
192              
193             =head2 C<method>
194              
195             Get form method.
196              
197             Returns string.
198              
199             =head1 ERRORS
200              
201             new():
202             Parameter 'enctype' has bad value.
203             Value: %s
204             Parameter 'method' has bad value.
205             Value: %s
206              
207             =head1 EXAMPLE1
208              
209             =for comment filename=form_default.pl
210              
211             use strict;
212             use warnings;
213              
214             use Data::HTML::Form;
215              
216             my $obj = Data::HTML::Form->new;
217              
218             # Print out.
219             print 'Method: '.$obj->method."\n";
220              
221             # Output:
222             # Method: get
223              
224             =head1 EXAMPLE2
225              
226             =for comment filename=form.pl
227              
228             use strict;
229             use warnings;
230              
231             use Data::HTML::Form;
232              
233             my $obj = Data::HTML::Form->new(
234             'action' => '/action.pl',
235             'css_class' => 'form',
236             'enctype' => 'multipart/form-data',
237             'id' => 'form-id',
238             'label' => 'Form label',
239             'method' => 'post',
240             );
241              
242             # Print out.
243             print 'Action: '.$obj->action."\n";
244             print 'CSS class: '.$obj->css_class."\n";
245             print 'Enctype: '.$obj->enctype."\n";
246             print 'Id: '.$obj->id."\n";
247             print 'Label: '.$obj->label."\n";
248             print 'Method: '.$obj->method."\n";
249              
250             # Output:
251             # Action: /action.pl
252             # CSS class: form
253             # Enctype: multipart/form-data
254             # Id: form-id
255             # Label: Form label
256             # Method: post
257              
258             =head1 DEPENDENCIES
259              
260             L<Error::Pure>,
261             L<List::Util>,
262             L<Mo>,
263             L<Readonly>.
264              
265             =head1 REPOSITORY
266              
267             L<https://github.com/michal-josef-spacek/Data-HTML-Form>
268              
269             =head1 AUTHOR
270              
271             Michal Josef Špaček L<mailto:skim@cpan.org>
272              
273             L<http://skim.cz>
274              
275             =head1 LICENSE AND COPYRIGHT
276              
277             © 2022 Michal Josef Špaček
278              
279             BSD 2-Clause License
280              
281             =head1 VERSION
282              
283             0.05
284              
285             =cut