Class Errors

Extends: ArrayProxy
Uses: Evented
Defined in: ../model/addon/-private/errors.js:13
Module: @ember-data/store

add (attribute, messages)

Module: @ember-data/store
attribute
String
- the property name of an attribute or relationship
messages
String[]|string
- an error message or array of error messages for the attribute

Manually adds errors to the record. This will trigger the becameInvalid event/ lifecycle method on the record and transition the record into an invalid state.

Example

 let errors = get(user, 'errors');

 // add multiple errors
 errors.add('password', [
   'Must be at least 12 characters',
   'Must contain at least one symbol',
   'Cannot contain your name'
 ]);

 errors.errorsFor('password');
 // =>
 // [
 //   { attribute: 'password', message: 'Must be at least 12 characters' },
 //   { attribute: 'password', message: 'Must contain at least one symbol' },
 //   { attribute: 'password', message: 'Cannot contain your name' },
 // ]

 // add a single error
 errors.add('username', 'This field is required');

 errors.errorsFor('password');
 // =>
 // [
 //   { attribute: 'username', message: 'This field is required' },
 // ]

errorsFor (attribute) Array

Module: @ember-data/store
attribute
String
returns
Array

Returns errors for a given attribute

let user = store.createRecord('user', {
  username: 'tomster',
  email: 'invalidEmail'
});
user.save().catch(function(){
  user.get('errors').errorsFor('email'); // returns:
  // [{attribute: "email", message: "Doesn't look like a valid email."}]
});

has (attribute) Boolean

Module: @ember-data/store
attribute
String
returns
Boolean
true if there some errors on given attribute

Checks if there are error messages for the given attribute.

app/controllers/user/edit.js
import Controller from '@ember/controller';
import { action } from '@ember/object';

export default class UserEditController extends Controller {
  @action
  save(user) {
    if (user.get('errors').has('email')) {
      return alert('Please update your email before attempting to save.');
    }
    user.save();
  }
}

remove (member)

Module: @ember-data/store
member
String
- the property name of an attribute or relationship

Manually removes all errors for a given member from the record. This will transition the record into a valid state, and triggers the becameValid event and lifecycle method.

Example:

 let errors = get('user', errors);
 errors.add('phone', ['error-1', 'error-2']);

 errors.errorsFor('phone');
 // =>
 // [
 //   { attribute: 'phone', message: 'error-1' },
 //   { attribute: 'phone', message: 'error-2' },
 // ]

 errors.remove('phone');

 errors.errorsFor('phone');
 // => undefined

© 2020 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://api.emberjs.com/ember-data/3.25/classes/Errors/methods