ember-cp-validations icon indicating copy to clipboard operation
ember-cp-validations copied to clipboard

if two models have two attributes with the same name, how can we get from the error validations object the modelName info for each attribute?

Open NouranMahmoud opened this issue 8 years ago • 4 comments

Environment

  • Ember CLI Version: 2.11.1
  • Ember CP Validations Version: 3.3.2

Steps to Reproduce

I have two models with has-many relationship, and both models have attributes with the same name (ex:- "title") .. when I get the "validation.errors" I get an array which contains two validation objects for both "title" of the first model and "title" of the second model .. the question is how can I get the modelName where each one of the "title"s belong to?

NouranMahmoud avatar Jun 15 '17 14:06 NouranMahmoud

@NouranMahmoud any chance you can reproduce this in a twiddle so I can play around with it?

offirgolan avatar Jun 16 '17 03:06 offirgolan

Sure @offirgolan thanks a lot for your help, I've created a full example of my case here I've added comments on the part that has the issue.

In a nutshell:

  • The example contains main form (multi-step-form controller) and two models (page, post).
  • Page and post have (has-many relationship) and both have property "title" on their models.
  • I validate the form on "goNext" action.
  • As this is a multi-step form so validation must be in specific fields (per step)
  • validation on the first step won't pass because there is a "title" property on "posts" model.
  • Question: how can I know which model does this title belong to? line: 48, file: controllers/multi-step-form.js

NouranMahmoud avatar Jun 17 '17 16:06 NouranMahmoud

@NouranMahmoud this seems to be a discovery issue on your end. I think think there is much I can do on the library side.

If I'm understanding this correctly, you have a page model with a hasMany of posts and you dont know which one is invalid.

You can do a couple of things here:

  1. Just do a quick look up to get the invalid ones pageModel.get('posts').filterBy('validations.isInvalid'). That will return all invalid post models that belong to that page model.
  2. You can restructure your logic. Im not really sure whats going on in your twiddle since it's a bit involved but if you're creating a new page object, step 1 should have an empty posts hasMany.
  3. You can disable the hasMany validations on step one, and then re-enable it on step two. See the docs on the disabled option.

offirgolan avatar Jun 27 '17 06:06 offirgolan

@NouranMahmoud the issue is quite old. Did you manage to solve your problem? Can we close the issue?

fsmanuel avatar Sep 10 '22 09:09 fsmanuel