Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ember Data 2.1 compatibility with belongsTo? #19

Closed
corrspt opened this issue Oct 29, 2015 · 3 comments
Closed

Ember Data 2.1 compatibility with belongsTo? #19

corrspt opened this issue Oct 29, 2015 · 3 comments
Assignees
Labels

Comments

@corrspt
Copy link

corrspt commented Oct 29, 2015

Hey!

I'm working with ember-model-validator and EmberData 2.1 and I'm having an issue validating a belongsTo relationship. I'm using version 1.1.0 of ember-model-validator

I've create my model like the following

import DS from 'ember-data';
import Ember from 'ember';
import Validator from 'weldnote/mixins/model-validator';

const { computed, isEmpty, inject } = Ember;
const { belongsTo } = DS;

export default DS.Model.extend(Validator, {
  i18n: inject.service(),
  code: belongsTo('industry-code', { async: false }),

  validations: {
    code: {
      presence: true
    }
  }

});

When I call validate() the code is considered to be empty (but it's not).
I've managed to debug the issue to the following:

//model-validator.js
_validatePresence: function _validatePresence(property, validation) {
      var propertyValue = this.get(property);
      // If the property is an async relationship.
      if (this._modelRelations() && !Ember['default'].isBlank(this._modelRelations()[property])) {
        propertyValue = this.get(property + '.content'); // Here is the problem
      }
      if (Ember['default'].isBlank(propertyValue)) {
        this.set('isValidNow', false);
        this._addToErrors(property, validation.presence, Messages['default'].presenceMessage);
      }
    },

If I console.log propertyValue, right before the this.get(property + .content') I get the following
Class {store: Class, container: Container, _internalModel: ember$data$lib$system$model$internal$model$$InternalModel, isError: false, adapterError: (...)

But the this.get(property + '.content') returns undefined.... was it this way with older versions of Ember Data (I'm guessing that's my problem).

I've been dealing with this through custom validations, but it's kind of annoying to always do this:

validations: {
    code: {
      custom: {
        validation(key, value, model) {
          let id = model.get('code.id');
          return !isEmpty(id);
        }, message(key, value) {
          let translation = value.translate('edit-view.attributes.required');
          return translation;
        }
      }
    }

Thanks a lot for the project. It's been very helpful

@esbanarango esbanarango self-assigned this Oct 29, 2015
@esbanarango
Copy link
Owner

@corrspt Thanks for reporting this bug. It should be fixed now. Could you confirm on your end.

@corrspt
Copy link
Author

corrspt commented Oct 30, 2015

@esbanarango - Thank you so much for fixing it insanely fast. I confirm that it's fixed, thanks again! I was able to delete a lot of code because of that 👍

@esbanarango
Copy link
Owner

So glad it helped you! 🍻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants