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

Howdy Monks!

I have a quick question, I was going to do some form validation using a lot of $blah =param(('whatever')); I think thats the syntax. Then I was going to check for eq '' and if it does, do an error. It was getting messy! REALLY messy.

Is there any modules around that are easy to understand, I looked at a few like Regexp::Common and Params::Validate...However I cannot figure out if these would be good for what I need them to do...

I just need something simple that checks for a empty or use a regex...and then set a scalar to an error message which I can use later.

Also is there any better way to get like 20 form values without calling 20 "$blah =param(('whatever'));"?

I appericate anything anyone can provide for me, thanks!

Replies are listed 'Best First'.
Re: Form Validation Ideas
by Cody Pendant (Prior) on Feb 25, 2008 at 02:59 UTC
    You want a module like Data::FormValidator.

    And you don't need to transfer the params to variables before you test them, if that's what you meant by this:

    $blah = param('whatever');
    to validate your params, you can just do
    if( param('whatever') ne '' ){ # error code here }


    Nobody says perl looks like line-noise any more
    kids today don't know what line-noise IS ...
Re: Form Validation Ideas
by kyle (Abbot) on Feb 25, 2008 at 01:57 UTC
Re: Form Validation Ideas
by hesco (Deacon) on Feb 25, 2008 at 03:19 UTC
    I do use it, and kyle is right. CGI::FormBuilder is exactly what you want. I have found that CGI::FormBuilder greatly accelerates for me the development of cgi web forms. It was so tempting to rush through the process and deploy something without the validation in place, before I discovered this module. Now with very little overhead I can usually produce useful and working webforms in a couple of hours. It even provides javascript elements which prevalidate form input on the browser side, without making me learn javascript.

    Check it out on CPAN.

    -- Hugh

    if( $lal && $lol ) { $life++; }
      Hesco,
      I'm trying to use CGI::FormBuilder, but I can't get the forms in the centre of the page, they are always left justified. Can you tell me how?

      Thx
      Chris

Re: Form Validation Ideas
by andreas1234567 (Vicar) on Feb 25, 2008 at 07:36 UTC
Re: Form Validation Ideas
by spatterson (Pilgrim) on Feb 25, 2008 at 17:36 UTC
    Simple check that every parameter is alphanumeric
    my %params = CGI::Vars; foreach my $p (keys %params) { unless ($params{$p} =~ /[[:alpha:]]/) { push @error_params, $p } } print "these params failed: ", join(", ", @error_params);
    If you know how many parameters to expect, you could count the number of parameters which were set:
    my %params = CGI::Vars; if (scalar(keys %params) < $no_expected) { print "error, you missed something."; }

    just another cpan module author
Re: Form Validation Ideas
by zby (Vicar) on Feb 25, 2008 at 17:46 UTC
    I use HTML::FormFu. Not really simple - but, I would say, comprehensible.
Re: Form Validation Ideas
by Trihedralguy (Pilgrim) on Feb 25, 2008 at 13:21 UTC
    I'm not quite sure that I know that FormBuilder is the option I'm looking for. I already know a bit of Javascript and I have all the javascript validation handled. Now its time for perl. Are there any solutions that do not deal with javascript validation, just perlside?
      For the part of your question about quickly testing a lot of params to see if they hava a value, you could do something like:
      my %missing; for my $item (qw{fname sname age street city zip etc}){ $missing{$item} = param($item) ? 0 : 1; }
      While you're at it you could do some basic cleansing inside the loop, and attack specific fields afterwards.
      hth

        That works until you have a parameter where "0" is a valid value.

        my @param_names = $q->param(); my %present_params; @present_params{ @param_names } = (1) x @param_names;

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.