[html-formfu] Perl DBI with FormFu
Carl Franks
fireartist at gmail.com
Tue Mar 17 09:59:23 GMT 2009
2009/3/17 Manu Kaul <manu.kaul at gmail.com>:
> Hi,
> I have just started getting into Catalyst for a new project where we have to
> communicate with a Model Class written in Perl DBI which is optimized and
> uses stored procedures to do most of the work for us. I was reading through
> the FormFu documentation on CPAN to understand how I could take advantage of
> FormFu's validation features etc and leverage off the existing Perl DBI
> Model class. I found no documenation on whether it was possible for FormFu
> to talk to a Perl DBI class.... Is this even possible?
> I noticed that all the communication to the backend was through Model::DBIC
> and Resultsets. If you could provide me with some examples of how I could
> use FormFu for validation purposes and then use my existing model class to
> do the rest of the work that would be extremely useful.
> Thanks in advance and look forward to a reply.
Hi Manu,
If you create custom constraints + validators, then can access your
Catalyst context, and so access your model.
If you're using Catalyst-Controller-HTML-FormFu, it'll automatically
copy the Cat context into
$form->stash->{context};
As an example of how to use it, here's a (slightly edited) part of the
new Validator docs:
=head1 BEST PRACTICES
Try to avoid using callbacks if possible. Below is a more maintainable
and reusable approach, which also keeps the code out of the controller.
A normal application's directory would contain:
lib/HTML/FormFu/Constraint/MyApp/
lib/HTML/FormFu/Validator/MyApp/
lib/HTML/FormFu/Plugin/MyApp/
etc.
Then, the form config file would just need:
validator: 'MyApp::SomeValidator'
And the class would be something like this:
package HTML::FormFu::Validator::MyApp::SomeValidator;
use strict;
use warnings;
use base 'HTML::FormFu::Validator';
sub validate_value {
my ( $self, $value, $params ) = @_;
my $c = $self->form->stash->{context};
# do something here with $c->model('MyModel');
# return 1 if valid
# fail
return 0;
}
1;
Hope this helps,
Carl
More information about the HTML-FormFu
mailing list