File: Simple.pm

package info (click to toggle)
libformvalidator-simple-perl 0.29-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 412 kB
  • sloc: perl: 3,043; makefile: 4
file content (699 lines) | stat: -rw-r--r-- 17,734 bytes parent folder | download | duplicates (4)
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
package FormValidator::Simple;
use strict;
use base qw/Class::Accessor::Fast Class::Data::Inheritable Class::Data::Accessor/;
use Class::Inspector;
use UNIVERSAL::require;
use Scalar::Util qw/blessed/;
use FormValidator::Simple::Results;
use FormValidator::Simple::Exception;
use FormValidator::Simple::Data;
use FormValidator::Simple::Profile;
use FormValidator::Simple::Validator;
use FormValidator::Simple::Constants;
use FormValidator::Simple::Messages;

our $VERSION = '0.29';

__PACKAGE__->mk_classaccessors(qw/data prof results/);
__PACKAGE__->mk_classaccessor( messages => FormValidator::Simple::Messages->new );

sub import {
    my $class = shift;
    foreach my $plugin (@_) {
        my $plugin_class;
        if ($plugin =~ /^\+(.*)/) {
            $plugin_class = $1;
        } else {
            $plugin_class = "FormValidator::Simple::Plugin::$plugin";
        }
        $class->load_plugin($plugin_class);
    }
}

sub load_plugin {
    my ($proto, $plugin) = @_;
    my $class  = ref $proto || $proto;
    unless (Class::Inspector->installed($plugin)) {
        FormValidator::Simple::Exception->throw(
            qq/$plugin isn't installed./
        );
    }
    $plugin->require;
    if ($@) {
        FormValidator::Simple::Exception->throw(
            qq/Couldn't require "$plugin", "$@"./
        );
    }
    {
        no strict 'refs';
        push @FormValidator::Simple::Validator::ISA, $plugin;
    }
}

sub set_option {
    my $class = shift;
    while ( my ($key, $val) = splice @_, 0, 2 ) {
        FormValidator::Simple::Validator->options->{$key} = $val;
    }
}

sub set_messages {
    my ($proto, $file) = @_;
    my $class = ref $proto || $proto;
    if (blessed $proto) {
        $proto->messages(FormValidator::Simple::Messages->new)->load($file);
        if ($proto->results) {
            $proto->results->message($proto->messages);
        } else {
            $proto->results( FormValidator::Simple::Results->new(
                messages => $proto->messages,
            ) );
        }
    } else {
        $class->messages->load($file);
    }
}

sub set_message_decode_from {
    my ($self, $decode_from) = @_;
    $self->messages->decode_from($decode_from);
}

sub set_message_format {
    my ($proto, $format) = @_;
    $format ||= '';
    $proto->messages->format($format);
}

sub new {
    my $proto = shift;
    my $class = ref $proto || $proto;
    my $self  = bless { }, $class;
    $self->_init(@_);
    return $self;
}

sub _init {
    my ($self, @args) = @_;
    my $class = ref $self;
    $class->set_option(@args);
    $self->results( FormValidator::Simple::Results->new(
        messages => $self->messages,
    ) );
}

sub set_invalid {
    my ($self, $name, $type) = @_;
    unless (ref $self) {
        FormValidator::Simple::Exception->throw(
            qq/set_invalid is instance method./
        );
    }
    unless ($name && $type) {
        FormValidator::Simple::Exception->throw(
            qq/set_invalid needs two arguments./
        );
    }
    $self->results->set_result($name, $type, FALSE);
}

sub check {
    my ($proto, $input, $prof, $options) = @_;
    $options ||= {};
    my $self = blessed $proto ? $proto : $proto->new(%$options);

    my $data = FormValidator::Simple::Data->new($input);
    my $prof_setting = FormValidator::Simple::Profile->new($prof);

    my $profile_iterator = $prof_setting->iterator;

    PROFILE:
    while ( my $profile = $profile_iterator->next ) {

        my $name        = $profile->name;
        my $keys        = $profile->keys;
        my $constraints = $profile->constraints;

        my $params = $data->param($keys);

        $self->results->register($name);

        $self->results->record($name)->data( @$params == 1 ? $params->[0] : '');

        my $constraint_iterator = $constraints->iterator;
        if ( scalar @$params == 1 ) {
            unless ( defined $params->[0] && $params->[0] ne '' ) {
                if ( $constraints->needs_blank_check ) {
                    $self->results->record($name)->is_blank( TRUE );
                }
                next PROFILE;
            }
        }

        CONSTRAINT:
        while ( my $constraint = $constraint_iterator->next ) {

            my ($result, $data) = $constraint->check($params);

            $self->results->set_result($name, $constraint->name, $result);

            $self->results->record($name)->data($data) if $data;
        }

    }
    return $self->results;
}

1;

=head1 NAME

