#!/usr/bin/perl use strict; use warnings; use Data::FormValidator::Constraints qw(:closures); use Data::FormValidator; my %input = ( input1 => 'eins', input2 => 'zwei', input3 => 'drei', input4 => 'vier'); my $results = validate_input(\%input); print "result=$results\n"; # Print the name of missing fields if ( $results->has_missing ) { for my $f ( $results->missing ) { print $f, " is missing\n"; } } # Print the name of invalid fields if ( $results->has_invalid ) { for my $f ( $results->invalid ) { print $f, " is invalid: ", $results->invalid( $f ), "\n"; my $array_ref = $results->invalid( $f ); my @array = @$array_ref; foreach(@array){ print $_ . ' array' . "\n"; } } } # Print unknown fields if ( $results->has_unknown ) { for my $f ( $results->unknown ) { print $f, " is unknown\n"; } } # Print valid fields for my $f ( $results->valid() ) { print 'valid ' . $f, " = ", $results->valid( $f ), "\n"; } sub validate_input{ my $input_hash_ref = shift; my $big_test = { filters => 'trim', required => [qw( input1 input2 input3 input4 )], constraint_methods => { input1 => validate_test1( 'exists', {fields => [qw( input1 ) ]} ), input2 => validate_test2( 'exists', {fields => [qw( input2 ) ]} ), input3 => validate_test3( 'exists', {fields => [qw( input3 ) ]} ), input4 => validate_test4( 'exists', {fields => [qw( input4 ) ]} ), } }; my $results = Data::FormValidator->check($input_hash_ref, $big_test); print "result1=$results\n"; return($results); } sub validate_test1{ my ($type,$attrs) = @_; return sub { my $dfv = shift; $dfv->name_this('validate_test1'); # value of 'prospective_date' parameter my $val = $dfv->get_current_constraint_value(); # get other data to refer to my $data = $dfv->get_filtered_data; return(undef); } } sub validate_test2{ my ($type,$attrs) = @_; return sub { my $dfv = shift; $dfv->name_this('validate_test2'); # value of 'prospective_date' parameter my $val = $dfv->get_current_constraint_value(); # get other data to refer to my $data = $dfv->get_filtered_data; return(1); } } sub validate_test3{ my ($type,$attrs) = @_; return sub { my $dfv = shift; $dfv->name_this('validate_test3'); # value of 'prospective_date' parameter my $val = $dfv->get_current_constraint_value(); # get other data to refer to my $data = $dfv->get_filtered_data; return(1); } } sub validate_test4{ my ($type,$attrs) = @_; return sub { my $dfv = shift; $dfv->name_this('validate_test4'); # value of 'prospective_date' parameter my $val = $dfv->get_current_constraint_value(); # get other data to refer to my $data = $dfv->get_filtered_data; return(1); } }