line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Form::DemonCore::Field; |
2
|
|
|
|
|
|
|
BEGIN { |
3
|
1
|
|
|
1
|
|
5516
|
$Form::DemonCore::Field::AUTHORITY = 'cpan:GETTY'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
BEGIN { |
6
|
1
|
|
|
1
|
|
20
|
$Form::DemonCore::Field::VERSION = '0.101'; |
7
|
|
|
|
|
|
|
} |
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
448
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has name => ( |
12
|
|
|
|
|
|
|
isa => 'Str', |
13
|
|
|
|
|
|
|
is => 'ro', |
14
|
|
|
|
|
|
|
required => 1, |
15
|
|
|
|
|
|
|
); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has form => ( |
18
|
|
|
|
|
|
|
isa => 'Form::DemonCore', |
19
|
|
|
|
|
|
|
is => 'ro', |
20
|
|
|
|
|
|
|
required => 1, |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
has required => ( |
24
|
|
|
|
|
|
|
isa => 'Bool', |
25
|
|
|
|
|
|
|
is => 'ro', |
26
|
|
|
|
|
|
|
default => sub { 0 }, |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
has notempty => ( |
30
|
|
|
|
|
|
|
isa => 'Bool', |
31
|
|
|
|
|
|
|
is => 'ro', |
32
|
|
|
|
|
|
|
default => sub { 0 }, |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
has param_name => ( |
36
|
|
|
|
|
|
|
is => 'ro', |
37
|
|
|
|
|
|
|
isa => 'Str', |
38
|
|
|
|
|
|
|
lazy_build => 1, |
39
|
|
|
|
|
|
|
); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub _build_param_name { |
42
|
|
|
|
|
|
|
my ( $self ) = @_; |
43
|
|
|
|
|
|
|
return $self->form->name.$self->form->param_split_char.$self->name; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
has default_value => ( |
47
|
|
|
|
|
|
|
is => 'rw', |
48
|
|
|
|
|
|
|
trigger => sub { shift->reset }, |
49
|
|
|
|
|
|
|
predicate => 'has_default_value', |
50
|
|
|
|
|
|
|
); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
has input_value => ( |
53
|
|
|
|
|
|
|
is => 'rw', |
54
|
|
|
|
|
|
|
trigger => sub { shift->reset }, |
55
|
|
|
|
|
|
|
predicate => 'has_input_value', |
56
|
|
|
|
|
|
|
clearer => 'clear_input_value', |
57
|
|
|
|
|
|
|
); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
has output_value => ( |
60
|
|
|
|
|
|
|
is => 'rw', |
61
|
|
|
|
|
|
|
predicate => 'has_output_value', |
62
|
|
|
|
|
|
|
clearer => 'clear_output_value', |
63
|
|
|
|
|
|
|
); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub session_value { |
66
|
|
|
|
|
|
|
my ( $self ) = @_; |
67
|
|
|
|
|
|
|
return $self->session->{value} if ($self->has_session_value); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub has_session_value { |
71
|
|
|
|
|
|
|
my ( $self ) = @_; |
72
|
|
|
|
|
|
|
return 0 unless $self->form->has_session; |
73
|
|
|
|
|
|
|
return defined $self->session->{value} ? 1 : 0; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub session { |
77
|
|
|
|
|
|
|
my $self = shift; |
78
|
|
|
|
|
|
|
if ($self->form->has_session) { |
79
|
|
|
|
|
|
|
$self->form->session->{demoncore} = {} if !defined $self->form->session->{demoncore}; |
80
|
|
|
|
|
|
|
$self->form->session->{demoncore}->{$self->param_name} = {} if !defined $self->form->session->{demoncore}->{$self->param_name}; |
81
|
|
|
|
|
|
|
return $self->form->session->{demoncore}->{$self->param_name}; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
die __PACKAGE__." can't use session without session on the form"; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
has is_valid => ( |
87
|
|
|
|
|
|
|
is => 'ro', |
88
|
|
|
|
|
|
|
isa => 'Bool', |
89
|
|
|
|
|
|
|
lazy_build => 1, |
90
|
|
|
|
|
|
|
predicate => 'is_validated', |
91
|
|
|
|
|
|
|
clearer => 'reset_validation', |
92
|
|
|
|
|
|
|
); |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub _build_is_valid { |
95
|
|
|
|
|
|
|
my $self = shift; |
96
|
|
|
|
|
|
|
$self->populate; |
97
|
|
|
|
|
|
|
if ($self->has_value) { |
98
|
|
|
|
|
|
|
my $empty = $self->empty_check->($self->value); |
99
|
|
|
|
|
|
|
if ($empty && $self->notempty) { |
100
|
|
|
|
|
|
|
$self->add_error($self->notempty_error); |
101
|
|
|
|
|
|
|
} elsif (!$empty) { |
102
|
|
|
|
|
|
|
for ($self->all_validators) { |
103
|
|
|
|
|
|
|
$self->add_error($_) for ($_->($self,$self->value)); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
} elsif ($self->required) { |
107
|
|
|
|
|
|
|
$self->add_error($self->required_error); |
108
|
|
|
|
|
|
|
} elsif ($self->notempty) { |
109
|
|
|
|
|
|
|
$self->add_error($self->notempty_error); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
return $self->has_errors ? 0 : 1; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
has value => ( |
115
|
|
|
|
|
|
|
is => 'rw', |
116
|
|
|
|
|
|
|
predicate => 'has_value', |
117
|
|
|
|
|
|
|
clearer => 'reset_value', |
118
|
|
|
|
|
|
|
trigger => sub { shift->value_changed }, |
119
|
|
|
|
|
|
|
); |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub BUILD { |
122
|
|
|
|
|
|
|
my ( $self ) = @_; |
123
|
|
|
|
|
|
|
$self->populate; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub clear_session { |
127
|
|
|
|
|
|
|
my ( $self ) = @_; |
128
|
|
|
|
|
|
|
delete $self->form->session->{demoncore}->{$self->param_name} if $self->form->has_session; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub reset { |
132
|
|
|
|
|
|
|
my ( $self ) = @_; |
133
|
|
|
|
|
|
|
$self->populate; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub value_changed { |
137
|
|
|
|
|
|
|
my ( $self ) = @_; |
138
|
|
|
|
|
|
|
$self->value_to_session; |
139
|
|
|
|
|
|
|
$self->value_to_output; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub value_to_session { |
143
|
|
|
|
|
|
|
my ( $self ) = @_; |
144
|
|
|
|
|
|
|
if ( $self->form->has_session ) { |
145
|
|
|
|
|
|
|
$self->session->{value} = $self->value; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub populate { |
150
|
|
|
|
|
|
|
my ( $self ) = @_; |
151
|
|
|
|
|
|
|
$self->reset_validation; |
152
|
|
|
|
|
|
|
if ($self->form->has_session) { |
153
|
|
|
|
|
|
|
if (defined $self->session->{value}) { |
154
|
|
|
|
|
|
|
$self->session_value($self->session->{value}); |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
$self->reset_value; |
158
|
|
|
|
|
|
|
$self->clear_output_value; |
159
|
|
|
|
|
|
|
if ($self->has_input_value) { |
160
|
|
|
|
|
|
|
$self->input_to_value if ( $self->has_input_value ); |
161
|
|
|
|
|
|
|
} elsif ($self->form->has_session && $self->has_session_value) { |
162
|
|
|
|
|
|
|
$self->value($self->session_value); |
163
|
|
|
|
|
|
|
} elsif ($self->has_default_value) { |
164
|
|
|
|
|
|
|
$self->value($self->default_value); |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
sub input_to_value { |
169
|
|
|
|
|
|
|
my ( $self ) = @_; |
170
|
|
|
|
|
|
|
$self->value($self->input_value); |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
sub value_to_output { |
174
|
|
|
|
|
|
|
my ( $self ) = @_; |
175
|
|
|
|
|
|
|
$self->output_value($self->value); |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
has empty_check => ( |
179
|
|
|
|
|
|
|
is => 'ro', |
180
|
|
|
|
|
|
|
isa => 'CodeRef', |
181
|
|
|
|
|
|
|
lazy_build => 1, |
182
|
|
|
|
|
|
|
); |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub _build_empty_check { |
185
|
|
|
|
|
|
|
return sub { shift =~ m/^$/ } |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
has required_error => ( |
189
|
|
|
|
|
|
|
is => 'rw', |
190
|
|
|
|
|
|
|
default => sub { 'This field is required' }, |
191
|
|
|
|
|
|
|
); |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
has notempty_error => ( |
194
|
|
|
|
|
|
|
is => 'rw', |
195
|
|
|
|
|
|
|
default => sub { 'This field may not be empty' }, |
196
|
|
|
|
|
|
|
); |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
sub submit_errors { |
199
|
|
|
|
|
|
|
my ( $self ) = @_; |
200
|
|
|
|
|
|
|
return unless $self->form->is_submitted; |
201
|
|
|
|
|
|
|
return $self->errors; |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
has errors => ( |
205
|
|
|
|
|
|
|
traits => ['Array'], |
206
|
|
|
|
|
|
|
is => 'ro', |
207
|
|
|
|
|
|
|
isa => 'ArrayRef', |
208
|
|
|
|
|
|
|
default => sub {[]}, |
209
|
|
|
|
|
|
|
handles => { |
210
|
|
|
|
|
|
|
all_errors => 'elements', |
211
|
|
|
|
|
|
|
add_error => 'push', |
212
|
|
|
|
|
|
|
count_errors => 'count', |
213
|
|
|
|
|
|
|
has_errors => 'count', |
214
|
|
|
|
|
|
|
has_no_errors => 'is_empty', |
215
|
|
|
|
|
|
|
}, |
216
|
|
|
|
|
|
|
); |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
has x => ( |
219
|
|
|
|
|
|
|
traits => ['Hash'], |
220
|
|
|
|
|
|
|
is => 'ro', |
221
|
|
|
|
|
|
|
isa => 'HashRef', |
222
|
|
|
|
|
|
|
default => sub {{}}, |
223
|
|
|
|
|
|
|
); |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
has validators => ( |
227
|
|
|
|
|
|
|
traits => ['Array'], |
228
|
|
|
|
|
|
|
is => 'ro', |
229
|
|
|
|
|
|
|
isa => 'ArrayRef[CodeRef]', |
230
|
|
|
|
|
|
|
default => sub {[]}, |
231
|
|
|
|
|
|
|
handles => { |
232
|
|
|
|
|
|
|
all_validators => 'elements', |
233
|
|
|
|
|
|
|
add_validator => 'push', |
234
|
|
|
|
|
|
|
count_validators => 'count', |
235
|
|
|
|
|
|
|
has_validators => 'count', |
236
|
|
|
|
|
|
|
has_no_validators => 'is_empty', |
237
|
|
|
|
|
|
|
}, |
238
|
|
|
|
|
|
|
); |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
1; |
241
|
|
|
|
|
|
|
__END__ |
242
|
|
|
|
|
|
|
=pod |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=head1 NAME |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
Form::DemonCore::Field |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=head1 VERSION |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
version 0.101 |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=head1 AUTHOR |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
Torsten Raudssus <torsten@raudss.us> L<http://raudss.us/> |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
This software is copyright (c) 2011 by Raudssus Social Software. |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
261
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=cut |
264
|
|
|
|
|
|
|
|