[Dancer-users] Forms with dancer

David Precious davidp at preshweb.co.uk
Sat Mar 12 19:52:57 CET 2011


On Sat, 2011-03-12 at 13:18 +0100, Erik Colson wrote:
> 
> I'm about to write a company app with dancer. I've been reading a lot
> about Formhandler, Formbuilder and Formfu.
> There are pros and cons for everyone of these :/
> So, my question is a double popularity poll:
> 
> . which form generator are you using ? 

I'm using CGI::FormBuilder for Dancer::Plugin::SimpleCRUD, and it seems
quite reasonable.  Unfortunately, it hasn't been updated since 2007, so
it doesn't look to be very actively developed, and has 20 untouched bugs
on RT.   I'm thinking of moving away from it.

I've also used HTML::FormFu in a Dancer project for a client, and found
it to do the job; I didn't do anything too clever with it, though, but
it did everything I asked of it capably.

A little example of using it in Dancer:

any '/companies/add' => sub {
    my $form = HTML::FormFu->new;
    $form->load_config_file('forms/add-edit-company.yaml');
    my $params_hashref = params;
    $form->process($params_hashref);
    if ($form->submitted_and_valid) {
        if (my $result = add_or_edit_company($params_hashref)) {
            redirect '/companies';
        } else {
            template add_edit_company => { 
                mode => 'Add',
                error => 'Failed to store new company details'
            };
        }       
    } else { 
        template add_edit_company => { mode => 'Add', form => $form };
    }        
};

As you can see, I'm passing the form to the template, and letting
HTML::FormFu take care of the validation of the parameters given to it.

HTML::FormFu looks to be more actively developed, but its encouragement
of declaring forms and their fields in YAMLwill either please or disgust
you, depending on your point of view.  You can create forms and add
fields to them by code, though, if you don't want your form declarations
factored out into YAML files.

It also declares itself beta software in the documentation, with
possible API changes before it hits 1.0.

Hope that may be of some use :)

Cheers

Dave P


-- 
David Precious  ("bigpresh")
http://www.preshweb.co.uk/

   "Programming is like sex. One mistake and you have to support
   it for the rest of your life". (Michael Sinz)




More information about the Dancer-users mailing list