FormValidator::Simple - validation with simple chains of constraints 

=head1 SYNOPSIS

    my $query = CGI->new;
    $query->param( param1 => 'ABCD' );
    $query->param( param2 =>  12345 );
    $query->param( mail1  => 'lyo.kato@gmail.com' );
    $query->param( mail2  => 'lyo.kato@gmail.com' );
    $query->param( year   => 2005 );
    $query->param( month  =>   11 );
    $query->param( day    =>   27 );

    my $result = FormValidator::Simple->check( $query => [
        param1 => ['NOT_BLANK', 'ASCII', ['LENGTH', 2, 5]],
        param2 => ['NOT_BLANK', 'INT'  ],
        mail1  => ['NOT_BLANK', 'EMAIL_LOOSE'],
        mail2  => ['NOT_BLANK', 'EMAIL_LOOSE'],
        { mails => ['mail1', 'mail2'       ] } => ['DUPLICATION'],
        { date  => ['year',  'month', 'day'] } => ['DATE'],
    ] );

    if ( $result->has_error ) {
        my $tt = Template->new({ INCLUDE_PATH => './tmpl' });
        $tt->process('template.html', { result => $result });
    }

template example

    [% IF result.has_error %]
    <p>Found Input Error</p>
    <ul>

        [% IF result.missing('param1') %]
        <li>param1 is blank.</li>
        [% END %]

        [% IF result.invalid('param1') %]
        <li>param1 is invalid.</li>
        [% END %]

        [% IF result.invalid('param1', 'ASCII') %]
        <li>param1 needs ascii code.</li>
        [% END %]

        [% IF result.invalid('param1', 'LENGTH') %]
        <li>input into param1 with characters that's length should be between two and five. </li>
        [% END %]

    </ul>
    [% END %]

example2

    [% IF result.has_error %]
    <ul>
        [% FOREACH key IN result.error %]
            [% FOREACH type IN result.error(key) %]
            <li>invalid: [% key %] - [% type %]</li>
            [% END %]
        [% END %]
    </ul>
    [% END %]

=head1 DESCRIPTION

This module provides you a sweet way of form data validation with simple constraints chains.
You can write constraints on single line for each input data.

This idea is based on Sledge::Plugin::Validator, and most of validation code is borrowed from this plugin.

