[html-formfu] DBIC::Validation::FormFu RFC & Element::Date patch

Jonas Alves jonas.alves at gmail.com
Thu Oct 18 18:55:53 GMT 2007


On 18/10/2007, Jonas Alves <jonas.alves at gmail.com> wrote:
>
>
>
> On 18/10/2007, Zbigniew Lukasiak <zzbbyy at gmail.com> wrote:
> >
> > Hi Jonas,
> >
> > How about moving the the validation from the DB layer code into some
> > separate class in HTML::FormFu hierarchy?  I hate it when I have to
> > add things to the DB model that belong to the form layer this makes
> > the model coupled to the form - and I would rather have a clean model
> > independend on what form processing layer I use.
> >
> > Cheers,
> > Zbyszek
> >
> >
> Hi Zby,
> I don't know if I got it, but if you don't want the model and the form
> layer coupled then you don't need to do anything. Just validate with Form=
Fu
> or your preferred validator first and then insert in the database like you
> usually do.
> On the other hand if you don't bother that they are coupled, you just need
> to call create or update on your resultset and the data is validated befo=
re
> being inserted. An exception with the form will be thrown if the data is =
not
> valid.
>
> Cheers,
> --
> Jonas
>

I like this approach because I almost don't need to write code in my
controllers. I just need to have the DBIC calls there.
I have an action class like this:

package MyApp::Action::CheckError;
use strict;
use warnings;
use base 'Catalyst::Action';
use Scalar::Util qw/blessed/;

sub execute {
    my $self =3D shift;
    my ($controller, $c ) =3D @_;

    eval {
        $self->next::method( @_ );
    };
    if (blessed $@ && $@->isa('HTML::FormFu')) {
        my $form =3D $@;
        $c->error(0);
        $c->stash(form =3D> $form);
        my $errors =3D { map { $_->name =3D> $_->message } @{$form->get_err=
ors}
};
        $c->res->status('400'); # Bad Request
        $c->stash(rest =3D> $errors);
    }
}

1;

So in my controller I just have:

sub create : MyAction('CheckError') {
    my ( $self, $c ) =3D @_;
    $c->model('DBIC::User')->create($c->req->params);
    $c->redirect(...);
}

All the code is clean and reusable. :)

Cheers
-- =

Jonas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/html-formfu/attachments/20071018/05=
824447/attachment.htm


More information about the HTML-FormFu mailing list