Permalink
Browse files

Patch issue where Select field values bled into each other following …

…a freeze/thaw of the form.
  • Loading branch information...
1 parent f9b24c0 commit 425abae7f75ea55e80f6af464b57e3c8f5bc9897 @nheinric committed Mar 1, 2012
Showing with 86 additions and 5 deletions.
  1. +1 −0 Makefile.PL
  2. +4 −5 lib/Form/Sensible/Field.pm
  3. +81 −0 t/fields/select_values_dont_bleed_after_freeze_thaw.t
View
@@ -18,6 +18,7 @@ requires 'File::ShareDir';
requires 'FindBin';
test_requires 'Test::More' => '0.42';
#test_requires 'HTML::Tree' => '3.23';
+test_requires 'Cache::FastMmap';
install_share 'share';
no_index 'directory' => 'examples';
# Perhaps we will use this to create command-line based form generators... another day.
@@ -71,18 +71,17 @@ has 'value_delegate' => (
required => 1,
default => sub {
my $self = shift;
- my $value = $self->default_value;
+ $self->{_value} = $self->default_value;
my $sub = sub {
my $caller = shift;
-
if ($#_ > -1) {
if (ref($_[0]) eq 'ARRAY' && !($caller->accepts_multiple)) {
- $value = $_[0]->[0];
+ $caller->{_value} = $_[0]->[0];
} else {
- $value = $_[0];
+ $caller->{_value} = $_[0];
}
}
- return $value;
+ return $caller->{_value};
};
return FSConnector($sub);
},
@@ -0,0 +1,81 @@
+use Test::More;
+use Cache::FastMmap;
+
+use Form::Sensible::Form;
+use Form::Sensible::Field::Select;
+
+## Build a form with two select fields
+my $form = Form::Sensible::Form->new( name => 'form' );
+
+my $select1 = Form::Sensible::Field::Select->new(
+ name => 'select1'
+ , accepts_multiple => 0
+ , validation => { required => 1 }
+ );
+$select1->add_option( 'select1-option1', 'Something' );
+$select1->add_option( 'select1-option2', 'Something completely different' );
+$select1->set_selection( 'option1' );
+
+my $select2 = Form::Sensible::Field::Select->new(
+ name => 'select2'
+ , accepts_multiple => 0
+ , validation => { required => 1 }
+ );
+$select2->add_option( 'select2-option1', 'Spam' );
+$select2->add_option( 'select2-option2', 'Spam eggs sausage and spam' );
+$select2->set_selection( 'option1' );
+
+$form->add_field( $select1 );
+$form->add_field( $select2 );
+
+## Cache the form
+my $cache = Cache::FastMmap->new( unlink_on_exit => 1 );
+# Silence "used only once" warning
+$Storable::Deparse = $Storable::Eval = 1;
+$Storable::Deparse = $Storable::Eval = 1;
+$cache->set( "the-form", $form );
+
+note "Test form, pre-serialization";
+test_validation( $form );
+
+note "Test form, post-serialization";
+my $form2 = $cache->get( "the-form" );
+test_validation( $form2 );
+
+
+done_testing;
+
+################################################################################
+sub test_validation {
+ my $form = shift;
+
+ $form->set_values({
+ select1 => "select1-option1"
+ , select2 => "select2-option1"
+ });
+ ok( $form->validate->is_valid, "Good data validated 1" )
+ or diag explain [ $form->validate, $form->get_all_values ];
+
+ $form->set_values({
+ select1 => "select1-option2"
+ , select2 => "select2-option2"
+ });
+ ok( $form->validate->is_valid, "Good data validated 2" )
+ or diag explain [ $form->validate, $form->get_all_values ];
+
+ $form->set_values({
+ select1 => "he's bleedin' demised"
+ , select2 => "select2-option1"
+ });
+ ok( ! $form->validate->is_valid, "Bad data failed 1" )
+ or diag explain $form->validate;
+
+ $form->set_values({
+ select1 => "select1-option1"
+ , select2 => "beautiful plumage"
+ });
+ ok( ! $form->validate->is_valid, "Bad data failed 2" )
+ or diag explain $form->validate;
+}
+
+

0 comments on commit 425abae

Please sign in to comment.