File Coverage

lib/Workflow/Validator/HasRequiredField.pm
Criterion Covered Total %
statement 18 20 90.0
branch 2 4 50.0
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 26 30 86.6


line stmt bran cond sub pod time code
1              
2             use warnings;
3 21     21   669 use strict;
  21         51  
  21         739  
4 21     21   112 use base qw( Workflow::Validator );
  21         32  
  21         587  
5 21     21   104 use Workflow::Exception qw( validation_error );
  21         43  
  21         6339  
6 21     21   469  
  21         31  
  21         2827  
7             $Workflow::Validator::HasRequiredField::VERSION = '1.61';
8              
9             my ( $self, $wf, @required_fields ) = @_;
10             my $context = $wf->context;
11 53     53 1 100 my @no_value = ();
12 53         90 foreach my $field (@required_fields) {
13 53         86 unless ( defined $context->param($field) ) {
14 53         75 push @no_value, $field;
15 36 50       92 }
16 0         0 }
17             if ( scalar @no_value ) {
18             validation_error "The following fields require a value: ",
19 53 50       182 join(', ',@no_value), { invalid_fields => \@no_value };
20 0           }
21             }
22              
23             1;
24              
25              
26             =pod
27              
28             =head1 NAME
29              
30             Workflow::Validator::HasRequiredField - Validator to ensure certain data are in the context
31              
32             =head1 VERSION
33              
34             This documentation describes version 1.61 of this package
35              
36             =head1 SYNOPSIS
37              
38             # Validator is created automatically when you mark a field as
39             # 'is_required=yes' in the action, such as:
40              
41             <action name="CreateUser">
42             <field name="username"
43             is_required="yes"
44             source_class="App::Fied::ValidUsers"/>
45             ...
46              
47             =head1 DESCRIPTION
48              
49             This is a simple validator to ensure that each of the fields you have
50             marked with the 'is_required' property as 'yes' are indeed present
51             before the associated action is executed.
52              
53             for instance, given the configuration:
54              
55             <action name="CreateUser">
56             <field name="username"
57             is_required="yes"/>
58             <field name="email"
59             is_required="yes"/>
60             <field name="office">
61             </action>
62              
63             An action executed with such a context:
64              
65             my $wf = FACTORY->get_workflow( $id );
66             $wf->context( username => 'foo' );
67             $wf->context( office => 'Ottumwa' );
68             $wf->execute_action( 'CreateUser' );
69              
70             Would fail with a message:
71              
72             The following fields require a value: email
73              
74             You normally do not need to configure this validator yourself. It gets
75             generated automatically when the Action configration is read
76             in. However, if you do need to create it yourself:
77              
78             <action name='Foo'>
79             <validator name="HasRequiredField">
80             <arg value="fieldOne"/>
81             <arg value="field_two"/>
82             </validator>
83             <?action>
84              
85             Note that we do not try to match the value in the context against a
86             set of known values or algorithm, just see if the value is defined --
87             using the Perl notion for defined rather than true/false, which means
88             '0' and the empty string will both be valid.
89              
90             =head2 METHODS
91              
92             =head3 validate
93              
94             Validates whether a given set of required fields are defined.
95              
96             Takes two parameters: a workflow object and an array of names of fields.
97              
98             The provided fields are matched against the workflow in question and
99             L<Workflow::Exception>'s are thrown in case of missing fields.
100              
101             =head1 COPYRIGHT
102              
103             Copyright (c) 2003-2022 Chris Winters. All rights reserved.
104              
105             This library is free software; you can redistribute it and/or modify
106             it under the same terms as Perl itself.
107              
108             Please see the F<LICENSE>
109              
110             =head1 AUTHORS
111              
112             Please see L<Workflow>
113              
114             =cut