Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am using Data::FormValidator to do my html form validation. I had a question on how I would be able to make a constraint run through a local routine that I create? I looked at the source and it looks like the module tacks on "valid" before each subroutine that does validation. I even tried naming my local sub valid+ and that didnt work. I might be overlooking something simple here. Any help or links would be appreciated. I also found this today Data::FormValidator-3.15 but written a lot of code already using the older module.
package TEST::Validator::Test; use Data::FormValidator; use strict; @TEST::Validator::Test::ISA= qw( Data::FormValidator ); use constant LOGIN => 'login'; my $form_validations = { LOGIN() => { required => [ qw( ipf_org_username ipf_username ) ], optional => [ qw( ipf_password ipf_confirm_password ) ], constraints => { ipf_passowrd => { constraint => "check_password +", params => [ qw( ipf_passw +ord ipf_confirm_password ) ], }, } }, }; sub get_validator { my $self = shift; my %args = @_; my $validator = new $self( $form_validations ); return($validator); } sub check_password { my ( $password, $confirm_password ) = shift; return ( $password eq $confirm_password ); } sub valid_check_password { my ( $password, $confirm_password ) = shift; return ( $password eq $confirm_password ); } 1;

Replies are listed 'Best First'.
Re: Data::FormValidator constraints against local sub routine
by cees (Curate) on Dec 08, 2003 at 20:55 UTC
    I had a question on how I would be able to make a constraint run through a local routine that I create?

    You can just pass a reference to the subroutine in the constraint. In your code above, change "check_password" to \&check_password:

    constraints => { ipf_password => { constraint => \&check_password, params => [ qw( ipf_password ipf_confirm_password ) ], }, }

    Or you can do it with an anonymous subroutine:

    constraints => { ipf_password => { constraint => sub { $_[0] eq $_[1] }, params => [ qw( ipf_password ipf_confirm_password ) ], }, }

    I suspect that 'ipf_passowrd' was a transcription error when you wrote this message...

    Also, I have to wonder why you are using version 1.6 of the module. It will be much less painful to upgrade to the latest version now, then it will be once you put this code in production. Since the current version is 3.15, you are way out of date...

    - Cees

Re: Data::FormValidator constraints against local sub routine
by jdtoronto (Prior) on Dec 08, 2003 at 20:45 UTC
    I meant to send this comment earlier in the day, I hope it is still useful:
    FRAGMENT - not complete: my ($results, $err_page) = $self->check_rm('qs_display', { required => [qw/upfirstname uplastname upemail upusername +uppassword confirm_password/], optional => [qw/upzip upcountry/], filters => ['trim'], constraints => { uppassword => { #Make sure passwords are the same name => 'user_passwords', constraint => \&check_password, params => [qw(uppassword confirm_password)], }, upusername => { #Is username unique? name => 'user_username', constraint => \&check_username, params => [qw(upusername)], }, upemail => 'email', upzip => 'zip_or_postcode', }, msgs => { any_errors => 'err__', prefix => 'err_', constraints => { 'user_passwords' => "Your passwords don't match", 'user_username' => "Username not unique", }, }, } ); return $err_page if $err_page; # Results are valid my $dfv_results = $results->valid(); print STDERR Dumper( $dfv_results ); $self->header_type('redirect'); $self->header_props( -url => 'http://posiedon.mine.nu'); return; } sub check_password { my ($p1, $p2) = @_; if ($p1 eq $p2) { return 1; } else { return 0; } }
    I think it is pretty straightforward. I get the two passowrd values from my form, the trivial sub (which could be much simplified!) merely returns true or false. I am using D::FV 3.14 with CGI::Application

    The code is not original, Cees Hek gave me the outline in an answer to a question on the CGI::App mail list.

    jdtoronto

Re: Data::FormValidator constraints against local sub routine
by Anonymous Monk on Dec 08, 2003 at 21:06 UTC
    Thanks for both answers. I will upgrade now.