[Catalyst] Where to put FormValidator profiles?
Bill Moseley
moseley at hank.org
Thu Aug 11 20:32:47 CEST 2005
On Thu, Aug 11, 2005 at 01:56:56PM -0400, Jesse Sheidlower wrote:
> On Thu, Aug 11, 2005 at 07:13:29AM -0700, Bill Moseley wrote:
> >
> > Also, where's a good place to put profiles? I suppose a file might be
> > good, set in the application config. But I'm curious how others
> > organize this.
>
> I keep, or generate (as necessary) my profiles in the Model class of
> the main table I'm dealing with. I have a get_profile() method that
> either returns a canned profile, or dynamically generates one,
> depending on what I need to do. Then in my Controller I just say
> my $profile = MyApp::M::Library->get_profile or whatever.
That seems like a good setup, as the profile is available outside
Catalyst.
On the other hand, I'm not sure how well that works when a form is
submitted that will end up updating many different tables. There's
also somewhat of a difference between validating user input than
checking constraints on a model class before doing an update.
I also have profiles that are the same but used for more than one
table. For example, to validate sort order, reverse sort, page
number, and so on on a table display.
My original plan was to create a Plugin to replace the FormValidator
plugin that calls DVF::new() instead of DFV::check() in the prepare
method so I could pre-load named profiles.
But, I need to know $class and $primary_column inside the profile at run
time, so I don't think there's a way to define this as a named profile
in a call to Data::FormValidator->new().
Here's what I'm doing in a generic table list action:
my $form = $c->form(
optional => [qw/ page rows order rev /],
defaults => {
page => 1,
order => $primary_column,
rows => ($c->config->{rows} || 10),
},
constraints => {
order => sub { $class->can( shift ) },
},
filters => ['trim'],
field_filters => {
page => ['digit'],
rows => [
'digit',
sub {
my $value = shift;
return $row_default unless defined $value
and $value <= $row_max;
return $value;
},
],
},
);
--
Bill Moseley
moseley at hank.org
More information about the Catalyst
mailing list