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

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

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Won't Fix
    • Affects Version/s: None
    • Fix Version/s: 5.1.0, 6.0.0
    • Component/s: None
    • Labels:
    • Sprint:
      Ruby Sprint 31

      Description

      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

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved: