Bind with DS.error in the two ways
Hi,
When I have a model with the DS-error validator and I receive an error from the server, for example, $E.get('validations.errors') gives errors which is correct. But when I pass validations, $E.get('errors.name') does not have errors from ember-cp-validations.
I think it can be useful to have kind of two way bindings. It can be useful for compatibility issues line like this one, from Ember-rapid-form for which I contribute.
Please, let me know if I'm wrong. Do you think it's useful?
ping @offirgolan . I'm ready to contribute but I want to know if you find it useful. Thanks.
When I first wrote up this library, I had support for this but it was removed due to a bug.
One thing I can recommend, is creating a 3rd party addon for your form library that overwrites that init method to bind to validations.attrs.${property}.errors. I have one create for ember-bootstrap that you can take a look at (ember-bootstrap-cp-validations)
I did this and it seems to work :
validate() {
return this.get('validations').validate(...arguments).then(() => {
let modelErrors = this.get('errors');
this.eachAttribute((attribute) => {
if (this instanceof self.DS.Model && !Ember.isNone(modelErrors) && Ember.canInvoke(modelErrors, 'add')) {
if(modelErrors.has(attribute)) {
modelErrors.remove(attribute);
}
let messages = this.get(`validations.attrs.${attribute}.messages`);
if (messages) {
messages.forEach((m) => modelErrors.add(attribute, m));
}
}
});
});
},
Let me know what you think. I can do a third party addon as you suggested but I think it can be a useful mixin in ember-cp-validations.
@GCorbel sorry for such a delayed response! I definitely think this would be a really great feature to have. It would even be better if a user is able to pick which attributes they want to exclude/include as well.
In terms of implementation, I would have to think this through a little more. I also want to make sure we dont run into an infinite loop situation with users using the validator('ds-error').
One implementation I think would work best is having an option available to all validators such as disabled, message, etc.
const Validations = buildValidations({
username: validator('presence', { presence: true, addToDS: true})
});
That way addToDS can be used in default options, as well as global options. Maybe Ill take a look at this over the weekend of sometime early next week.
P.S. Idk if I really like addToDS lol. Feel free to make suggestions 😄
Edit:
Now that I think of it, we can just disable the addToDS option in the ds-error validator. That will probably make the implementation pretty simple... I shall explore more!
Closing some old issues as wontfix for now. If this issue is still relevant, please feel free to open a new one!