Skip to content

Commit 925d809

Browse files
committed
use coercion on 'options' instead of duplicate code in _load_options
1 parent 1b1d3c5 commit 925d809

File tree

1 file changed

+9
-21
lines changed

1 file changed

+9
-21
lines changed

lib/HTML/FormHandler/Field/Select.pm

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -601,29 +601,17 @@ sub _load_options {
601601
return unless @options; # so if there isn't an options method and no options
602602
# from a table, already set options attributes stays put
603603

604-
# allow returning arrayref
605-
my $first_option = $options[0];
606-
if ( ref $first_option eq 'ARRAY' ) {
607-
if ( ref $first_option->[0] eq 'ARRAY' ) {
608-
@options = map { label => $_, value => $_ },
609-
@{ $first_option->[0] };
610-
}
611-
else {
612-
@options = @$first_option;
613-
}
614-
}
615-
return unless @options;
616-
my $opts;
617-
# if options_<field_name> is returning an already constructed array of hashrefs
618-
if ( ref $options[0] eq 'HASH' ) {
619-
$opts = \@options;
604+
# possibilities:
605+
# @options = ( 1 => 'one', 2 => 'two' );
606+
# @options = ([ 1 => 'one', 2 => 'tw' ]);
607+
# @options = ([ { value => 1, label => 'one'}, { value => 2, label => 'two'}]);
608+
# @options = ([[ 'one', 'two' ]]);
609+
my $opts = ref $options[0] eq 'ARRAY' ? $options[0] : \@options;
610+
if ( ref $opts->[0] eq 'HASH' ) {
611+
# do the array of hashrefs separately so we can get the defaults
620612
$self->default_from_options($opts);
621613
}
622-
else {
623-
croak "Options array must contain an even number of elements for field " . $self->name
624-
if @options % 2;
625-
push @{$opts}, { value => shift @options, label => shift @options } while @options;
626-
}
614+
$opts = $self->options($opts); # coerce will reformat
627615
if ($opts) {
628616
# sort options if sort method exists
629617
$opts = $self->sort_options($opts) if $self->has_sort_options_method;

0 commit comments

Comments
 (0)