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

Expose a DS.Errors compatible API

Open musaffa opened this issue 9 years ago • 8 comments

Currently this.get(user, 'validations.errors') returns an array of error objects. But calling errors with DS.Errors returns an Ember Object.

{{#each model.errors.username as |error|}}
  <div class="error">
    {{error.message}}
  </div>
{{/each}}

In this implementation, we can call username attribute on the object returned by errors. The most of the form addons conforms to this API. It would be nice if ember-cp-validations follows the same convention. It will make the integration with form addons much easier.

musaffa avatar Apr 25 '16 11:04 musaffa

Thanks for opening up this issue. You can access specific attribute validations via model.get('validations.attrs.username.errors')

or in your template via

{{#each (v-get model 'username' 'errors') as |error|}}
  <div class="error">
    {{error.message}}
  </div>
{{/each}}

offirgolan avatar Apr 25 '16 19:04 offirgolan

@offirgolan Unfortunately almost all the form addons, for example ember-form-for (see implemenation), ember-simple-form (see implementation), implements DS.Errors like API. If ember-cp-validations could expose an error API according to the ember's convention, it could support the form addons out of the box.

I don't want to create another form addon and sometimes it is not possible to override the addon templates. So all the developers are left to do is to create another form addon or an adapter which will convert ember-cp-validations errors to DS.Errors compatible error object. What if ember-cp-validations already solves the problem for them?

Thanks.

musaffa avatar Apr 28 '16 02:04 musaffa

I understand the use-case for this, but as of right now, I dont see the way the validations object is structured to change. If we were to have an errors object on the model, it will not only overwrite ember-data DS.Error implementation, but will have residual effects for any platform that is dependent on it. The validation object needs to be its own entity to not clash with any existing implementations.

One proposal was to populate the DS.Error object with this library's messages (see #157). Unfortunately, I have yet to explore this option. Maybe once I have some free time. In the mean time, I would suggest taking a look at ember-bootstrap-cp-validations.

If you have an idea about how to best solve this issue without restructuring the entire API, feel free to let me know and we can try to put something together.

offirgolan avatar Apr 28 '16 04:04 offirgolan

Another option can be a DS.Errors like errors object under validationsnamespace. It wont solve the problem but will ease the pain a lot.

Not only polluting errors object of the model is a bad idea, mutating model instance in the form itself is a bad idea. Model instances should be immutable.

musaffa avatar Apr 28 '16 11:04 musaffa

@offirgolan Off topic, is it possible to know whether an attribute has any validator?

musaffa avatar Apr 28 '16 14:04 musaffa

Another option can be a DS.Errors like errors object under validationsnamespace. I wont solve the problem but will ease the pain a lot.

I feel like the easiest way to solve your problem is just to do the following in your model

validationErrors: computed('validations.errors.[]', function() {
  return _.groupBy(this.get('validations.errors'), e => e.get('attribute'));
})

Maybe I can add something similar to the validations object. 😄

is it possible to know whether an attribute has any validator?

If an attribute does not have any validators, then it will not be part of the validations.attrs object

offirgolan avatar Apr 28 '16 22:04 offirgolan

Maybe I can add something similar to the validations object.

:+1:

musaffa avatar Apr 29 '16 05:04 musaffa

If an attribute does not have any validators, then it will not be part of the validations.attrs object

Is it possible to check for the presence of any specific validator on an attribute? For example, I want to determine whether the field is required (to put a * in the label) dynamically based the presence of the Presence validator. Even more specific, whether that Presence validator is set to true.

Edit:

got it!

this.get('object.validations.attrs.username.options.presence.presence')

musaffa avatar Apr 30 '16 09:04 musaffa

Closing some old issues as wontfix for now. If this issue is still relevant, please feel free to open a new one!

RobbieTheWagner avatar Feb 07 '23 17:02 RobbieTheWagner