#!/usr/bin/perl use strict; use MooseX::Declare; use KiokuDB; use Continuity; # Base class for all forms class Form extends HTML::FormHandler { has owner => (is => 'rw'); our $form_count = 0; has '+name' => ( default => sub { return "f-" . $form_count++ } ); has '+html_prefix' => ( default => 1 ); method update { if($self->validated) { my $v = $self->value; foreach my $field (keys %$v) { eval { # ignore validation issues :) $self->owner->$field($v->{$field}) if $self->owner->can($field); } } } } } # Though I'm roughly OK with listing inputs for the form, it annoys me to have # to specify the types. That, at at least a basic level, should be readable # from the fields to which this form is connected. class BasicPersonForm extends Form with HTML::FormHandler::Render::Table { has '+field_list' => ( default => sub {[ name => 'Text', birthday => 'Text', save => { type => 'Submit', value => 'Save' }, ]}); } class Person { use MooseX::Types::DateTimeX qw( DateTime ); has name => ( is => 'rw', default => sub { '' } ); has birthday => ( is => 'rw', isa => DateTime, coerce => 1 ); method basic_form { BasicPersonForm->new( owner => $self, init_object => $self ) } } sub main { my $r = shift; my $db = KiokuDB->connect( 'dbi:SQLite:phone.db', create => 1 ); my $scope = $db->new_scope; # This is a new person, pending some data my $new_person = Person->new; while(1) { my @people = $db->grep(sub { ref($_) eq 'Person' })->all; my @forms = map { $_->basic_form } @people; foreach my $form (@forms) { $r->print($form->render); } $r->print("