(Sledge is a MVC web application framework: http://sl.edge.jp [Japanese] )

The result object this module returns behaves like L<Data::FormValidator::Results>.

=head1 HOW TO SET PROFILE

    FormValidator::Simple->check( $q => [
        #profile
    ] );

Use 'check' method. 

A hash reference includes input data, or an object of some class that has a method named 'param', for example L<CGI>, is needed as first argument.

And set profile as array reference into second argument. Profile consists of some pairs of input data and constraints.

    my $q = CGI->new;
    $q->param( param1 => 'hoge' );

    FormValidator::Simple->check( $q => [
        param1 => [ ['NOT_BLANK'], ['LENGTH', 4, 10] ],
    ] );

In this case, param1 is the name of a form element. and the array ref "[ ['NOT_BLANK']... ]" is a constraints chain.

Write constraints chain as arrayref, and you can set some constraints into it. In the last example, two constraints
'NOT_BLANK', and 'LENGTH' are set. Each constraints is should be set as arrayref, but in case the constraint has no
argument, it can be written as scalar text.

    FormValidator::Simple->check( $q => [
        param1 => [ 'NOT_BLANK', ['LENGTH', 4, 10] ],
    ] );

Now, in this sample 'NOT_BLANK' constraint is not an arrayref, but 'LENGTH' isn't. Because 'LENGTH' has two arguments, 4 and 10.

=head2 MULTIPLE DATA VALIDATION

When you want to check about multiple input data, do like this.

    my $q = CGI->new;
    $q->param( mail1 => 'lyo.kato@gmail.com' );
    $q->param( mail2 => 'lyo.kato@gmail.com' );

    my $result = FormValidator::Simple->check( $q => [
        { mails => ['mail1', 'mail2'] } => [ 'DUPLICATION' ],
    ] )

    [% IF result.invalid('mails') %]
    <p>mail1 and mail2 aren't same.</p>
    [% END %]

and here's an another example.

    my $q = CGI->new;
    $q->param( year  => 2005 );
    $q->param( month =>   12 );
    $q->param(   day =>   27 );

    my $result = FormValidator::Simple->check( $q => [ 
        { date => ['year', 'month', 'day'] } => [ 'DATE' ],
    ] );

    [% IF result.invalid('date') %]
    <p>Set correct date.</p>
    [% END %]

=head2 FLEXIBLE VALIDATION

    my $valid = FormValidator::Simple->new();

    $valid->check( $q => [ 
        param1 => [qw/NOT_BLANK ASCII/, [qw/LENGTH 4 10/] ],
    ] );

    $valid->check( $q => [
        param2 => [qw/NOT_BLANK/],
    ] );

    my $results = $valid->results;

    if ( found some error... ) {
        $results->set_invalid('param3' => 'MY_ERROR');
    }

template example

    [% IF results.invalid('param1') %]
    ...
    [% END %]
    [% IF results.invalid('param2') %]
    ...
    [% END %]
    [% IF results.invalid('param3', 'MY_ERROR') %]
    ...
    [% END %]

=head1 HOW TO SET OPTIONS

Option setting is needed by some validation, especially in plugins.

You can set them in two ways.

    FormValidator::Simple->set_option(
        dbic_base_class => 'MyProj::Model::DBIC',
        charset         => 'euc',
    );

or

    $valid = FormValidator::Simple->new(
        dbic_base_class => 'MyProj::Model::DBIC',
        charset         => 'euc',
    );

    $valid->check(...)

=head1 VALIDATION COMMANDS

You can use follow variety validations.
and each validations can be used as negative validation with 'NOT_' prefix.

    FormValidator::Simple->check( $q => [ 
        param1 => [ 'INT', ['LENGTH', 4, 10] ],
        param2 => [ 'NOT_INT', ['NOT_LENGTH', 4, 10] ],
    ] );

=over 4

=item SP

check if the data has space or not.

=item INT

check if the data is integer or not.

=item UINT

unsigined integer check.
for example, if -1234 is input, the validation judges it invalid.

=item DECIMAL

    $q->param( 'num1' => '123.45678' );

    my $result = FormValidator::Simple->check( $q => [ 
        num1 => [ ['DECIMAL', 3, 5] ],
    ] );

each numbers (3,5) mean maximum digits before/after '.'

=item ASCII

check is the data consists of only ascii code.

=item LENGTH

check the length of the data.

    my $result = FormValidator::Simple->check( $q => [
        param1 => [ ['LENGTH', 4] ],
    ] );

check if the length of the data is 4 or not.

    my $result = FormValidator::Simple->check( $q => [
        param1 => [ ['LENGTH', 4, 10] ],
    ] );

when you set two arguments, it checks if the length of data is in
the range between 4 and 10.

=item HTTP_URL

verify it is a http(s)-url

    my $result = FormValidator::Simple->check( $q => [
        param1 => [ 'HTTP_URL' ],
    ] );

=item SELECTED_AT_LEAST

verify the quantity of selected parameters is counted over allowed minimum.

    <input type="checkbox" name="hobby" value="music" /> Music
    <input type="checkbox" name="hobby" value="movie" /> Movie
    <input type="checkbox" name="hobby" value="game"  /> Game

    my $result = FormValidator::Simple->check( $q => [ 
        hobby => ['NOT_BLANK', ['SELECTED_AT_LEAST', 2] ],
    ] );

=item REGEX

check with regular expression.

    my $result = FormValidator::Simple->check( $q => [ 
        param1 => [ ['REGEX', qr/^hoge$/ ] ],
    ] );

=item DUPLICATION

check if the two data are same or not.

    my $result = FormValidator::Simple->check( $q => [ 
        { duplication_check => ['param1', 'param2'] } => [ 'DUPLICATION' ],
    ] );

=item EMAIL

check with L<Email::Valid>.

=item EMAIL_MX

check with L<Email::Valid>, including  mx check.

=item EMAIL_LOOSE

check with L<Email::Valid::Loose>.

=item EMAIL_LOOSE_MX

check with L<Email::Valid::Loose>, including mx check.

=item DATE

check with L<Date::Calc>

    my $result = FormValidator::Simple->check( $q => [ 
        { date => [qw/year month day/] } => [ 'DATE' ]
    ] );

=item TIME

check with L<Date::Calc>

    my $result = FormValidator::Simple->check( $q => [
        { time => [qw/hour min sec/] } => ['TIME'],
    ] );

=item DATETIME

check with L<Date::Calc>

    my $result = FormValidator::Simple->check( $q => [ 
        { datetime => [qw/year month day hour min sec/] } => ['DATETIME']
    ] );

=item DATETIME_STRPTIME

check with L<DateTime::Format::Strptime>.

    my $q = CGI->new;
    $q->param( datetime => '2006-04-26T19:09:21+0900' );

    my $result = FormValidator::Simple->check( $q => [
      datetime => [ [ 'DATETIME_STRPTIME', '%Y-%m-%dT%T%z' ] ],
    ] );

=item DATETIME_FORMAT

check with DateTime::Format::***. for example, L<DateTime::Format::HTTP>,
L<DateTime::Format::Mail>, L<DateTime::Format::MySQL> and etc.

    my $q = CGI->new;
    $q->param( datetime => '2004-04-26 19:09:21' );

    my $result = FormValidator::Simple->check( $q => [
      datetime => [ [qw/DATETIME_FORMAT MySQL/] ],
    ] );

=item GREATER_THAN

numeric comparison

    my $result = FormValidator::Simple->check( $q => [
        age => [ ['GREATER_THAN', 25] ],
    ] );

=item LESS_THAN

numeric comparison

    my $result = FormValidator::Simple->check( $q => [
        age => [ ['LESS_THAN', 25] ],
    ] );

=item EQUAL_TO

numeric comparison

    my $result = FormValidator::Simple->check( $q => [
        age => [ ['EQUAL_TO', 25] ],
    ] );

=item BETWEEN

numeric comparison

    my $result = FormValidator::Simple->check( $q => [
        age => [ ['BETWEEN', 20, 25] ],
    ] );

=item ANY

check if there is not blank data in multiple data.

    my $result = FormValidator::Simple->check( $q => [ 
        { some_data => [qw/param1 param2 param3/] } => ['ANY']
    ] );

=item IN_ARRAY

check if the food ordered is in menu

    my $result = FormValidator::Simple->check( $q => [
        food => [ ['IN_ARRAY', qw/noodle soba spaghetti/] ],
    ] };

=back

=head1 HOW TO LOAD PLUGINS

    use FormValidator::Simple qw/Japanese CreditCard/;

L<FormValidator::Simple::Plugin::Japanese>, L<FormValidator::Simple::Plugin::CreditCard> are loaded.

or use 'load_plugin' method.

    use FormValidator::Simple;
    FormValidator::Simple->load_plugin('FormValidator::Simple::Plugin::CreditCard');

If you want to load plugin which name isn't in FormValidator::Simple::Plugin namespace, use +.

    use FormValidator::Simple qw/+MyApp::ValidatorPlugin/;

=head1 MESSAGE HANDLING

You can custom your own message with key and type.

    [% IF result.has_error %]
        [% FOREACH key IN result.error %]
            [% FOREACH type IN result.error(key) %]
            <p>error message:[% type %] - [% key %]</p>
            [% END %]
        [% END %]
    [% END %]

And you can also set messages configuration before.
You can prepare configuration as hash reference.

    FormValidator::Simple->set_messages( {
        action1 => {
            name => {
                NOT_BLANK => 'input name!',
                LENGTH    => 'input name (length should be between 0 and 10)!',
            },
            email => {
                DEFAULT => 'input correct email address!',
            },
        },
    } );

or a YAML file.

    # messages.yml
    DEFAULT:
        name:
            DEFAULT: name is invalid!
    action1:
        name:
            NOT_BLANK: input name!
            LENGTH: input name(length should be between 0 and 10)!
        email:
            DEFAULT: input correct email address!
    action2:
        name:
            DEFAULT: ...
            
    # in your perl-script, set the file's path.
    FormValidator::Simple->set_messages('messages.yml');

DEFAULT is a special type.
If it can't find setting for indicated validation-type, it uses message set for DEFAULT.

after setting, execute check(),

    my $result = FormValidator::Simple->check( $q => [
        name  => [qw/NOT_BLANK/, [qw/LENGTH 0 10/] ],
        email => [qw/NOT_BLANK EMAIL_LOOSE/, [qw/LENGTH 0 20/] ],
    ] );

    # matching result and messages for indicated action.
    my $messages = $result->messages('action1');

    foreach my $message ( @$messages ) {
        print $message, "\n";
    }

    # or you can get messages as hash style.
    # each fieldname is the key
    my $field_messages = $result->field_messages('action1');
    if ($field_messages->{name}) {
        foreach my $message ( @{ $field_messages->{name} } ) {
            print $message, "\n";
        }
    }

When it can't find indicated action, name, and type, it searches proper message from DEFAULT action.
If in template file,

    [% IF result.has_error %]
        [% FOREACH msg IN result.messages('action1') %]
        <p>[% msg %]</p>
        [% END %]
    [% END %]

you can set each message format.

    FormValidator::Simple->set_message_format('<p>%s</p>');
    my $result = FormValidator::Simple->check( $q => [
        ...profile
    ] );

    [% IF result.has_error %]
        [% result.messages('action1').join("\n") %]
    [% END %]

=head1 RESULT HANDLING

See L<FormValidator::Simple::Results>

=head1 FLAGGED UTF-8

If you set encoding like follows, it automatically decode the
result messages.

    FormValidtor::Simple->set_mesasges_decode_from('utf-8');

=head1 SEE ALSO

L<Data::FormValidator>

http://sl.edge.jp/ (Japanese)

http://sourceforge.jp/projects/sledge

=head1 AUTHOR

Lyo Kato E<lt>lyo.kato@gmail.comE<gt>

=head1 COPYRIGHT AND LICENSE

This library is free software.
You can redistribute it and/or modify it under the same terms as perl itself.

=cut