On 4/2/07, Zbigniew Lukasiak <[EMAIL PROTECTED]> wrote:
Hi,

Thanks for such a detailed analysis of the problem.  I'll apply your
patch and release a new version of HTML::Widget::DBIC.

But explaining the current situation - I am waiting for the
HTML::FormFu to be released and then I'll convert the DBIC code to be
based on it and make HTML::Widget::DBIC obsolete.

Well... I found some other bugs too in the meantime. As a pretty bad
one, I had a submit element called 'delete' along with 'save' and
'cancel' and suddenly my rows started getting deleted whenever I
submitted a form! Here's the current version I'm using, which fixes
that and a couple problems with undef too:

sub fill_widget {
   my ($dbic,$widget)[EMAIL PROTECTED];

   croak('fill_widget needs a HTML::Widget object as argument')
       unless ref $widget && $widget->isa('HTML::Widget');
   my @real_elements = $widget->find_elements;

   foreach my $element ( @real_elements ) {
       my $name=$element->name;
       next unless $name && $dbic->result_source->has_column($name)
&& $element->can('value');
       if($element->isa('HTML::Widget::Element::Checkbox')) {
                          $element->checked($dbic->$name?1:0);
                  } else {
                      if (ref $dbic->$name and $dbic->$name->can('id') and 
$dbic->$name->id) {
                          $element->value($dbic->$name->id);
                      } else {
                              $element->value($dbic->$name)
                                    unless 
$element->isa('HTML::Widget::Element::Password');
                          }
                  }
   }
}


sub populate_from_widget {
        my ($dbic,$result)[EMAIL PROTECTED];
        croak('populate_from_widget needs a HTML::Widget::Result object
as argument')
        unless ref $result && $result->isa('HTML::Widget::Result');

        #   find all checkboxes
   my %cb = map {$_->name => undef } grep {
$_->isa('HTML::Widget::Element::Checkbox') }
       $result->find_elements;
   my $params = $result->params;

   foreach my $col ( $dbic->result_source->columns ) {
       my $col_info = $dbic->column_info($col);
       my $value = scalar($result->param($col));

       next unless exists $params->{ $col } || exists $cb{ $col };

       if (defined $value) {
           if ($col_info->{data_type} and $col_info->{data_type} =~
m/^timestamp|date|int|numeric/i
               and $value eq '') {
               $value = undef;
           } elsif (!ref($value) and $value eq 'undef') {
               $value = undef;
           }
       }

       $dbic->$col($value);
   }
   $dbic->insert_or_update;
   return $dbic;
}

_______________________________________________
Html-widget mailing list
[email protected]
http://lists.rawmode.org/cgi-bin/mailman/listinfo/html-widget

Reply via email to