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
- is related to
-
MONGOID-2020 Changed, Changes, & Errors: Root and Recursively Children
- Closed