http://www.perlmonks.org?node_id=329592

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

HI All

I have a html form which has two date fields - datefrom & dateto. I am using CGI::Application::ValidateRM to validate form to make sure that users have entered dates. This is working fine.

Problem is I am not able to force user to enter datefrom < dateto, which is important. I can get around this by using sql and then reload the page all over again but I want to avoid it. Using Date::Calc->Delta_Days I am able to calculate total no of days, either +tive or -tive. However I am not able to fit this into profile of the page. At the moment my check_rm profile read like this

my ($results,$err_page) = $self->check_rm('holidayRequest',{ required => ['holidayfrom','holidayto'], optional => ['usercomments'], filters => ['trim'], msgs=>{ any_errors => 'err__', prefix=>'err_', }, }); return $err_page if $err_page;

Thanks in advance for your help

Replies are listed 'Best First'.
Re: Dates and formvalidation
by larsen (Parson) on Feb 17, 2004 at 14:31 UTC
    I'm working on similar problems with Data::FormValidator. I see it's not CGI::Application::ValidateRM, but given the relationship between these two modules, maybe the following example will give you some benefit (in other words: everything I'm going to say is supposed to be useful under the hypothesis that CGI::A::VRM's interface is similar to D::FV's one):
    # ... This is part of the validator profile ... constraints => { ... holidayto => { params => [ qw/ holidayfrom holidayto / ] constraint => sub { my $from = shift; my $to = shift; # Perform some check with Date::Calc... } } , ... }
    Note that you can supply an anonymous sub, and a list of parameters that will be passed to the subroutine itself.
Re: Dates and formvalidation
by edan (Curate) on Feb 17, 2004 at 14:12 UTC

    You'll need to use JavaScript or some other client-side validation code in order to avoid a round-trip with the server.

    Update:: Upon closer inspection of your post, I think that the other answers are probably in the right direction, and not what I said. It threw me off when you said you were reloading the page, but since you are already performing the validation on the server (when the form is submitted), and you just need to add further validation, so listen to the other answers (they look right).

    --
    edan (formerly known as 3dan)

Re: Dates and formvalidation
by swiftone (Curate) on Feb 17, 2004 at 14:33 UTC
    Check the docs for Data::FormValidator (ValidateRM uses D::FV profiles). You'll want to write your own constraint, it should be fairly straightforward once you decipher the docs.

    To clarify the other recommendation, don't rely on client-side code, as the client can disable any such code. You can include it to avoid server work, but you'll want the code on the server just in case anyway.

Re: Dates and formvalidation
by jdtoronto (Prior) on Feb 17, 2004 at 14:32 UTC
    Read the docs for Data::FormValidator on constraints, then create a constraint that checks for the relationship you need.
Re: Dates and formvalidation
by bar10der (Beadle) on Feb 19, 2004 at 11:48 UTC
    Thanks Larsen, I did try to write my own constraint and though it does not give me any error, it does not perform the validation. Here is my code-
    my $profile = { required => ['holidayfrom','holidayto'], optional => ['usercomments'], filters => ['trim'], constraints => { hoidayto => { params => [ 'holidayfrom', 'holidayto' ], constraint => sub { my $from = shift; my $to = shift; my ($d1,$m1,$y1) = split("/",$from); my ($d2,$m2,$y2) = split("/",$to); my $Dd = Delta_Days( $y1, $m1, $d1, $y2, $m2, $d2 ); if($Dd < 0){return 1;} return 0; } } }, msgs=>{ any_errors => 'err__', prefix=>'err_', }, };

    I am sure I am making some basic mistake. I am fairly new to Perl.