[html-formfu] Re: problems with Repeatable Select and increment_fields_names = 0

Doug Orleans dougorleans at gmail.com
Fri Dec 4 01:43:44 GMT 2009


On Tue, Dec 1, 2009 at 5:06 PM, Doug Orleans <dougorleans at gmail.com> wrote:
> I'm having problems with a Repeatable Select element and
> increment_fields_names = 0:

Actually the same thing happens with any repeated _Group subclass
(e.g. Radiogroup).  A patch is below for _Group->prepare_attrs, based
on some similar code in _Field->_render_value.  Note that it still
handles the case of a single multi-Select (i.e. with the "multiple"
attribute) or Checkboxgroup, by checking whether there are multiple
fields with the same name and leaving the value array alone otherwise.
 It's possible that some forms could be relying on the current
behavior with multiple multi-Selects with the same name that have
disjoint option lists, but this seems unlikely?

--dougorleans at gmail.com


*** /usr/local/share/perl/5.10.0/HTML/FormFu/Element/_Group.pm	2009-11-25
07:02:28.000000000 -0500
--- HTML/FormFu/Element/_Group.pm	2009-12-03 20:33:26.000000000 -0500
***************
*** 318,327 ****
--- 318,344 ----
      my $value
          = defined $self->name
          ? $self->get_nested_hash_value( $self->form->input,
$self->nested_name )
          : undef;

+     if ( ref $value eq 'ARRAY' ) {
+         my $elems = $self->form->get_fields( $self->name );
+         if ( $#$elems ) {
+             # There are multiple fields with the same name; assume
+             # none are multi-value fields, i.e. only one selected
+             # option per field.  (Otherwise it might be ambiguous
+             # which option came from which field.)
+             for ( 0 .. @$elems - 1 ) {
+                 if ( $self == $elems->[$_] ) {
+                     # Use the value of the option actually selected in
+                     # this group.
+                     $value = $value->[$_];
+                 }
+             }
+         }
+     }
+
      if ( !$submitted && defined $default ) {
          for my $deflator ( @{ $self->_deflators } ) {
              $default = $deflator->process($default);
          }
      }



More information about the HTML-FormFu mailing list