TF-1 Technology Blog

Posted: May 2nd, 2012 by Alex Pavlovic

Ever needed to simply validate form data in perl Catalyst framework? Well it turns out to be simpler then you thought. Since Data::FormValidator and Catalyst::Plugin::FormValidator are no longer recommended to be used ( as per Catalyst::Plugin::FormValidator docs ), we will use Catalyst::Plugin::FormValidator::Simple which wraps FormValidator::Simple functionality quite well. If you are needing form construction, rendering and more, you can look at HTML::FormHandler instead.

FormValidator::Simple as the name implies is simple and functional form validation module. It does form validation and nothing more. It has a catalyst plugin which allows us to easily integrate it it into our web application. Here is some code below to illustrate how to use it with custom constraint ( via FormValidator::Simple::Plugin::CustomConstraint ) to validates captcha sequence on the website contact form.

First we load support for captcha, session and formvalidator in MyApp.pm as per below.


use Catalyst qw/
  -Debug
  Static::Simple
  ConfigLoader
  Session
  Session::Store::FastMmap
  Session::State::Cookie
  FormValidator::Simple
  Captcha
/;

__PACKAGE__->config(
  name => 'MyApp',
  validator => {
    plugins => [ 'CustomConstraint', 'Trim' ]
  },
  'Plugin::Captcha' => {
    session_name => 'captcha_session',
    new => {
      width => 40,
      height => 40,
      lines => 1,
      gd_font => 'Giant',
      thickness => 0,
      frame => 0,
      rnd_data => [0 .. 9]
    },
    create => [ 'normal', 'box', '#333333', '#f4f4f4' ],
    particle => [ 40 ],
  },
  'Plugin::Session' => {
    expires => 8600
  },
);

Next we need to tell our root controller to handle the form processing as per below.


sub process : Local {

  #ensure form fields are required ( NOT_BLANK )
  #and that they are always whitespace trimmed ( TRIM )
  #then use custom constraint via ( CUSTOM_CONSTRAINT )

  my $result = $c->form(
    name => [ qw/TRIM NOT_BLANK/ ],
    email  => [ qw/TRIM NOT_BLANK EMAIL_LOOSE/ ],
    message => [ qw/TRIM NOT_BLANK/ ],
    validate => [
      'TRIM',
      'NOT_BLANK', [
        'CUSTOM_CONSTRAINT',
        sub {
          my ( $validate, $args ) = @_;
          my $result = $c->validate_captcha( $validate );
          return $result;
        }
      ]
    ]
  );

  if ( $result->has_error ) {
    #do something in case of error
  } else {
    #process the form data as there is no errors
  }

}

Lastly we need a template to ensure form rendering. Here is example of one. It will show a single form input, feel free to expand on it to include more inputs as per input specification above.

[% IF c.form.missing('name') %] this field is required [% ELSIF c.form.invalid('name') %] this field is invalid [% END %]

This shows how to validate form inputs using custom constraints and FormValidator::Simple via catalyst plugin. This example completely separates templating from form validation, so you could always hand off the template to your designer for further layout modifications and so on, without them ever needing to work with perl code.

Comments are closed.

several months ago: TF-1 marks the successful engagement of 500 #Drupal projects, from which over 100 are Drupal #Commerce and Drupal #Ubercart specific. Reply Retweet Favourite
several months ago: At the new office, TF-1 Team @ work http://t.co/B0p9eR23 Reply Retweet Favourite
several months ago: We just started listing our Thrinacia pledgers. Check it out at https://t.co/ZLFfAfbZ Reply Retweet Favourite
several months ago: Taskforce-1 is now a seed partner of NCFA. http://t.co/fvkNHT1R Reply Retweet Favourite
several months ago: Article on Thrinacia, our open source SaaS crowdfunding platform just got published on NCFA Canada at http://t.co/xuk2DTZv Reply Retweet Favourite
several months ago: Several new pledges received for Thrinacia initiative today, thank you everyone and lets raise enough to complete Milestone 2. Reply Retweet Favourite
Contact Us




TaskForce-1 Industries Ltd.
#110 - 19162 - 22'nd Av.
Surrey, B.C - Canada
V3S 3S6 View Map
Toll Free Phone: 1.888.630.5733