Skip to content

Commit

Permalink
handle disabled compound fields
Browse files Browse the repository at this point in the history
  • Loading branch information
gshank committed Apr 24, 2012
1 parent 3bfd895 commit 6e8cd29
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 17 deletions.
1 change: 1 addition & 0 deletions lib/HTML/FormHandler.pm
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,7 @@ L<HTML::FormHandler::Manual::Rendering>.

# for consistency in api with field nodes
sub form { shift }
sub is_form { 1 }
sub has_form { 1 }

# Moose attributes
Expand Down
19 changes: 8 additions & 11 deletions lib/HTML/FormHandler/Field.pm
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@ has 'form' => (
weak_ref => 1,
predicate => 'has_form',
);
sub is_form { 0 }
has 'html_name' => (
isa => 'Str',
is => 'rw',
Expand Down Expand Up @@ -1244,7 +1245,10 @@ sub BUILD {
sub _result_from_fields {
my ( $self, $result ) = @_;

if ( my @values = $self->get_default_value ) {
if ( $self->disabled && $self->has_init_value ) {
$result->_set_value($self->init_value);
}
elsif ( my @values = $self->get_default_value ) {
if ( $self->has_inflate_default_method ) {
@values = $self->inflate_default(@values);
}
Expand All @@ -1264,16 +1268,9 @@ sub _result_from_input {
$result->_set_input($input);
}
elsif ( $self->disabled ) {
# Disabled fields are not submitted, and so have no input
# but we need to have them in results.
if ( $self->has_init_value ) {
$result->_set_input( $self->init_value );
}
else {
# This really ought to come from _result_from_object, but there's
# no way to get there from here.
return $self->_result_from_fields( $result );
}
# This really ought to come from _result_from_object, but there's
# no way to get there from here.
return $self->_result_from_fields( $result );
}
elsif ( $self->has_input_without_param ) {
$result->_set_input( $self->input_without_param );
Expand Down
7 changes: 7 additions & 0 deletions lib/HTML/FormHandler/InitResult.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Internal role for initializing the result objects.
sub _result_from_fields {
my ( $self, $self_result ) = @_;

# defaults for compounds, etc.
if ( my @values = $self->get_default_value ) {
my $value = @values > 1 ? \@values : shift @values;
if( ref $value eq 'HASH' || blessed $value ) {
Expand All @@ -23,15 +24,21 @@ sub _result_from_fields {
$self->init_value($value) if defined $value;
$self_result->_set_value($value) if defined $value;
}
my $my_value;
for my $field ( $self->sorted_fields ) {
next if ($field->inactive && !$field->_active);
my $result = HTML::FormHandler::Field::Result->new(
name => $field->name,
parent => $self_result
);
$result = $field->_result_from_fields($result);
$my_value->{ $field->name } = $result->value if $result->has_value;
$self_result->add_result($result) if $result;
}
# setting value here to handle disabled compound fields, where we want to
# preserve the 'value' because the fields aren't submitted...except for the
# form. Not sure it's the best idea to skip for form, but it maintains previous behavior
$self_result->_set_value($my_value) if ( keys %$my_value );
$self->_set_result($self_result);
$self_result->_set_field_def($self) if $self->DOES('HTML::FormHandler::Field');
return $self_result;
Expand Down
2 changes: 2 additions & 0 deletions t/basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ my $init_object = {
$form = My::Form->new( init_object => $init_object, no_preload => 0 );
is( $form->field('optname')->value, 'Over Again', 'value with int_obj' );
$form->process( params => {} );
my $value = $form->value;
$DB::single=1;
ok( !$form->validated, 'form validated' );

# it's not crystal clear what the behavior should be here, but I think
Expand Down
26 changes: 21 additions & 5 deletions t/field_setup/disabled.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,34 @@ use Data::Dumper;

has_field 'foo' => ( type => 'Select', disabled => 1 );
has_field 'bar';
has_field 'user' => ( type => 'Compound', required => 1 );
has_field 'user.email_address' => ( disabled => 1, required => 1 );
has_field 'save' => ( type => 'Submit' );
}

my $form = MyApp::Test::Form1->new;
my $init_object = { foo => 'my_foo', bar => 'my_bar' };
my $init_object = {
foo => 'my_foo',
bar => 'my_bar',
user => { email_address => 'joe@nowhere.com' },
};
$form->process( init_object => $init_object, params => {} );
is_deeply( $form->fif, $init_object, 'fif is correct' );
my $submitted = { bar => 'subm_bar' };
my $fif = {
foo => 'my_foo',
bar => 'my_bar',
'user.email_address' => 'joe@nowhere.com',
};
is_deeply( $form->fif, $fif, 'fif is correct' );
my $submitted = {
bar => 'subm_bar',
};

$form->process( init_object => $init_object, params => $submitted );
is_deeply( $form->fif, { foo => 'my_foo', bar => 'subm_bar' },
$fif->{bar} = 'subm_bar';
is_deeply( $form->fif, $fif,
'right fif after submission, init_object' );
is_deeply( $form->value, { foo => 'my_foo', bar => 'subm_bar' } );
$init_object->{bar} = 'subm_bar';
is_deeply( $form->value, $init_object, 'right value' );
}

{
Expand Down
2 changes: 1 addition & 1 deletion t/fields/select.t
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ ok( $form, 'form built' );
my $rendered_field = $form->field('my_list')->render;
like( $rendered_field, qr/<option value="1" id="my_list\.0" selected="selected">/, 'element is selected' );
# the 'value' of the field should reflect the selected values
is_deeply( $form->value, {}, 'no form value' );
is_deeply( $form->value, { my_list => 1 }, 'value ok' );
ok( $form->field('my_list')->fif, 'fif value');
$form->process( { my_list => 2 } );
is_deeply( $form->fif, { my_list => 2 }, 'fif is correct' );
Expand Down

0 comments on commit 6e8cd29

Please sign in to comment.