fix for certain fields failing silently when they are required and left blank
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Field / Role / Mutable.pm
1 package Reaction::UI::ViewPort::Field::Role::Mutable;
2
3 use Reaction::Role;
4
5 use aliased 'Reaction::InterfaceModel::Action';
6 use aliased 'Reaction::Meta::InterfaceModel::Action::ParameterAttribute';
7
8 use namespace::clean -except => [ qw(meta) ];
9
10 has model     => (is => 'ro', isa => Action, required => 1);
11 has attribute => (is => 'ro', isa => ParameterAttribute, required => 1);
12
13 has value      => (
14   is => 'rw', lazy_build => 1, trigger_adopt('value'),
15   clearer => 'clear_value',
16 );
17 has needs_sync => (is => 'rw', isa => 'Int', default => 0);
18
19 has message => (is => 'rw', isa => 'Str', clearer => 'clear_message');
20
21 after clear_value => sub {
22   my $self = shift;
23   $self->clear_message if $self->has_message;
24   $self->needs_sync(1);
25 };
26
27 sub adopt_value {
28   my ($self) = @_;
29   $self->clear_message if $self->has_message;
30   $self->needs_sync(1); # if $self->has_attribute;
31 }
32
33
34 sub can_sync_to_action {
35   my $self = shift;
36
37   # if field is already sync'ed, it can be sync'ed again
38   # this will make sync_to_action no-op if needs_sync is 0
39   return 1 unless $self->needs_sync;
40   my $attr = $self->attribute;
41
42   if ($self->has_value) {
43     my $value = $self->value;
44     if (my $tc = $attr->type_constraint) {
45       $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
46       if (defined (my $error = $tc->validate($value))) {
47         $self->message($error);
48         return;
49       }
50     }
51   } else {
52     if( $self->model->attribute_is_required($attr) ){
53       if(my $error = $self->model->error_for($self->attribute) ){
54         $self->message( $error );
55       }
56       return;
57     }
58   }
59   return 1;
60 };
61
62
63 sub sync_to_action {
64   my ($self) = @_;
65
66   # don't sync if we're already synced
67   return unless $self->needs_sync;
68
69   # if we got here, needs_sync is 1
70   # can_sync_to_action will do coercion checks, etc.
71   return unless $self->can_sync_to_action;
72
73   my $attr = $self->attribute;
74
75   if ($self->has_value) {
76     my $value = $self->value;
77     if (my $tc = $attr->type_constraint) {
78       #this will go away when we have moose dbic. until then though...
79       $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
80     }
81     my $writer = $attr->get_write_method;
82     confess "No writer for attribute" unless defined($writer);
83     $self->model->$writer($value);
84   } else {
85     my $predicate = $attr->get_predicate_method;
86     confess "No predicate for attribute" unless defined($predicate);
87     if ($self->model->$predicate) {
88       my $clearer = $attr->get_clearer_method;
89       confess "${predicate} returned true but no clearer for attribute"
90         unless defined($clearer);
91       $self->model->$clearer;
92     }
93   }
94   $self->needs_sync(0);
95 };
96 sub sync_from_action {
97   my ($self) = @_;
98   return unless !$self->needs_sync; # && $self->has_attribute;
99   if( !$self->has_message ){
100     if(my $error = $self->model->error_for($self->attribute) ){
101       $self->message( $error );
102     }
103   }
104 };
105
106 around accept_events => sub { ('value', shift->(@_)) };
107
108
109
110 1;
111
112 =head1 NAME
113
114 Reaction::UI::ViewPort::Role::Actions
115
116 =head1 DESCRIPTION
117
118 A role to ease attaching actions to L<Reaction::InterfaceModel::Object>s
119
120 =head1 ATTRIBUTES
121
122 =head2 needs_sync
123
124 =head2 message
125
126 =head2 model
127
128 =head2 attribute
129
130 =head2 value
131
132 =head1 METHODS
133
134 =head2 accept_events
135
136 =head2 sync_from_action
137
138 =head2 sync_to_action
139
140 =head2 adopt_value
141
142 =head1 AUTHORS
143
144 See L<Reaction::Class> for authors.
145
146 =head1 LICENSE
147
148 See L<Reaction::Class> for the license.
149
150 =cut