sub edit_form { my $self = shift; my $q = $self->query(); # load the HTML::Template object my $template = $self->load_tmpl('FormTemplate.tmpl') || die "Can't find template FormTemplate.tmpl"; if ($q->param('first_name')) { # The user has filled in the form, so we # check the values and update the database # if everything is OK my %form_data = $q->Vars; my %form_profile = ( required => [qw(first_name last_name)], optional => [qw(email)], constraints => { email => 'email', }, filters => [ 'trim' ], ); my ($valid, $missing, $invalid, $unknown) = Data::FormValidator->validate(\%form_data, \%form_profile); if (@$missing || @$invalid) { # There were problems with the form my @errors; push @errors, map { { 'missing_'.$_ => 1 } } @$missing; push @errors, map { { 'invalid_'.$_ => 1 } } @$invalid; $template->param(errors => \@errors); } else { # Everything looks good, so update the database eval { ### Do some database stuff here My::DB::User->create($valid); }; if ($@) { $template->param(errors => [ { failed_on_update => 1} ]); } else { # The database was updated successfully # so display a success page $template->param(database_updated => 1); } } } return $self->fillinform(\$template->output()); } sub fillinform { my $self = shift; my $html = shift; # ref to string of HTML my $fif = new HTML::FillInForm; return $fif->fill(scalarref => $html, fobject => $self->query); }