diff --git a/lib/WebGUI/Form/Control.pm b/lib/WebGUI/Form/Control.pm index ee4a3052e..2bb57bfa3 100644 --- a/lib/WebGUI/Form/Control.pm +++ b/lib/WebGUI/Form/Control.pm @@ -302,9 +302,10 @@ sub generateIdParameter { #------------------------------------------------------------------- -=head2 get ( var ) +=head2 get ( [var] ) -Returns a property of this form object. +Returns a property of this form object. If no property is specified, returns a hashref of all +properties. =head3 var @@ -315,7 +316,11 @@ The variable name of the value to return. sub get { my $self = shift; my $var = shift; - return $self->{_params}{$var}; + if ( $var ) { + return $self->{_params}{$var}; + } + + return $self->{_params}; } #------------------------------------------------------------------- @@ -696,7 +701,20 @@ sub toHtmlWithWrapper { } } +#---------------------------------------------------------------------------- +=head2 toTemplateVars ( ) + +Returns a hashref of template variables of the properties of this control, used +to re-create it in a template. + +=cut + +sub toTemplateVars { + my ( $self ) = @_; + my %var = %{$self->get}; + return \%var; +} 1; diff --git a/t/tests/Test/WebGUI/Form/Control.pm b/t/tests/Test/WebGUI/Form/Control.pm index b842ba68a..e4197e049 100644 --- a/t/tests/Test/WebGUI/Form/Control.pm +++ b/t/tests/Test/WebGUI/Form/Control.pm @@ -41,7 +41,7 @@ sub t_00_method_check : Test(1) { } -sub t_01_get_set : Test(2) { +sub t_01_get_set : Test(3) { my $test = shift; my $session = $test->session; @@ -49,7 +49,7 @@ sub t_01_get_set : Test(2) { lives_ok { $form->set('name', 'form1'); } 'set name'; is $form->get('name'), 'form1', 'get name'; - + cmp_deeply $form->get, superhashof({ name => 'form1' }), 'get hashref'; } sub t_02_instanced : Test(1) { @@ -63,4 +63,16 @@ sub t_02_instanced : Test(1) { is $form->get('name'), 'form1', 'name set on instanciation'; } +sub t_03_toTemplateVars : Test(2) { + my $test = shift; + my $session = $test->session; + + my $form = $test->class->new($session, { + name => 'form1', + }); + + cmp_deeply $form->get, superhashof({ name => 'form1' }), 'toTemplateVars hashref'; + isnt $form->toTemplateVars, $form->get, 'toTemplateVars creates safe hashref'; +} + 1;