Server-side validation processing using Ember Data

I'm having server side processing issues with Ember and Ember data.

If a validation error occurs, the API returns 422. The Ember data then launches the becameInvalid on the model.

Hence, I'm not sure what the best way to handle the errors I get, and how to get them to go into the view.

 App.Challenge = DS.Model.extend Ember.Validations, title: attr('string') summary: attr('string') # other attributes becameInvalid: (errors) -> # is it the place where I should handle the errors? # how would I make the errors bubble up to the view here? 

I have 2 questions.

  • I'm not sure that becameInvalid is the place to handle errors, and if so, how to make errors appear in the view
  • In becameInvalid , @get('isValid') returns true , which makes no sense to me.
+4
source share
1 answer

Is this the place where I should handle errors?

Yes. But you may not need to do anything. Ember-data expects your api to include any validation errors in it. This error object is passed to the becameInvalid hook, and is also stored as the errors property in the model. Therefore, if all you want to do is display errors in your view, it might be enough to do something like:

 {{input value=firstName}}<p class="inline-help">{{errors.firstName}}</p> 

See: https://github.com/emberjs/data/blob/master/packages/ember-data/lib/serializers/rest_serializer.js#L50-L61

In wasInvalid, @get ('isValid') returns true, which makes no sense to me

Agreed, which is strange. I think this is a binding, like, for example, the startInvalid hook works before the bindings are updated.

+2
source

Source: https://habr.com/ru/post/1489350/


All Articles