Create fields are fixed again. Also fixed: ChooseMany when tryingto remove the last...
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Field / Mutable / ChooseOne.pm
CommitLineData
ddccc6a2 1package Reaction::UI::ViewPort::Field::Mutable::ChooseOne;
2
3use Reaction::Class;
4
5class ChooseOne is 'Reaction::UI::ViewPort::Field', which {
6
c8fbb8ad 7 does 'Reaction::UI::ViewPort::Field::Role::Mutable';
8 does 'Reaction::UI::ViewPort::Field::Role::Choices';
ddccc6a2 9
10 around value => sub {
11 my $orig = shift;
12 my $self = shift;
13 return $orig->($self) unless @_;
14 my $value = shift;
15 if (defined $value) {
16 $value = $self->str_to_ident($value) if (!ref $value);
aac30a0d 17 my $attribute = $self->attribute;
18 my $checked = $attribute->check_valid_value($self->model, $value);
19 unless (defined $checked) {
20 require Data::Dumper;
21 my $serialised = Data::Dumper->new([ $value ])->Indent(0)->Dump;
22 $serialised =~ s/^\$VAR1 = //; $serialised =~ s/;$//;
23 confess "${serialised} is not a valid value for ${\$attribute->name} on "
24 ."${\$attribute->associated_class->name}";
25 }
ddccc6a2 26 $value = $checked;
27 }
28 $orig->($self, $value);
29 };
30
cc44a337 31 implements _build_value_string => as {
32 my $self = shift;
70db8298 33 my $value = $self->value;
34 return $self->obj_to_name($value->{value}) if ref $value eq 'HASH';
35 $value;
cc44a337 36 };
37
ddccc6a2 38 implements is_current_value => as {
39 my ($self, $check_value) = @_;
40 my $our_value = $self->value;
41 return unless ref($our_value);
42 $check_value = $self->obj_to_str($check_value) if ref($check_value);
43 return $self->obj_to_str($our_value) eq $check_value;
44 };
45
46
47};
48
491;