=== Changes
==================================================================
--- Changes (revision 22942)
+++ Changes (local)
@@ -1,5 +1,8 @@
0.02005
-
+ _ issue #21: added methods to _Group for empty_first_label, and refactored
+ item generation out of options, values, value_range to _get_empty_first_option,
+ which the others call.
+
- Bugfix: save_to_model() many_to_many multi-value fields where
'default_column' included the table name (or 'me.') failed.
=== lib/HTML/FormFu/Element/_Group.pm
==================================================================
--- lib/HTML/FormFu/Element/_Group.pm (revision 22942)
+++ lib/HTML/FormFu/Element/_Group.pm (local)
@@ -6,10 +6,12 @@
use HTML::FormFu::ObjectUtil qw/ _coerce /;
use HTML::FormFu::Util qw/ append_xml_attribute literal /;
+
use Storable qw( dclone );
use Carp qw( croak );
__PACKAGE__->mk_accessors(qw/ _options empty_first /);
+__PACKAGE__->mk_output_accessors(qw/ empty_first_label/);
my @ALLOWED_OPTION_KEYS = qw/
group
@@ -56,13 +58,7 @@
croak "options argument must be an array-ref" if $@;
if ( $self->empty_first ) {
- push @new,
- {
- value => '',
- label => '',
- attributes => {},
- label_attributes => {},
- };
+ push @new, $self->_get_empty_first_option;
}
for my $item (@options) {
@@ -74,7 +70,16 @@
return $self;
}
-
+sub _get_empty_first_option {
+ my ( $self ) = @_;
+ my $l = $self->empty_first_label || '';
+ return {
+ value => '',
+ label => $l,
+ attributes => {},
+ label_attributes => {},
+ };
+}
sub _parse_option {
my ( $self, $item ) = @_;
@@ -197,10 +202,6 @@
croak "values argument must be an array-ref" if $@;
}
- if ( $self->empty_first ) {
- unshift @values, '';
- }
-
@new = (
map { { value => $_,
label => ucfirst $_,
@@ -209,6 +210,9 @@
}
} @values
);
+ if ( $self->empty_first ) {
+ unshift @new, $self->_get_empty_first_option;
+ }
$self->_options( \@new );
@@ -231,10 +235,6 @@
my $end = pop @values;
my $start = pop @values;
- if ( $self->empty_first ) {
- unshift @values, '';
- }
-
return $self->values( [ @values, $start .. $end ] );
}
=== t/elements/select_empty_first_label.t
==================================================================
--- t/elements/select_empty_first_label.t (revision 22942)
+++ t/elements/select_empty_first_label.t (local)
@@ -0,0 +1,81 @@
+use strict;
+use warnings;
+use lib 't/lib';
+
+use Test::More tests => 4;
+
+use HTML::FormFu;
+
+my $form = HTML::FormFu->new({
+ localize_class => 'HTMLFormFu::I18N',
+});
+
+{
+ my $field = $form->element('Select')->name('foo');
+ $field->empty_first(1);
+ $field->empty_first_label('empty_label');
+ $field->options([ [ 1 => 'One' ], [ 2 => 'Two' ] ]);
+
+ my $field_xhtml = qq{
+
+};
+
+ is( "$field", $field_xhtml, 'stringified field' );
+
+}
+
+{
+ my $field = $form->element('Select')->name('foo');
+ $field->empty_first(1);
+ $field->empty_first_label('empty_label');
+ $field->values([qw/one two/]);
+
+ my $field_xhtml = qq{
+
+};
+
+ is( "$field", $field_xhtml, 'stringified field' );
+
+}
+{
+ my $field = $form->element('Select')->name('foo');
+ $field->empty_first(1);
+ $field->empty_first_label('empty_label');
+ $field->value_range([1, 2]);
+
+ my $field_xhtml = qq{
+
+};
+
+ is( "$field", $field_xhtml, 'stringified field' );
+
+}
+{
+ my $field = $form->element('Select')->name('foo');
+ $field->empty_first(1);
+ $field->empty_first_label_loc('test_label');
+ $field->options([ [ 1 => 'One' ], [ 2 => 'Two' ] ]);
+
+ my $field_xhtml = qq{
+
+};
+
+ is( "$field", $field_xhtml, 'stringified field' );
+
+}
\ No newline at end of file