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