23c23 < package Data::FormValidator; --- > package Data::FormValidator::Enhanced; 463a464,472 > #!#################################################################### > my %alternates; > # get list of alternates > while ( my ( $field, $deps) = each %{$profile->{alternates}} ) { > foreach my $dep (_arrayify($deps)){ > $alternates{$dep} = 1; > } > } > ###################################################################### 519a529 > #!#################################################################### 522c532 < grep { not (exists $optional{$_} or exists $required{$_} ) } keys %valid; --- > grep { not (exists $optional{$_} or exists $required{$_} or exists $alternates{$_} ) } keys %valid; 526a537 > ###################################################################### 536a548,558 > > #!#################################################################### > # Check for the absence of alternate fields > while ( my ( $field, $deps) = each %{$profile->{alternates}} ) { > my $valid_alternate = 0; > foreach my $dep (_arrayify($deps)){ > $valid_alternate = 1 if exists $valid{$dep}; > } > push @missings, $field unless $valid_alternate; > } > ###################################################################### #### #!/usr/bin/perl -w use strict; use Data::FormValidator::Enhanced; my $validator_profile = { 'contact' => { required => [], optional => [], alternates => { zip_or_postal => [ qw( zip postal ) ], }, constraints => { zip => "zip", postal => "post_code", }, filters => [ "trim" ], }, }; my $validator = Data::FormValidator::Enhanced->new($validator_profile); my %param = ( zip => '', postal => '',); my %error; ($error{valid}, $error{missing}, $error{invalid}, $error{unknown}) = $validator->validate(\%param, 'contact'); my $errortext = ''; $errortext .= "The following required fields were not completed: \n" . join("\n", @{$error{missing}}) . "\n"; $errortext .= "The following fields were not completed correctly: \n" . join("\n", @{$error{invalid}}) . "\n"; print $errortext; #### use strict; use CGI qw(header); use Template; my $data = do {local $/; }; my $template = Template->new(); my $params = { questions => [ { question => 'What is your favorite number?', radio => [ { regular => 'question01', value => 'a', label => 5, }, { regular => 'question01', value => 'b', label => 7, }, { regular => 'question01', value => 'c', label => 13, }, { special => 'question01', value => 'd', label => 'Other', text => 'other', }, ], }, { question => 'Where did you hear about this product?', radio => [ { regular => 'question02', value => 'a', label => 'Friend', }, { special => 'question02', value => 'b', label => 'Magazine', text => 'magname', }, { regular => 'question02', value => 'c', label => 'Brochure', }, { special => 'question02', value => 'd', label => 'Other', text => 'other', }, ], }, ], }; print header; $template->process(\$data, $params) or die $!; __DATA__
[% FOR q = questions %]

[% q.question %]
[% FOR r = q.radio %] [% UNLESS r.special %] [% r.label %]
[% ELSE %] [% r.label%] (please specify:)
[% END %] [% END %] [% END %]

##
## use strict; use CGI qw(header); use Template; my $data = do {local $/; }; my $template = Template->new(); my $params = { questions => [ { question => 'What is your favorite number?', radio => [ { regular => 'question01', value => 'a', label => 5, }, { regular => 'question01', value => 'b', label => 7, }, { regular => 'question01', value => 'c', label => 13, }, { special => 'question01', value => 'd', label => 'Other', text => 'other', }, ], }, { question => 'Where did you hear about this product?', radio => [ { regular => 'question02', value => 'a', label => 'Friend', }, { special => 'question02', value => 'b', label => 'Magazine', text => 'magname', }, { regular => 'question02', value => 'c', label => 'Brochure', }, { special => 'question02', value => 'd', label => 'Other', text => 'other', }, ], }, ], }; print header; $template->process(\$data, $params) or die $!; __DATA__
[% FOR questions %]

[% question %]
[% FOR radio %] [% UNLESS special %] [% label %]
[% ELSE %] [% label%] (please specify:)
[% END %] [% END %] [% END %]