Skip to content

Commit

Permalink
fix for deflation of undef value, from ether's pull request 122
Browse files Browse the repository at this point in the history
  • Loading branch information
gshank committed Oct 15, 2016
1 parent 8f0d9da commit 631f13c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/HTML/FormHandler/Field.pm
Expand Up @@ -676,13 +676,15 @@ sub fif {
{
return defined $lresult->input ? $lresult->input : '';
}
if ( defined $lresult->value ) {
if ( $lresult->has_value ) {
my $value;
if( $self->_can_deflate ) {
return $self->_apply_deflation($lresult->value);
$value = $self->_apply_deflation($lresult->value);
}
else {
return $lresult->value;
$value = $lresult->value;
}
return ( defined $value ? $value : '' );
}
elsif ( defined $self->value ) {
# this is because checkboxes and submit buttons have their own 'value'
Expand Down
35 changes: 35 additions & 0 deletions t/infl_defl/undef_deflation.t
@@ -0,0 +1,35 @@
use strict;
use warnings;
use Test::More;

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

has_field 'foo' => ( deflate_method => \&deflate_foo );
has_field 'bar';

sub deflate_foo {
my ( $self, $value ) = @_;
if ( ! defined $value ) {
$value = 'deflated';
}
return $value;
}

}

my $form = MyApp::Form::Test->new;
ok( $form );

my $init_obj = {
foo => undef,
bar => 'somebar',
};

$form->process( init_object => $init_obj, params => {} );
my $fif = $form->fif;
is_deeply( $fif, { foo => 'deflated', bar => 'somebar' }, 'undef value was deflated' );

done_testing;

0 comments on commit 631f13c

Please sign in to comment.