forked from tokuhirom/FormValidator-Lite
/
Lite.pm
368 lines (268 loc) · 8.87 KB
/
Lite.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package FormValidator::Lite;
use strict;
use warnings;
use 5.008_001;
use Carp ();
use UNIVERSAL::require;
use Scalar::Util qw/blessed/;
use FormValidator::Lite::Constraint::Default;
use FormValidator::Lite::Upload;
our $VERSION = '0.17';
our $Rules;
our $FileRules;
sub import {
my ($class, @constraints) = @_;
$class->load_constraints(@constraints);
}
sub new {
my ($class, $q) = @_;
Carp::croak("Usage: ${class}->new(\$q)") unless $q;
bless { _q => $q, _error => {} }, $class;
}
sub check {
my ($self, @rule_ary) = @_;
Carp::croak("this is instance method") unless ref $self;
my $q = $self->{_q};
while (my ($key, $rules) = splice(@rule_ary, 0, 2)) {
local $_;
if (ref $key) {
$key = [%$key];
$_ = [ map { $q->param($_) } @{ $key->[1] } ];
$key = $key->[0];
} else {
$_ = $q->param($key);
}
for my $rule (@$rules) {
my $rule_name = ref($rule) ? $rule->[0] : $rule;
my $args = ref($rule) ? [ @$rule[ 1 .. scalar(@$rule)-1 ] ] : +[];
my $is_ok = do {
if ((not (defined $_ && length $_)) && $rule_name ne 'NOT_NULL') {
1;
} else {
if (my $file_rule = $FileRules->{$rule_name}) {
local $_ = FormValidator::Lite::Upload->new($q, $key);
$file_rule->(@$args) ? 1 : 0;
} else {
my $code = $Rules->{$rule_name} or Carp::croak("unknown rule $rule_name");
$code->(@$args) ? 1 : 0;
}
}
};
if ($is_ok==0) {
$self->set_error($key => $rule_name);
}
}
}
return $self;
}
sub is_error {
my ($self, $key) = @_;
$self->{_error}->{$key} ? 1 : 0;
}
sub is_valid {
my $self = shift;
!$self->has_error ? 1 : 0;
}
sub has_error {
my ($self, ) = @_;
%{ $self->{_error} } ? 1 : 0;
}
sub set_error {
my ($self, $param, $rule_name) = @_;
$self->{_error}->{$param}->{$rule_name}++;
push @{$self->{_error_ary}}, [$param, $rule_name];
}
sub load_constraints {
my $class = shift;
for (@_) {
my $constraint = $_;
$constraint = ($constraint =~ s/^\+//) ? $constraint : "FormValidator::Lite::Constraint::${constraint}";
$constraint->use or die $@;
}
}
sub load_function_message {
my ($self, $lang) = @_;
my $pkg = "FormValidator::Lite::Messages::$lang";
$pkg->require or die $@;
no strict 'refs';
$self->{_msg}->{function} = ${"${pkg}::MESSAGES"};
}
sub set_param_message {
my ($self, %args) = @_;
$self->{_msg}->{param} = \%args;
}
sub set_message_data {
my ($self, $msg) = @_;
for my $key (qw/message param function/) {
Carp::croak("missing key $key") unless $msg->{$key};
}
$self->{_msg} = $msg;
}
sub set_message {
my ($self, @args) = @_;
my %msg = ref $args[0] ? %{$args[0]} : @args;
$self->{_msg}->{message} = +{
%{ $self->{_msg}->{message} || +{} },
%msg
};
}
sub get_error_messages {
my $self = shift;
Carp::croak("message doesn't loaded yet") unless $self->{_msg};
my %dup_check;
my @messages;
for my $err (@{$self->{_error_ary}}) {
my $param = $err->[0];
my $func = $err->[1];
next if exists $dup_check{"$param.$func"};
push @messages, $self->get_error_message( $param, $func );
$dup_check{"$param.$func"}++;
}
return wantarray ? @messages : \@messages;
}
# $validator->get_error_message('email', 'NOT_NULL');
sub get_error_message {
my ($self, $param, $function) = @_;
$function = lc($function);
my $msg = $self->{_msg};
Carp::croak("please load message file first") unless $msg;
my $err_message = $msg->{message}->{"${param}.${function}"};
my $err_param = $msg->{param}->{$param};
my $err_function = $msg->{function}->{$function};
my $gen_msg = sub {
my ($tmpl, @args) = @_;
local $_ = $tmpl;
s!\[_(\d+)\]!$args[$1-1]!ge;
$_;
};
if ($err_message) {
return $gen_msg->($err_message, $err_param);
} elsif ($err_function && $err_param) {
return $gen_msg->($err_function, $err_param);
} else {
Carp::carp "${param}.${function} is not defined in message file.";
if ($msg->{default_tmpl}) {
return $gen_msg->($err_function || $msg->{default_tmpl}, $err_function || $param);
} else {
return '';
}
}
}
sub get_error_messages_from_param {
my ($self, $target_param) = @_;
my %dup_check;
my @messages;
for my $err (@{$self->{_error_ary}}) {
my $param = $err->[0];
my $func = $err->[1];
next if $target_param ne $param;
next if exists $dup_check{"$param.$func"};
push @messages, $self->get_error_message( $param, $func );
$dup_check{"$param.$func"}++;
}
return @messages;
}
1;
__END__
=head1 NAME
FormValidator::Lite - lightweight form validation library
=head1 SYNOPSIS
use FormValidator::Lite;
FormValidator::Lite->load_constraints(qw/Japanese/);
my $q = CGI->new();
my $validator = FormValidator::Lite->new($q);
my $res = $validator->check(
name => [qw/NOT_NULL/],
name_kana => [qw/NOT_NULL KATAKANA/],
{mails => [qw/mail1 mail2/]} => ['DUPLICATION'],
);
if ( ..... return_true_when_if_error() ..... ) {
$validator->set_error('login_id' => 'DUPLICATION');
}
if ($validator->has_error) {
...
}
# in your tmpl
<ul>
? for my $msg ($validator->get_error_messages) {
<li><?= $msg ?></li>
? }
</ul>
=head1 DESCRIPTION
FormValidator::Lite is simple, fast implementation for form validation.
IT'S IN BETA QUALITY. API MAY CHANGE IN FUTURE.
=head1 HOW TO WRITE YOUR OWN CONSTRAINTS
http parameter comes from $_
validator args comes from @_
=head1 METHODS
=over 4
=item my $validator = FormValidator::Lite->new($q);
Create a new instance.
$q is query like object, such as Apache::Request, CGI.pm, Plack::Request.
=item $validator->check(@rule_ary)
This method do validation.
=item $validator->is_error($key)
Return true value if parameter named $key got error.
=item $validator->is_valid()
Return true value if $validator don't detects error.
This is same as !$validator->has_error().
=item $validator->has_error()
Return true value if $validator detects error.
This is same as !$validator->is_valid().
=item $validator->set_error($param, $rule_name)
Set new error to parameter named $param. The rule name is $rule_name.
=item $validator->load_constraints($name)
load constraint components named "FormValidator::Lite::Constraint::${name}".
=item $validator->load_function_message($lang)
$validator->load_function_message('ja');
Load function message file.
Currently, FormValidator::Lite::Messages::ja and FormValidator::Lite::Messages::en are available.
=item $validator->set_param_message($param => $message, ...)
$validator->set_param_message(
name => 'Your Name',
);
Make relational map for the parameter name to human readable name.
=item $validator->set_message_data({ message => $msg, param => $param, function => $function })
$v->set_message_data(YAML::Load(<<'...'));
---
message:
zip.jzip: Please input correct zip number.
param:
name: Your Name
function:
not_null: "[_1] is empty"
hiragana: "[_1] is not Hiragana"
...
Setup error message map.
=item $validator->set_message("$param.$func" => $message)
$v->set_message('zip.jzip' => 'Please input correct zip number.');
Set error message for the $param and $func.
=item my @errors = $validator->get_error_messages()
=item my $errors = $validator->get_error_messages()
Get whole error messages for $q in array/arrayref.
=item my $msg = $validator->get_error_message($param => $func)
Generate error message for parameter $param and function named $func.
=item my @msgs = $validator->get_error_messages_from_param($param)
Get error messages by $q for parameter $param.
=back
=head1 WHY NOT FormValidator::Simple?
Yes, I know. This module is very similar with FV::S.
But, FormValidator::Simple is too heavy for me.
FormValidator::Lite is fast!
Perl: 5.010000
FVS: 0.23
FVL: 0.02
Rate FormValidator::Simple FormValidator::Lite
FormValidator::Simple 353/s -- -75%
FormValidator::Lite 1429/s 304% --
=head1 AUTHOR
Tokuhiro Matsuno E<lt>tokuhirom {at} gmail.comE<gt>
=head1 THANKS TO
craftworks
nekokak
=head1 SEE ALSO
L<FormValidator::Simple>, L<Data::FormValidator>, L<HTML::FormFu>
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut