Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-3924

Errors in submodels (I want details, not that uninformative "assocation is invalid")

    • Type: Icon: Task Task
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • 5.1.0, 6.0.0
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      I think it is more an Rails and ActiveRecord issue, but as I am using Mongoid, I am starting the issue here.

      https://github.com/mongoid/mongoid/blob/master/lib/mongoid/validatable/associated.rb#L31:L46

      I can not display errors in associated model fields, instead I get abstract "there is a error" for submodel all fields. Proposed solution:

      module Mongoid
        module Validatable
      
          class AssociatedValidator
            def validate_each(document, attribute, value)
              errors = {}
              begin
                document.begin_validate
                valid = Array.wrap(value).collect do |doc|
                  if doc.nil? || doc.flagged_for_destroy?
                    true
                  else
                    valid = doc.validated? ? true : doc.valid?
                    errors.merge!(doc.errors.messages)
                    valid
                  end
                end.all?
              ensure
                document.exit_validate
              end
              document.errors.messages[attribute] = errors unless valid
            end
          end
      
        end
      end
      
      

      That monkeypatch gets me submodels errors that I can easily map to input fields.

      { "errors": { 
        "submodel": { 
          "key": ["errors"] 
        } 
      } }
      

      See also:
      https://github.com/emberjs/data/issues/2583#issuecomment-70445395

            Assignee:
            Unassigned Unassigned
            Reporter:
            mxrguspxrt mxrguspxrt
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: