Data-FormValidator-Tutorial
view release on metacpan or search on metacpan
lib/Data/FormValidator/Tutorial.pm view on Meta::CPAN
};
This is saying that the C<email> field is valid only when the regex pattern fits the input. Now I just gave a very rough (and probably inaccurate) pattern, just so you get the idea. So many people have come across this type of thing, that there's a...
...
'email' => 'email',
...
Pretty sweet, huh? There are other things you can do on the right-hand side of a constraint, too. Right now, you've seen a regexp pattern and using a built-in constraint, but you can also point to a subroutine and do your own methodology:
...
'email' => sub {
my $email = shift;
if ( $email =~ /purdy\.info$/ ) {
# only accepting emails from my domain
return 1;
} else {
return 0;
}
},
...
You could also bump that subroutine outside the data structure somewhere and refer to it by name:
...
'email' => \&my_domain_email(),
...
sub my_domain_email {
...
}
Lastly, either you or the PHB (pointed-haired boss) will note that password1 and password2 should be confirmed to be the same thing, to make sure the user didn't typo the password wrong. I think you can handle that yourself, given the ammo I've give...
...
'constraints' => {
'email' => 'email',
'password1' => {
'constraint' => "check_passwords",
'params' => [ qw( password1 password2 ) ],
},
},
...
So what this is saying is to check C<password1>, but instead of pointing to a regexp pattern, a built-in constraint or a subroutine, it's actually pointing to a more complex hashref. Within that hashref is C<constraint>, which could be confusing, bu...
sub check_passwords {
my ( $pw1, $pw2 ) = @_;
if ( $pw1 eq $pw2 ) {
return 1;
} else {
return 0;
}
}
=head1 TODO
This is just an early release of this tutorial - we're using the release early & often mentality, so there's still a few things left to do. We want to address of of the more complicated aspects of dfv, like Filters, etc.
=head1 SEE ALSO
L<Data::FormValidator> and the dfv mailing list: L<http://lists.sourceforge.net/lists/listinfo/cascade-dataform>
Also, Jason Purdy presented dfv at ApacheCon 2005 (L<http://www.apachecon.com>). You can download the slides here:
L<http://www.purdy.info/useperl/th06_slides.pdf>
=head1 AUTHORS
Originally written by T. M. Brannon, <tbone@cpan.org>
William McKee, <william@knowmad.com> and Jason Purdy, <jason@purdy.info>
=head1 LICENSE
Copyright (C) 2004-2005 Jason Purdy, <jason@purdy.info> and William McKee, <william@knowmad.com>
This library is free software. You can modify and or distribute it under the same terms as Perl itself.
=cut
( run in 0.520 second using v1.01-cache-2.11-cpan-39bf76dae61 )