line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Chloro::Field; |
2
|
|
|
|
|
|
|
BEGIN { |
3
|
1
|
|
|
1
|
|
19357
|
$Chloro::Field::VERSION = '0.06'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
1427
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use MooseX::StrictConstructor; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use namespace::autoclean; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use Chloro::Types qw( Bool CodeRef NonEmptySimpleStr Str Value ); |
12
|
|
|
|
|
|
|
use Moose::Util::TypeConstraints; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
with 'Chloro::Role::FormComponent'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has type => ( |
17
|
|
|
|
|
|
|
is => 'ro', |
18
|
|
|
|
|
|
|
isa => 'Moose::Meta::TypeConstraint', |
19
|
|
|
|
|
|
|
required => 1, |
20
|
|
|
|
|
|
|
init_arg => 'isa', |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
has default => ( |
24
|
|
|
|
|
|
|
is => 'ro', |
25
|
|
|
|
|
|
|
isa => Value | CodeRef, |
26
|
|
|
|
|
|
|
predicate => 'has_default', |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
has is_required => ( |
30
|
|
|
|
|
|
|
is => 'ro', |
31
|
|
|
|
|
|
|
isa => Bool, |
32
|
|
|
|
|
|
|
init_arg => 'required', |
33
|
|
|
|
|
|
|
default => 0, |
34
|
|
|
|
|
|
|
); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
has is_secure => ( |
37
|
|
|
|
|
|
|
is => 'ro', |
38
|
|
|
|
|
|
|
isa => Bool, |
39
|
|
|
|
|
|
|
init_arg => 'secure', |
40
|
|
|
|
|
|
|
default => 0, |
41
|
|
|
|
|
|
|
); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
has extractor => ( |
44
|
|
|
|
|
|
|
is => 'ro', |
45
|
|
|
|
|
|
|
isa => NonEmptySimpleStr, |
46
|
|
|
|
|
|
|
default => '_extract_field_value', |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
has validator => ( |
50
|
|
|
|
|
|
|
is => 'ro', |
51
|
|
|
|
|
|
|
isa => NonEmptySimpleStr, |
52
|
|
|
|
|
|
|
default => '_errors_for_field_value', |
53
|
|
|
|
|
|
|
); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
override BUILDARGS => sub { |
56
|
|
|
|
|
|
|
my $class = shift; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
my $p = super(); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
$p->{isa} |
61
|
|
|
|
|
|
|
= Moose::Util::TypeConstraints::find_or_create_isa_type_constraint( |
62
|
|
|
|
|
|
|
$p->{isa} ); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
return $p; |
65
|
|
|
|
|
|
|
}; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub generate_default { |
68
|
|
|
|
|
|
|
my $self = shift; |
69
|
|
|
|
|
|
|
my $params = shift; |
70
|
|
|
|
|
|
|
my $prefix = shift; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
my $default = $self->default(); |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
return ref $default |
75
|
|
|
|
|
|
|
? $self->$default( $params, $prefix ) |
76
|
|
|
|
|
|
|
: $default; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# The Storable hooks are needed because the Moose::Meta::TypeConstraint object |
80
|
|
|
|
|
|
|
# contains a code reference, and Storable will just die if it tries to |
81
|
|
|
|
|
|
|
# serialize it. So we save the type's _name_ and look that up when thawing. |
82
|
|
|
|
|
|
|
# |
83
|
|
|
|
|
|
|
# Unfortunately, this requires poking around in the object guts a little bit. |
84
|
|
|
|
|
|
|
sub STORABLE_freeze { |
85
|
|
|
|
|
|
|
my $self = shift; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
my %copy = %{$self}; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
my $type = delete $copy{type}; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
return q{}, \%copy, \( $type->name() ); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub STORABLE_thaw { |
95
|
|
|
|
|
|
|
my $self = shift; |
96
|
|
|
|
|
|
|
shift; |
97
|
|
|
|
|
|
|
shift; |
98
|
|
|
|
|
|
|
my $obj = shift; |
99
|
|
|
|
|
|
|
my $type = shift; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
%{$self} = %{$obj}; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
$self->{type} |
104
|
|
|
|
|
|
|
= Moose::Util::TypeConstraints::find_or_create_type_constraint( ${$type} ); |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
return; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# This exists mostly to make testing easier |
110
|
|
|
|
|
|
|
sub dump { |
111
|
|
|
|
|
|
|
my $self = shift; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
return ( |
114
|
|
|
|
|
|
|
type => $self->type(), |
115
|
|
|
|
|
|
|
required => $self->is_required(), |
116
|
|
|
|
|
|
|
secure => $self->is_secure(), |
117
|
|
|
|
|
|
|
( $self->has_default() ? ( default => $self->default() ) : () ), |
118
|
|
|
|
|
|
|
); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
__PACKAGE__->meta()->make_immutable(); |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
1; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# ABSTRACT: A field in a form |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=pod |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head1 NAME |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Chloro::Field - A field in a form |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head1 VERSION |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
version 0.06 |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head1 SYNOPSIS |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
See L<Chloro>. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 DESCRIPTION |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
This class represents a field in a form. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 METHODS |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
This class has the following methods: |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head2 Chloro::Field->new() |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
You'll probably make fields by using the C<field()> subroutine exported by |
154
|
|
|
|
|
|
|
L<Chloro>, but you can make one using this constructor. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
The constructor accepts the following parameters: |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=over 4 |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=item * name |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
The name of the field. This is required. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=item * human_name |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
A more friendly version of the name. This defaults to the same value as |
167
|
|
|
|
|
|
|
C<name>. This value will be used when generating error messages for this |
168
|
|
|
|
|
|
|
field. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=item * isa |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
This must be a Moose type constraint. You can pass a |
173
|
|
|
|
|
|
|
L<Moose::Meta::TypeConstraint> object, a type name, or a type created with |
174
|
|
|
|
|
|
|
L<MooseX::Types>. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Just like with L<Moose> attributes, an unknown name is treated as a class name. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=item * default |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
The default value for the field. Like Moose attributes, this can either be a |
181
|
|
|
|
|
|
|
non-reference value of a subroutine reference. |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
A subroutine reference will be called as a method on the field object, and |
184
|
|
|
|
|
|
|
will receive two additional arguments. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
The first argument is the parameter passed to the C<< $form->process() >> |
187
|
|
|
|
|
|
|
method. |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
The second is the prefix for the field, if it is part of a group. |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=item * required |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
A boolean indicating whether the field is required. Defaults to false. |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=item * secure |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
A boolean indicating whether the field contains sensitive data. Defaults to |
198
|
|
|
|
|
|
|
false. |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=item * extractor |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
This is an optional method I<on the field's form> that will be used to extract |
203
|
|
|
|
|
|
|
this field's value. |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
The extractor is expected to return a two element list. The first should be |
206
|
|
|
|
|
|
|
the name of the field in the form, the second is the value. |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=item * validator |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
This is an optional method I<on the field's form> that will be used to |
211
|
|
|
|
|
|
|
validate this field's value. |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=back |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=head2 $field->name() |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
The name as passed to the constructor. |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=head2 $field->human_name() |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
A more friendly name, which defaults to the same value as C<< $field->name() |
222
|
|
|
|
|
|
|
>>. |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=head2 $field->type() |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
This returns a L<Moose::Meta::TypeConstraint> object, based on the value |
227
|
|
|
|
|
|
|
passed to the constructor in the C<isa> parameter. |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=head2 $field->default() |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
The default, as passed to the constructor, if any. |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=head2 $field->is_required() |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
Returns a boolean indicating whether the field is required. |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=head2 $field->is_secure() |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
Returns a boolean indicating whether the field contains sensitive data. |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=head2 $field->extractor() |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
Returns the method used to extract the field's data from the user-submitted |
244
|
|
|
|
|
|
|
parameters. This defaults to L<_extract_field_value>, a method provided by |
245
|
|
|
|
|
|
|
L<Chloro::Role::Form>. |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=head2 $field->validator() |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
Returns the method used to extract the field's data from the user-submitted |
250
|
|
|
|
|
|
|
parameters. This defaults to L<_errors_for_field_value>, a method provided by |
251
|
|
|
|
|
|
|
L<Chloro::Role::Form>. |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=head2 $field->generate_default( $params, $prefix ) |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
Given the user-submitted parameters and an optional prefix, this method |
256
|
|
|
|
|
|
|
returns a default value for the field. If the default is a subroutine |
257
|
|
|
|
|
|
|
reference, that reference will be called with the parameters passed to this |
258
|
|
|
|
|
|
|
method. |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
=head2 $field->dump() |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
Returns a data structure representing the field definition. This exists |
263
|
|
|
|
|
|
|
primarily for testing. |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=head1 ROLES |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
This class consumes the L<Chloro::Role::FormComponent> role. |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=head1 AUTHOR |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
Dave Rolsky <autarch@urth.org> |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
This software is Copyright (c) 2011 by Dave Rolsky. |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
This is free software, licensed under: |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
=cut |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
__END__ |
285
|
|
|
|
|
|
|
|