Catalyst-Plugin-Form-Processor
view release on metacpan or search on metacpan
lib/Catalyst/Plugin/Form/Processor.pm view on Meta::CPAN
# Single argument to Form::Processor->new means it's an item id or object.
# Hash references must be turned into lists.
my %args;
if ( defined $args_ref ) {
if ( ref $args_ref eq 'HASH' ) {
%args = %{$args_ref};
}
elsif ( Scalar::Util::blessed( $args_ref ) ) {
%args = (
item => $args_ref,
item_id => $args_ref->id,
);
}
else {
%args = ( item_id => $args_ref );
}
}
# Set schema name -- mostly for DBIC
if ( $package->can( 'schema' ) ) {
unless ( exists $args{schema} ) {
my $schema_name = $c->config->{form}{model_name}
|| $c->stash->{model_name};
$args{schema} = $c->model( $schema_name )->schema
if defined $schema_name;
}
}
$args{user_data}{context} = $c;
# Since $c holds a reference to the form and the form holds
# a reference to the context must weaken.
Scalar::Util::weaken( $args{user_data}{context} );
return $c->stash->{form} = $package->new( %args );
} ## end sub form
sub validate_form {
my ( $c, @rest ) = @_;
my $form = $c->form( @rest );
return
$c->form_posted
&& $form->validate( $c->req->parameters );
}
sub update_from_form {
my ( $c, @rest ) = @_;
my $form = $c->form( @rest ) || return;
return
$c->form_posted
&& $form->update_from_form( $c->req->parameters );
}
sub form_posted {
my $c = shift;
return $c->req->method eq 'POST' || $c->req->method eq 'PUT';
}
# Used to override finalize, but that's not called in a redirect.
# TODO: add support for multiple forms on a page (and multiple forms
# in the stash).
#
# And better, simply remove this.
before 'finalize' => sub {
my $c = shift;
my $form = $c->stash->{form} || return;
# Disabled in configuration
return if $c->config->{form}{no_fillin};
return if $c->res->status != 200;
my $params = $form->fif;
return unless ref $params && %{$params};
return unless defined $c->response->{body};
$c->log->debug( 'Filling in form with HTML::FillInForm' ) if $c->debug;
# Run FillInForm
$c->response->output(
HTML::FillInForm->new->fill(
scalarref => \$c->response->{body},
fdat => $params,
) );
return;
};
after 'setup_finalize' => sub {
my $c = shift;
( run in 1.124 second using v1.01-cache-2.11-cpan-39bf76dae61 )