1Workflow::Validator(3)User Contributed Perl DocumentationWorkflow::Validator(3)
2
3
4

NAME

6       Workflow::Validator - Ensure data are valid
7

SYNOPSIS

9        # First declare the validator...
10        <validator name="DateValidator"
11                   class="MyApp::Validator::Date">
12          <param name="date_format" value="%Y-%m-%d %h:%m"/>
13        </validator>
14
15        # Then associate the validator with runtime data from the context...
16        <action name="MyAction">
17           <validator name="DateValidator">
18              <arg>$due_date</arg>
19           </validator>
20        </action>
21
22        # TODO: You can also inintialize and instantiate in one step if you
23        # don't need to centralize or reuse (does this work?)
24
25        <action name="MyAction">
26           <validator class="MyApp::Validator::Date">
27              <param name="date_format" value="%Y-%m-%d %h:%m"/>
28              <arg>$due_date</arg>
29           </validator>
30        </action>
31
32        # Then implement the logic
33
34        package MyApp::Validator::Date;
35
36        use strict;
37        use base qw( Workflow::Validator );
38        use DateTime::Format::Strptime;
39        use Workflow::Exception qw( configuration_error );
40
41        __PACKAGE__->mk_accessors( 'formatter' );
42
43        sub _init {
44            my ( $self, $params ) = @_;
45            unless ( $params->{date_format} ) {
46                configuration_error
47                    "You must define a value for 'date_format' in ",
48                    "declaration of validator ", $self->name;
49            }
50            if ( ref $params->{date_format} ) {
51                configuration_error
52                    "The value for 'date_format' must be a simple scalar in ",
53                    "declaration of validator ", $self->name;
54            }
55            my $formatter = DateTime::Format::Strptime->new(
56                                     pattern => $params->{date_format},
57                                     on_error => 'undef' );
58            $self->formatter( $formatter );
59        }
60
61        sub validate {
62            my ( $self, $wf, $date_string ) = @_;
63            my $fmt = $self->formatter;
64            my $date_object = $fmt->parse_datetime( $date_string );
65            unless ( $date_object ) {
66                validation_error
67                    "Date '$date_string' does not match pattern '", $fmt->pattern, "' ",
68                    "due to error '", $fmt->errstr, "'";
69            }
70        }
71

DESCRIPTION

73       Validators specified by 'validator_name' are looked up in the Work‐
74       flow::Factory which reads a separate configuration and generates val‐
75       idators. (Generally all validators should be declared, but it is not
76       required.)
77
78       Validators are objects with a single public method, 'validate()' that
79       take as arguments a workflow object and a list of parameters. The
80       parameters are filled in by the workflow engine according to the
81       instantiation declaration in the Action.
82
83       The idea behind a validator is that it validates data but does not care
84       where it comes from.
85

SUBCLASSING

87       Strategy
88
89       Methods
90
91       #=head3 init
92
93       _init( \%params )
94
95       Called when the validator is first initialized. If you do not have suf‐
96       ficient information in "\%params" you should throw an exception.
97
98       validate( $workflow, $data )
99
100       Determine whether your $data is true or false. If necessary you can get
101       the application context information from the $workflow object.
102
104       Copyright (c) 2003-2004 Chris Winters. All rights reserved.
105
106       This library is free software; you can redistribute it and/or modify it
107       under the same terms as Perl itself.
108

AUTHORS

110       Chris Winters <chris@cwinters.com>
111
112
113
114perl v5.8.8                       2007-04-25            Workflow::Validator(3)
Impressum