Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Select now uses Spark::Couplet so we can maintain ordered pairs of te…

…xt and value
  • Loading branch information...
commit 8e133f54d750d93315b81debb7742ed2926bbce2 1 parent b940235
James Laver authored
Showing with 22 additions and 9 deletions.
  1. +16 −7 lib/SparkX/Form/Field/Select.pm
  2. +6 −2 t/13-select.t
23 lib/SparkX/Form/Field/Select.pm
View
@@ -4,6 +4,7 @@ package SparkX::Form::Field::Select;
use Moose;
use HTML::Tiny;
+use Spark::Types qw(SCouplet);
extends 'Spark::Form::Field';
with 'Spark::Form::Field::Role::Printable::HTML',
@@ -14,11 +15,15 @@ has '+value' => (
);
has options => (
- isa => 'ArrayRef',
+ isa => SCouplet,
is => 'rw',
+ coerce => 1,
required => 0,
lazy => 1,
default => sub { return shift->value },
+ handles => {
+ options_kv => 'key_values_paired',
+ },
);
sub to_html {
@@ -30,17 +35,21 @@ sub to_xhtml {
}
sub _render_element {
- my ($self, $html, $option) = @_;
+ my ($self, $html, $text, $value) = @_;
return $html->option({
- value => $option,
- (($self->value eq $option) ? (selected => 'selected') : ()),
- }, $option);
+ value => $value,
+ (($self->value eq $value) ? (selected => 'selected') : ()),
+ }, $text);
}
sub _render {
my ($self, $html) = @_;
- my @options = map { $self->_render_element($html, $_) } @{$self->options};
-
+ my @options = map {
+ $self->_render_element(
+ $html, # HTML::Tiny,
+ @{$_}, # Text, Value
+ )
+ } $self->options_kv;
return $html->select(
{name => $self->name}, join q{ }, @options
);
8 t/13-select.t
View
@@ -8,8 +8,12 @@ use SparkX::Form::Field::Select;
my $form = Spark::Form->new;
my $b = SparkX::Form::Field::Select->new(
name => 'test', form => $form,
- options => ['test-1','test-2','test-3'],
+ options => [
+ 'Option 1' => 'o1',
+ 'Option 2' => 'o2',
+ 'Option 3' => 'o3',
+ ],
value => 'test-2',
);
-is($b->to_xhtml,'<select name="test"><option value="test-1">test-1</option> <option selected="selected" value="test-2">test-2</option> <option value="test-3">test-3</option></select>','Test xhtml representation');
+is($b->to_xhtml,'<select name="test"><option value="o1">Option 1</option> <option value="o2">Option 2</option> <option value="o3">Option 3</option></select>','Test xhtml representation');
Please sign in to comment.
Something went wrong with that request. Please try again.