DS.Model#errors

errors{DS.Errors}

Defined in addon/-private/system/model/model.js:309

When the record is in the invalid state this object will contain any errors returned by the adapter. When present the errors hash contains keys corresponding to the invalid property names and values which are arrays of Javascript objects with two keys:

  • message A string containing the error message from the backend
  • attribute The name of the property associated with this error message
record.get('errors.length'); // 0
record.set('foo', 'invalid value');
record.save().catch(function() {
  record.get('errors').get('foo');
  // [{message: 'foo should be a number.', attribute: 'foo'}]
});

The errors property us useful for displaying error messages to the user.

<label>Username: {{input value=username}} </label>
{{#each model.errors.username as |error|}}
  <div class="error">
    {{error.message}}
  </div>
{{/each}}
<label>Email: {{input value=email}} </label>
{{#each model.errors.email as |error|}}
  <div class="error">
    {{error.message}}
  </div>
{{/each}}

You can also access the special messages property on the error object to get an array of all the error strings.

{{#each model.errors.messages as |message|}}
  <div class="error">
    {{message}}
  </div>
{{/each}}
doc_EmberJs
2016-11-30 16:50:08
Comments
Leave a Comment

Please login to continue.