Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: RFC: Class::CGI

by Ovid (Cardinal)
on Apr 08, 2006 at 01:42 UTC ( #542008=note: print w/ replies, xml ) Need Help??


in reply to Re: RFC: Class::CGI
in thread RFC: Class::CGI

I like Data::FormValidator, but I was never happy with the syntax and never used it extensively. Class::CGI basically treats form data as a collection of objects which can be fetched. Further, the various objects which different forms contain can be mixed and matched at will, with ease. Because a lot of the validation stuff is encapsulated in the handler classes responsible for a given object, the programmer's main code is focused on the business concerns.

In short, the code can be as simple as this:

use Class::CGI handlers => { customer => 'Class::CGI::Customer', date => 'My::Date::Handler', }; my $cgi = Class::CGI->new; eval { my $cust = $cgi->param('customer'); my $date = $cgi->param('date'); }; handle_possible_error($@); my $name = $cust->name; # look ma, objects! my $year = $date->year;

There's a bunch of work which gets abstracted away instead of cluttering the code in a large data structure with odd parameters the end user has to figure out.

That's not to say anything is wrong with the Data::FormValidator approach. It's a difference in approach. With DFV, the validation is centralized in the code doing the validating and it validates all of the data. In the Class::CGI approach, each handler is responsible for validating its own data.

Those are just from my rough impressions of DFV, though. If you can show me how clean the top-level code can be, I'd love to see it.

Cheers,
Ovid

New address of my CGI Course.


Comment on Re^2: RFC: Class::CGI
Download Code
Re^3: RFC: Class::CGI
by rhesa (Vicar) on Apr 08, 2006 at 02:27 UTC
    Well, I'm not a veteran DFV user, so I may be stumbling here. But something like this:
    use Data::FormValidator; use Data::FormValidator::Constraints::DateTime qw(to_datetime); use CGI; use My::Customer; my $results = Data::FormValidator->check( CGI->new, { constraints => { customer => My::Customer->constraint, date => to_datetime('%D'), } } ); if( $results->success ) { my $date = $results->valid('date'); my $customer = $results->valid('customer'); $date->year; $customer->address->zipcode; }
    There's no need to inline the profile like that, of course.

    &My::Customer::constraint might look like this:

    package My::Customer; use base 'My::Class::DBI'; sub constraint { my $pkg = shift; return sub { my $value = shift; return unless $value =~ /^(\d+)$/; return $pkg->retrieve($value); }; }
    But, that's all just guesswork on my part (and completely untested). It does show tremendous potential, that I really should try to incorporate in my own work ;)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://542008]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (14)
As of 2015-07-01 21:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (22 votes), past polls