[html-formfu] Populate form using a complex hashref?

Moritz Onken onken at houseofdesign.de
Mon Mar 9 10:16:49 GMT 2009


Am 09.03.2009 um 10:39 schrieb Carl Franks:

> 2009/3/8 Moritz Onken <onken at houseofdesign.de>:
>> For HashRef to work properly I need a method to unset the values of  
>> each
>> field.
>> I tried that with:
>>
>> map { eval { $_->default(undef)  } } (grep { $_->is_field }
>> @{$form->get_all_elements});
>>
>> which works fine, but fails on a Date element with:
>>
>> Invalid date format:  at lib/HTML/FormFu/Element/Date.pm line 150
>>
>> Is this a Date (DateTime) bug because you cannot set it to undef or  
>> should I
>> try another
>> way to set these fields to undef.
>
> I think it's reasonable to be able to do $date->default( undef );
> but are you sure that's what's triggering this error?
>
> line 150 is inside a:
>    elsif ( defined( $default = $self->default ) )
>
> Carl

Right, I probably mixed things up. Nevertheless here is a patch for  
Date.pm and
a test which fails without that patch.




Index: /Users/mo/Documents/workspace/HTML-FormFu/lib/HTML/FormFu/ 
Element/Date.pm
===================================================================
--- /Users/mo/Documents/workspace/HTML-FormFu/lib/HTML/FormFu/Element/ 
Date.pm	(revision 1360)
+++ /Users/mo/Documents/workspace/HTML-FormFu/lib/HTML/FormFu/Element/ 
Date.pm	(working copy)
@@ -99,7 +99,7 @@
              for my $i (0 .. $#order) {
                  my $field = $order[$i];

-                $self->_elements->[$i]->default( $self->$field- 
 >{default} );
+                $self->_elements->[$i]->default( $value ? $self-> 
$field->{default} : undef );
              }
          }


### t/elements/date_undef.t

use strict;
use warnings;

use Test::More tests => 2;

use HTML::FormFu;

my $form = HTML::FormFu->new(
     { tt_args => { INCLUDE_PATH => 'share/templates/tt/xhtml' } } );

$form->populate({elements => [{type => "Date", name => "foo", default  
=> '30-08-2009'}]});

$form->process;

like($form->render, qr/value="2009" selected="selected"/);

$form->get_field('foo')->default(undef);

like($form->render, qr/value="2009">/);



More information about the HTML-FormFu mailing list