| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Workflow::Validator::HasRequiredField; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 21 |  |  | 21 |  | 760 | use warnings; | 
|  | 21 |  |  |  |  | 38 |  | 
|  | 21 |  |  |  |  | 799 |  | 
| 4 | 21 |  |  | 21 |  | 137 | use strict; | 
|  | 21 |  |  |  |  | 53 |  | 
|  | 21 |  |  |  |  | 630 |  | 
| 5 | 21 |  |  | 21 |  | 125 | use base qw( Workflow::Validator ); | 
|  | 21 |  |  |  |  | 46 |  | 
|  | 21 |  |  |  |  | 7405 |  | 
| 6 | 21 |  |  | 21 |  | 440 | use Workflow::Exception qw( validation_error ); | 
|  | 21 |  |  |  |  | 52 |  | 
|  | 21 |  |  |  |  | 3317 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | $Workflow::Validator::HasRequiredField::VERSION = '1.62'; | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | sub validate { | 
| 11 | 53 |  |  | 53 | 1 | 152 | my ( $self, $wf, @required_fields ) = @_; | 
| 12 | 53 |  |  |  |  | 128 | my $context  = $wf->context; | 
| 13 | 53 |  |  |  |  | 116 | my @no_value = (); | 
| 14 | 53 |  |  |  |  | 123 | foreach my $field (@required_fields) { | 
| 15 | 36 | 50 |  |  |  | 81 | unless ( defined $context->param($field) ) { | 
| 16 | 0 |  |  |  |  | 0 | push @no_value, $field; | 
| 17 |  |  |  |  |  |  | } | 
| 18 |  |  |  |  |  |  | } | 
| 19 | 53 | 50 |  |  |  | 240 | if ( scalar @no_value ) { | 
| 20 | 0 |  |  |  |  |  | validation_error "The following fields require a value: ", | 
| 21 |  |  |  |  |  |  | join(', ',@no_value), { invalid_fields => \@no_value }; | 
| 22 |  |  |  |  |  |  | } | 
| 23 |  |  |  |  |  |  | } | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | 1; | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | __END__ | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | =pod | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | =head1 NAME | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | Workflow::Validator::HasRequiredField - Validator to ensure certain data are in the context | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | =head1 VERSION | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | This documentation describes version 1.62 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 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | This is a simple validator to ensure that each of the fields you have | 
| 53 |  |  |  |  |  |  | marked with the 'is_required' property as 'yes' are indeed present | 
| 54 |  |  |  |  |  |  | before the associated action is executed. | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | for 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 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | An 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 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | Would fail with a message: | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | The following fields require a value: email | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | You normally do not need to configure this validator yourself. It gets | 
| 78 |  |  |  |  |  |  | generated automatically when the Action configration is read | 
| 79 |  |  |  |  |  |  | in. 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 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | Note that we do not try to match the value in the context against a | 
| 89 |  |  |  |  |  |  | set of known values or algorithm, just see if the value is defined -- | 
| 90 |  |  |  |  |  |  | using 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 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | Validates whether a given set of required fields are defined. | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | Takes two parameters: a workflow object and an array of names of fields. | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | The provided fields are matched against the workflow in question and | 
| 102 |  |  |  |  |  |  | L<Workflow::Exception>'s are thrown in case of missing fields. | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | =head1 COPYRIGHT | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | Copyright (c) 2003-2023 Chris Winters. All rights reserved. | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | This library is free software; you can redistribute it and/or modify | 
| 109 |  |  |  |  |  |  | it under the same terms as Perl itself. | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | Please see the F<LICENSE> | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | =head1 AUTHORS | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | Please see L<Workflow> | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | =cut |