[html-formfu] How to best pass the resultset object to DBIC validator callbacks?

Carl Franks fireartist at gmail.com
Fri Mar 6 12:29:04 GMT 2009


2009/3/5 Dan Dascalescu <ddascalescu+formfu at gmail.com>:

[snip]

> 1. Is this the best way to achieve the goal of doing some DB-related
> validation in a callback? If so, I'll be happy to document this in the
> Validator::Callback.pm, which is painfully empty at the moment.
>
> 2. Is it best practice to put the callback information in the form
> config file, or to modify the form in the controller code?

To be honest, I don't think Callbacks are a very maintainable
approach, and I never use them.

For me, a normal application's directory would contain:
    lib/HTML/FormFu/Constraint/MyApp/
    lib/HTML/FormFu/Validator/MyApp/
    lib/HTML/FormFu/Plugin/MyApp/
    etc.

So the config file would just need something like:
    validator: 'MyApp::UniqueURL'

And the class would be something like this:

    package HTML::FormFu::Validator::MyApp::UniqueURL;
    use strict;
    use warnings;
    use base 'HTML::FormFu::Validator';

    sub validate_value {
        my ( $self, $value, $params ) = @_;

        my $c = $self->form->stash->{context};

        return $c->model('DBIC::Urls')->validator_unique_url($value);
    }

    1;

It's not much setup once you've got your first to copy/paste from ++
it keeps the code out of the controller ++
and it's very reusable ++

Hope this helps!
Carl



More information about the HTML-FormFu mailing list