Skip to content

Instantly share code, notes, and snippets.

@dhoss
Created November 10, 2009 05:48
Show Gist options
  • Save dhoss/230652 to your computer and use it in GitHub Desktop.
Save dhoss/230652 to your computer and use it in GitHub Desktop.
package Fnargh::Form::Auto;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler::Model::DBIC';
with 'HTML::FormHandler::Render::Simple';
has_field 'submit' => (
type => 'Submit',
label => shift, # WTF, not working
);
sub get_fields {
my $self = shift;
my $schema = $self->schema;
my $table = $self->item_class;
my @columns = $schema->source($table)->columns;
# do something here to determine what's optional/required
return \@columns;
}
sub field_list {
my $self = shift;
return {
auto_required => $self->get_fields,
};
}
no HTML::FormHandler::Moose;
1;
package Fnargh::Controller::CMS;
use Moose;
BEGIN { extends 'Catalyst::Controller' };
use Fnargh::Form::Auto;
#...
sub create : Chained('blog') PathPart('new') Args(0) {
my ($self, $c) = @_;
my $blog_id;
my $form = Fnargh::Form::Auto->new(
item_class => 'Entity',
item_id => 1,
schema => $c->model('Database')->schema,
);
## here goes my attempt at auto generating a form from a DBIC schema
$c->stash(
template => 'cms/new.tt',
form => $form,
);
return unless $form->process(
item_id => $blog_id,
params => $c->req->parameters,
schema => $c->model('Database')->schema,
);
}
use strict;
use warnings;
use Test::More;
use FindBin;
use lib "$FindBin::Bin/../lib";
use Fnargh::Form::Auto;
use Fnargh::Schema;
my $schema = Fnargh::Schema->connect("dbi:SQLite:t/test.db");
$schema->deploy;
ok ($schema, "got schema");
my $form = Fnargh::Form::Auto->new( item_id => 1, item_class => 'Role', schema => $schema );
ok($form, "got form");
is_deeply($form->field_list, { auto_required => [qw/ role_id role /] }, "Got field list");
unlink 't/test.db';
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment