Skip to content

Commit

Permalink
add no_update flag to skip update_model
Browse files Browse the repository at this point in the history
  • Loading branch information
gshank committed Mar 6, 2012
1 parent e79dbe0 commit cb6c6e9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -13,6 +13,7 @@
git://github.com/gshank/html-formhandler_pre-0.40_compat.git
README at: https://github.com/gshank/html-formhandler_pre-0.40_compat/blob/master/README

Add 'no_update' flag.
Remove 'deflate_to' flag; provide new inflation/deflation methods.
see HTML::FormHandler::Manual::InflationDeflation
New 'build_id_method' to provide different builder method for field IDs.
Expand Down
11 changes: 9 additions & 2 deletions lib/HTML/FormHandler.pm
Expand Up @@ -262,6 +262,11 @@ for using to fill in the form from C<< $form->fif >>.
A hash of inflated values (that would be used to update the database for
a database form) can be retrieved with C<< $form->value >>.
If you don't want to update the database on this process call, you can
set the 'no_update' flag:
$form->process( item => $book, params => $params, no_update => 1 );
=head3 params
Parameters are passed in when you call 'process'.
Expand Down Expand Up @@ -823,6 +828,7 @@ has 'do_form_wrapper' => ( is => 'rw', builder => 'build_do_form_wrapper' );
sub build_do_form_wrapper { 0 }
has 'no_widgets' => ( is => 'ro', isa => 'Bool' );
has 'no_preload' => ( is => 'ro', isa => 'Bool' );
has 'no_update' => ( is => 'rw', isa => 'Bool', clearer => 'clear_no_update' );
has 'active' => (
is => 'rw',
traits => ['Array'],
Expand Down Expand Up @@ -1139,7 +1145,7 @@ sub process {
$self->clear if $self->processed;
$self->setup_form(@_);
$self->validate_form if $self->has_params;
$self->update_model if $self->validated;
$self->update_model if ( $self->validated && !$self->no_update );
$self->after_update_model if $self->validated;
$self->dump_fields if $self->verbose;
$self->processed(1);
Expand All @@ -1150,7 +1156,7 @@ sub run {
my $self = shift;
$self->setup_form(@_);
$self->validate_form if $self->has_params;
$self->update_model if $self->validated;
$self->update_model if ( $self->validated && !$self->no_update );;
$self->after_update_model if $self->validated;
my $result = $self->result;
$self->clear;
Expand All @@ -1174,6 +1180,7 @@ sub clear {
$self->clear_result;
$self->clear_use_defaults_over_obj;
$self->clear_use_init_obj_over_item;
$self->clear_no_update;
}

sub values { shift->value }
Expand Down
33 changes: 33 additions & 0 deletions t/form_setup/no_update.t
@@ -0,0 +1,33 @@
use strict;
use warnings;
use Test::More;

{
package MyApp::Form::Test;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';

has_field 'foo';
has_field 'bar';

sub update_model {
my $self = shift;
my $values = $self->values;
$values->{foo} = 'updated';
$self->_set_value($values);
}
}

my $form = MyApp::Form::Test->new;
my $params = { foo => 'pfoo', bar => 'pbar' };
$form->process( params => $params );
is_deeply( $form->values, { foo => 'updated', bar => 'pbar' },
'values processed by update_model' );
$form->process( params => $params, no_update => 1 );
is_deeply( $form->values, $params,
'values not processed by update_model' );
$form->process( params => $params );
is_deeply( $form->values, { foo => 'updated', bar => 'pbar' },
'values processed by update_model' );

done_testing;

0 comments on commit cb6c6e9

Please sign in to comment.