1Workflow::Validator(3)User Contributed Perl DocumentationWorkflow::Validator(3)
2
3
4
6 Workflow::Validator - Ensure data are valid
7
9 This documentation describes version 1.05 of this package
10
12 # First declare the validator...
13 <validator name="DateValidator"
14 class="MyApp::Validator::Date">
15 <param name="date_format" value="%Y-%m-%d %h:%m"/>
16 </validator>
17
18 # Then associate the validator with runtime data from the context...
19 <action name="MyAction">
20 <validator name="DateValidator">
21 <arg>$due_date</arg>
22 </validator>
23 </action>
24
25 # TODO: You can also inintialize and instantiate in one step if you
26 # don't need to centralize or reuse (does this work?)
27
28 <action name="MyAction">
29 <validator class="MyApp::Validator::Date">
30 <param name="date_format" value="%Y-%m-%d %h:%m"/>
31 <arg>$due_date</arg>
32 </validator>
33 </action>
34
35 # Then implement the logic
36
37 package MyApp::Validator::Date;
38
39 use strict;
40 use base qw( Workflow::Validator );
41 use DateTime::Format::Strptime;
42 use Workflow::Exception qw( configuration_error );
43
44 __PACKAGE__->mk_accessors( 'formatter' );
45
46 sub _init {
47 my ( $self, $params ) = @_;
48 unless ( $params->{date_format} ) {
49 configuration_error
50 "You must define a value for 'date_format' in ",
51 "declaration of validator ", $self->name;
52 }
53 if ( ref $params->{date_format} ) {
54 configuration_error
55 "The value for 'date_format' must be a simple scalar in ",
56 "declaration of validator ", $self->name;
57 }
58 my $formatter = DateTime::Format::Strptime->new(
59 pattern => $params->{date_format},
60 on_error => 'undef' );
61 $self->formatter( $formatter );
62 }
63
64 sub validate {
65 my ( $self, $wf, $date_string ) = @_;
66 my $fmt = $self->formatter;
67 my $date_object = $fmt->parse_datetime( $date_string );
68 unless ( $date_object ) {
69 validation_error
70 "Date '$date_string' does not match pattern '", $fmt->pattern, "' ",
71 "due to error '", $fmt->errstr, "'";
72 }
73 }
74
76 Validators specified by 'validator_name' are looked up in the
77 Workflow::Factory which reads a separate configuration and generates
78 validators. (Generally all validators should be declared, but it is not
79 required.)
80
81 Validators are objects with a single public method, 'validate()' that
82 take as arguments a workflow object and a list of parameters. The
83 parameters are filled in by the workflow engine according to the
84 instantiation declaration in the Action.
85
86 The idea behind a validator is that it validates data but does not care
87 where it comes from.
88
90 Strategy
91 Methods
92 init( \%params )
93
94 Called when the validator is first initialized. If you do not have
95 sufficient information in "\%params" you should throw an exception.
96
97 _init
98
99 This is a dummy method, please see "init".
100
101 validate( $workflow, $data )
102
103 Determine whether your $data is true or false. If necessary you can get
104 the application context information from the $workflow object.
105
107 Copyright (c) 2003-2004 Chris Winters. All rights reserved.
108
109 This library is free software; you can redistribute it and/or modify it
110 under the same terms as Perl itself.
111
113 Chris Winters <chris@cwinters.com>
114
115
116
117perl v5.30.1 2020-01-30 Workflow::Validator(3)