fix for certain fields failing silently when they are required and left blank
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Field / Role / Mutable.pm
index 614a36c..0acac38 100644 (file)
@@ -15,7 +15,7 @@ has value      => (
   clearer => 'clear_value',
 );
 has needs_sync => (is => 'rw', isa => 'Int', default => 0);
-#predicates are autmagically generated for lazy and non-required attrs
+
 has message => (is => 'rw', isa => 'Str', clearer => 'clear_message');
 
 after clear_value => sub {
@@ -23,13 +23,19 @@ after clear_value => sub {
   $self->clear_message if $self->has_message;
   $self->needs_sync(1);
 };
+
 sub adopt_value {
   my ($self) = @_;
   $self->clear_message if $self->has_message;
   $self->needs_sync(1); # if $self->has_attribute;
-};
+}
+
+
 sub can_sync_to_action {
   my $self = shift;
+
+  # if field is already sync'ed, it can be sync'ed again
+  # this will make sync_to_action no-op if needs_sync is 0
   return 1 unless $self->needs_sync;
   my $attr = $self->attribute;
 
@@ -43,17 +49,25 @@ sub can_sync_to_action {
       }
     }
   } else {
-    if ( $self->model->attribute_is_required($attr) ) {
-      my $tc = $attr->type_constraint;
-      $self->message($tc->get_message) if $tc->has_message;
+    if( $self->model->attribute_is_required($attr) ){
+      if(my $error = $self->model->error_for($self->attribute) ){
+        $self->message( $error );
+      }
       return;
     }
   }
   return 1;
 };
+
+
 sub sync_to_action {
   my ($self) = @_;
+
+  # don't sync if we're already synced
   return unless $self->needs_sync;
+
+  # if we got here, needs_sync is 1
+  # can_sync_to_action will do coercion checks, etc.
   return unless $self->can_sync_to_action;
 
   my $attr = $self->attribute;