line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Form::Tiny::FieldDefinition; |
2
|
|
|
|
|
|
|
$Form::Tiny::FieldDefinition::VERSION = '2.20'; |
3
|
51
|
|
|
51
|
|
3168
|
use v5.10; |
|
51
|
|
|
|
|
192
|
|
4
|
51
|
|
|
51
|
|
306
|
use strict; |
|
51
|
|
|
|
|
137
|
|
|
51
|
|
|
|
|
1175
|
|
5
|
51
|
|
|
51
|
|
326
|
use warnings; |
|
51
|
|
|
|
|
109
|
|
|
51
|
|
|
|
|
1437
|
|
6
|
51
|
|
|
51
|
|
287
|
use Moo; |
|
51
|
|
|
|
|
198
|
|
|
51
|
|
|
|
|
361
|
|
7
|
51
|
|
|
51
|
|
17455
|
use Types::Standard qw(Enum Bool HasMethods CodeRef InstanceOf HashRef); |
|
51
|
|
|
|
|
126
|
|
|
51
|
|
|
|
|
1449
|
|
8
|
51
|
|
|
51
|
|
141931
|
use Types::Common::String qw(NonEmptySimpleStr); |
|
51
|
|
|
|
|
2943247
|
|
|
51
|
|
|
|
|
599
|
|
9
|
51
|
|
|
51
|
|
56860
|
use Types::TypeTiny qw(StringLike); |
|
51
|
|
|
|
|
126
|
|
|
51
|
|
|
|
|
334
|
|
10
|
51
|
|
|
51
|
|
28048
|
use Carp qw(croak); |
|
51
|
|
|
|
|
153
|
|
|
51
|
|
|
|
|
3279
|
|
11
|
51
|
|
|
51
|
|
329
|
use Scalar::Util qw(blessed); |
|
51
|
|
|
|
|
132
|
|
|
51
|
|
|
|
|
2510
|
|
12
|
|
|
|
|
|
|
|
13
|
51
|
|
|
51
|
|
333
|
use Form::Tiny::Utils qw(try has_form_meta); |
|
51
|
|
|
|
|
125
|
|
|
51
|
|
|
|
|
2409
|
|
14
|
51
|
|
|
51
|
|
734
|
use Form::Tiny::Error; |
|
51
|
|
|
|
|
134
|
|
|
51
|
|
|
|
|
1117
|
|
15
|
51
|
|
|
51
|
|
22779
|
use Form::Tiny::Path; |
|
51
|
|
|
|
|
171
|
|
|
51
|
|
|
|
|
57014
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has 'name' => ( |
18
|
|
|
|
|
|
|
is => 'ro', |
19
|
|
|
|
|
|
|
isa => NonEmptySimpleStr, |
20
|
|
|
|
|
|
|
required => 1, |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
has 'name_path' => ( |
24
|
|
|
|
|
|
|
is => 'ro', |
25
|
|
|
|
|
|
|
isa => InstanceOf ['Form::Tiny::Path'], |
26
|
|
|
|
|
|
|
reader => 'get_name_path', |
27
|
|
|
|
|
|
|
init_arg => undef, |
28
|
|
|
|
|
|
|
lazy => 1, |
29
|
|
|
|
|
|
|
default => sub { Form::Tiny::Path->from_name(shift->name) }, |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
has 'required' => ( |
33
|
|
|
|
|
|
|
is => 'ro', |
34
|
|
|
|
|
|
|
isa => Enum [0, 1, 'soft', 'hard'], |
35
|
|
|
|
|
|
|
writer => 'set_required', |
36
|
|
|
|
|
|
|
default => sub { 0 }, |
37
|
|
|
|
|
|
|
); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
has 'type' => ( |
40
|
|
|
|
|
|
|
is => 'ro', |
41
|
|
|
|
|
|
|
isa => HasMethods ['validate', 'check'], |
42
|
|
|
|
|
|
|
writer => 'set_type', |
43
|
|
|
|
|
|
|
predicate => 'has_type', |
44
|
|
|
|
|
|
|
); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
has 'addons' => ( |
47
|
|
|
|
|
|
|
is => 'ro', |
48
|
|
|
|
|
|
|
writer => 'set_addons', |
49
|
|
|
|
|
|
|
isa => HashRef, |
50
|
|
|
|
|
|
|
default => sub { {} }, |
51
|
|
|
|
|
|
|
init_arg => undef, |
52
|
|
|
|
|
|
|
); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
has 'coerce' => ( |
55
|
|
|
|
|
|
|
is => 'ro', |
56
|
|
|
|
|
|
|
isa => Bool | CodeRef, |
57
|
|
|
|
|
|
|
writer => 'set_coercion', |
58
|
|
|
|
|
|
|
default => sub { 0 }, |
59
|
|
|
|
|
|
|
); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
has 'adjust' => ( |
62
|
|
|
|
|
|
|
is => 'ro', |
63
|
|
|
|
|
|
|
isa => CodeRef, |
64
|
|
|
|
|
|
|
predicate => 'is_adjusted', |
65
|
|
|
|
|
|
|
writer => 'set_adjustment', |
66
|
|
|
|
|
|
|
); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
has 'default' => ( |
69
|
|
|
|
|
|
|
is => 'ro', |
70
|
|
|
|
|
|
|
isa => CodeRef, |
71
|
|
|
|
|
|
|
writer => 'set_default', |
72
|
|
|
|
|
|
|
predicate => 'has_default', |
73
|
|
|
|
|
|
|
); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
has 'message' => ( |
76
|
|
|
|
|
|
|
is => 'ro', |
77
|
|
|
|
|
|
|
isa => StringLike, |
78
|
|
|
|
|
|
|
writer => 'set_message', |
79
|
|
|
|
|
|
|
predicate => 'has_message', |
80
|
|
|
|
|
|
|
); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
has 'data' => ( |
83
|
|
|
|
|
|
|
is => 'ro', |
84
|
|
|
|
|
|
|
writer => 'set_data', |
85
|
|
|
|
|
|
|
predicate => 'has_data', |
86
|
|
|
|
|
|
|
); |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
has '_subform' => ( |
89
|
|
|
|
|
|
|
is => 'ro', |
90
|
|
|
|
|
|
|
isa => Bool, |
91
|
|
|
|
|
|
|
reader => 'is_subform', |
92
|
|
|
|
|
|
|
lazy => 1, |
93
|
|
|
|
|
|
|
default => sub { $_[0]->has_type && has_form_meta($_[0]->type) }, |
94
|
|
|
|
|
|
|
init_arg => undef, |
95
|
|
|
|
|
|
|
); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub BUILD |
98
|
|
|
|
|
|
|
{ |
99
|
293
|
|
|
293
|
0
|
15023
|
my ($self, $args) = @_; |
100
|
|
|
|
|
|
|
|
101
|
293
|
100
|
100
|
|
|
1392
|
if ($self->coerce && ref $self->coerce ne 'CODE') { |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# checks for coercion == 1 |
104
|
16
|
|
|
|
|
110
|
my $t = $self->type; |
105
|
16
|
100
|
33
|
|
|
163
|
croak 'type doesn\'t provide coercion' |
|
|
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
106
|
|
|
|
|
|
|
if !$self->has_type |
107
|
|
|
|
|
|
|
|| !($t->can('coerce') && $t->can('has_coercion') && $t->has_coercion); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
291
|
100
|
|
|
|
2336
|
if ($self->has_default) { |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
croak 'default value for an array field is unsupported' |
113
|
12
|
100
|
|
|
|
21
|
if scalar grep { $_ eq 'ARRAY' } @{$self->get_name_path->meta}; |
|
19
|
|
|
|
|
459
|
|
|
12
|
|
|
|
|
220
|
|
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub hard_required |
118
|
|
|
|
|
|
|
{ |
119
|
31
|
|
|
31
|
1
|
91
|
my ($self) = @_; |
120
|
|
|
|
|
|
|
|
121
|
31
|
|
100
|
|
|
242
|
return $self->required eq '1' || $self->required eq 'hard'; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub get_coerced |
125
|
|
|
|
|
|
|
{ |
126
|
344
|
|
|
344
|
0
|
790
|
my ($self, $form, $value) = @_; |
127
|
344
|
|
|
|
|
783
|
my $coerce = $self->coerce; |
128
|
|
|
|
|
|
|
|
129
|
344
|
100
|
|
|
|
1163
|
if ($coerce) { |
130
|
|
|
|
|
|
|
my $error = try sub { |
131
|
62
|
100
|
|
62
|
|
148
|
if (ref $coerce eq 'CODE') { |
132
|
29
|
|
|
|
|
66
|
$value = $coerce->($form, $value); |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
else { |
135
|
33
|
|
|
|
|
115
|
$value = $self->type->coerce($value); |
136
|
|
|
|
|
|
|
} |
137
|
62
|
|
|
|
|
356
|
}; |
138
|
|
|
|
|
|
|
|
139
|
62
|
100
|
|
|
|
290
|
if ($error) { |
140
|
3
|
50
|
|
|
|
84
|
$form->add_error( |
141
|
|
|
|
|
|
|
Form::Tiny::Error::DoesNotValidate->new( |
142
|
|
|
|
|
|
|
{ |
143
|
|
|
|
|
|
|
field => $self->name, |
144
|
|
|
|
|
|
|
error => $self->has_message ? $self->message : $error, |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
) |
147
|
|
|
|
|
|
|
); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
344
|
|
|
|
|
906
|
return $value; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub get_adjusted |
155
|
|
|
|
|
|
|
{ |
156
|
275
|
|
|
275
|
0
|
541
|
my $self = shift; |
157
|
275
|
|
|
|
|
472
|
my $value = pop; |
158
|
|
|
|
|
|
|
|
159
|
275
|
100
|
|
|
|
5486
|
if ($self->is_subform) { |
160
|
6
|
|
|
|
|
86
|
$value = $self->type->fields; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
275
|
100
|
|
|
|
4708
|
return $value unless $self->is_adjusted; |
164
|
24
|
|
|
|
|
80
|
return $self->adjust->(@_, $value); |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub get_default |
168
|
|
|
|
|
|
|
{ |
169
|
20
|
|
|
20
|
0
|
976
|
my ($self, $form) = @_; |
170
|
|
|
|
|
|
|
|
171
|
20
|
50
|
|
|
|
57
|
croak 'no default value set but was requested' |
172
|
|
|
|
|
|
|
unless $self->has_default; |
173
|
|
|
|
|
|
|
|
174
|
20
|
|
|
|
|
65
|
my $default = $self->default->($form); |
175
|
20
|
100
|
|
|
|
386
|
if ($self->is_subform) { |
176
|
1
|
|
|
|
|
33
|
my $subform = $self->type; |
177
|
1
|
50
|
|
|
|
4
|
croak 'subform default input is not valid' |
178
|
|
|
|
|
|
|
unless $subform->check($default); |
179
|
|
|
|
|
|
|
|
180
|
1
|
|
|
|
|
4
|
$default = $subform->fields; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
20
|
|
|
|
|
421
|
return $default; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
sub validate |
187
|
|
|
|
|
|
|
{ |
188
|
344
|
|
|
344
|
1
|
688
|
my ($self, $form, $value) = @_; |
189
|
|
|
|
|
|
|
|
190
|
344
|
|
|
|
|
517
|
my @errors; |
191
|
|
|
|
|
|
|
|
192
|
344
|
100
|
|
|
|
955
|
if ($self->has_type) { |
193
|
199
|
100
|
|
|
|
520
|
if ($self->has_message) { |
194
|
8
|
100
|
|
|
|
49
|
push @errors, $self->message |
195
|
|
|
|
|
|
|
if !$self->type->check($value); |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
else { |
198
|
191
|
|
|
|
|
668
|
my $error = $self->type->validate($value); |
199
|
191
|
100
|
|
|
|
8713
|
push @errors, $error |
200
|
|
|
|
|
|
|
if defined $error; |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
344
|
100
|
100
|
|
|
1767
|
if (@errors == 0 && (my $validators = $self->addons->{validators})) { |
205
|
5
|
|
|
|
|
9
|
for my $validator (@{$validators}) { |
|
5
|
|
|
|
|
13
|
|
206
|
9
|
|
|
|
|
23
|
my ($message, $code) = @{$validator}; |
|
9
|
|
|
|
|
19
|
|
207
|
|
|
|
|
|
|
|
208
|
9
|
100
|
|
|
|
24
|
if (!$code->($form, $value)) { |
209
|
4
|
|
|
|
|
24
|
push @errors, $message; |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
|
214
|
344
|
|
|
|
|
2971
|
for my $error (@errors) { |
215
|
70
|
100
|
66
|
|
|
406
|
if (ref $error eq 'ARRAY' && $self->is_subform) { |
216
|
6
|
|
|
|
|
69
|
foreach my $exception (@$error) { |
217
|
6
|
50
|
33
|
|
|
50
|
if (defined blessed $exception && $exception->isa('Form::Tiny::Error')) { |
218
|
6
|
|
|
|
|
103
|
my $path = $self->get_name_path; |
219
|
6
|
100
|
|
|
|
95
|
$path = $path->clone->append(HASH => $exception->field) |
220
|
|
|
|
|
|
|
if defined $exception->field; |
221
|
|
|
|
|
|
|
|
222
|
6
|
|
|
|
|
38
|
$exception->set_field($path->join); |
223
|
6
|
|
|
|
|
317
|
$exception = Form::Tiny::Error::NestedFormError->new( |
224
|
|
|
|
|
|
|
field => $self->name, |
225
|
|
|
|
|
|
|
error => $exception, |
226
|
|
|
|
|
|
|
); |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
else { |
229
|
0
|
|
|
|
|
0
|
$exception = Form::Tiny::Error::DoesNotValidate->new( |
230
|
|
|
|
|
|
|
field => $self->name, |
231
|
|
|
|
|
|
|
error => $exception, |
232
|
|
|
|
|
|
|
); |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
|
235
|
6
|
|
|
|
|
509
|
$form->add_error($exception); |
236
|
|
|
|
|
|
|
} |
237
|
|
|
|
|
|
|
} |
238
|
|
|
|
|
|
|
else { |
239
|
64
|
|
|
|
|
1687
|
$form->add_error( |
240
|
|
|
|
|
|
|
Form::Tiny::Error::DoesNotValidate->new( |
241
|
|
|
|
|
|
|
{ |
242
|
|
|
|
|
|
|
field => $self->name, |
243
|
|
|
|
|
|
|
error => $error, |
244
|
|
|
|
|
|
|
} |
245
|
|
|
|
|
|
|
) |
246
|
|
|
|
|
|
|
); |
247
|
|
|
|
|
|
|
} |
248
|
|
|
|
|
|
|
} |
249
|
|
|
|
|
|
|
|
250
|
344
|
|
|
|
|
1212
|
return @errors == 0; |
251
|
|
|
|
|
|
|
} |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
1; |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
__END__ |