1Workflow::Validator::InUEsneurmeCroantterdiTbyuptee(d3W)PoerrklflDoowc:u:mVeanltiadtaitoonr::InEnumeratedType(3)
2
3
4
6 Workflow::Validator::InEnumeratedType - Ensure a value is one of a
7 declared set of values
8
10 # Inline the enumeration...
11
12 <action name="PlayGame">
13 <validator name="InEnumeratedType">
14 <value>Rock</value>
15 <value>Scissors</value>
16 <value>Paper</value>
17 <arg value="$play"/>
18 </validator>
19 </action>
20
21 # Or declare it in the validator to be more readable...
22 <validator name="RSP"
23 class="Validator::InEnumeratedType">
24 <value>Rock</value>
25 <value>Scissors</value>
26 <value>Paper</value>
27 </validator>
28
29 # ...and use it in your action
30 <action name="PlayGame">
31 <validator name="RSP">
32 <arg value="$play"/>
33 </validator>
34 </action>
35
37 This validator ensures that a value matches one of a set of values. You
38 declare the values in the set (or enumerated type) in either the main
39 validator declaration or in the declaration inside the action, then
40 pass a single argument of the value in the context you would like to
41 check.
42
43 Declaring the members of the enumerated type in the validator configu‐
44 ration makes for more readable (and brief) action configurations, as
45 well as making the types more reusable, but it is really up to you.
46
48 Strategy
49
50 Unlike some other validator classes this one is setup to be subclass‐
51 able. It is usable as-is, of course, but many times you will find that
52 you have need of more interesting types in your enumeration than simple
53 strings. So this class provides the hooks for you to simply create your
54 own.
55
56 For instance, in a trouble ticket system you may have the idea that
57 tickets can only be assigned to particular users. Maybe they are in a
58 'worker' role, maybe they are some administrators, whatever. By creat‐
59 ing a class to have these users as an enumerated type, combined with
60 declaring the required Action fields, you make for a pretty powerful
61 piece of reflection.
62
63 Onto the code. First we declare a field type of 'worker':
64
65 <field type="worker"
66 class="MyApp::Field::Worker"/>
67
68 Next a validator of this enumerated type:
69
70 <validator name="IsWorker"
71 class="MyApp::Validator::WorkerEnumeration"/>
72
73 We then associate this field type with a field in the action and the
74 validator to ensure the user selects a worker from the right pool:
75
76 <action name="AssignTicket">
77 <field name="assignee"
78 type="worker"
79 is_required="yes"/>
80 ...
81 <validator name="IsWorker">
82 <arg value="$assignee"/>
83 </validator>
84
85 Note that the name of the field and the name used in the validator are
86 the same. This allows external applications to query the action for its
87 fields, get 'assignee' as the name and get a list of User objects (or
88 something similar) as the types from which to choose a value, and
89 checks that same field to ensure a correct choice was submitted.
90
91 The implementation for the validator might look like:
92
93 package MyApp::Validator::WorkerEnumeration;
94
95 sub validate {
96 my ( $self, $wf, $worker_id ) = @_;
97 my $ticket = $context->param( 'ticket' );
98 unless ( $ticket ) {
99 my $ticket_id = $context->param( 'ticket_id' );
100 $ticket = Ticket->fetch( $ticket_id );
101 }
102 my $workers = $ticket->fetch_available_workers();
103 my @worker_id = map { $_->id } @{ $workers };
104 $self->add_enumerated_values( @worker_id );
105 $self->SUPER::validate( $wf, $worker_id );
106 }
107
108 METHODS
109
110 #=head3 _init( \%params )
111
112 #=head3 validator
113
114 #=head3 add_enumerated_values( @values )
115
116 #=head3 get_enumerated_values()
117
118 is_enumerated_value( $value )
119
120 This is most often the single method you will want to modify.
121
123 Copyright (c) 2003-2004 Chris Winters. All rights reserved.
124
125 This library is free software; you can redistribute it and/or modify it
126 under the same terms as Perl itself.
127
129 Chris Winters <chris@cwinters.com>
130
131
132
133perl v5.8.8 2007-W0o4r-k2f5low::Validator::InEnumeratedType(3)