home
  • Blog
  • Home
  • Projects
    • Ember
    • EmberData
    • Ember CLI
4.12
  • Packages
    • @ember-data/adapter
    • @ember-data/adapter/error
    • @ember-data/adapter/json-api
    • @ember-data/adapter/rest
    • @ember-data/canary-features
    • @ember-data/debug
    • @ember-data/deprecations
    • @ember-data/experimental-preview-types
    • @ember-data/graph
    • @ember-data/json-api
    • @ember-data/legacy-compat
    • @ember-data/model
    • @ember-data/request
    • @ember-data/request/fetch
    • @ember-data/serializer
    • @ember-data/serializer/json
    • @ember-data/serializer/json-api
    • @ember-data/serializer/rest
    • @ember-data/store
    • @ember-data/tracking
  • Classes
    • <Interface> Adapter
    • <Interface> Cache
    • <Interface> Handler
    • <Interface> Serializer
    • AbortError
    • Adapter
    • AdapterError
    • BelongsToReference
    • BooleanTransform
    • BuildURLMixin
    • Cache
    • CacheManager
    • CacheStoreWrapper
    • CanaryFeatureFlags
    • ConflictError
    • CurrentDeprecations
    • DateTransform
    • DebugLogging
    • EmbeddedRecordsMixin
    • Errors
    • Fetch
    • ForbiddenError
    • Future
    • HasManyReference
    • IdentifierCache
    • InvalidError
    • JSONAPIAdapter
    • JSONAPISerializer
    • JSONSerializer
    • ManyArray
    • Model
    • NotFoundError
    • NotificationManager
    • NumberTransform
    • PromiseArray
    • PromiseManyArray
    • PromiseObject
    • RESTAdapter
    • RESTSerializer
    • RecordArray
    • RecordReference
    • RequestManager
    • RequestStateService
    • SchemaService
    • Serializer
    • ServerError
    • Snapshot
    • SnapshotRecordArray
    • StableRecordIdentifier
    • Store
    • StringTransform
    • TimeoutError
    • Transform
    • UnauthorizedError

Class Errors public


Extends: Ember.ArrayProxy
Defined in: ../model/src/-private/errors.ts:30
Module: @ember-data/model

Holds validation errors for a given record, organized by attribute names.

This class is not directly instantiable.

Every Model has an errors property that is an instance of Errors. This can be used to display validation error messages returned from the server when a record.save() rejects.

For Example, if you had a User model that looked like this:

```js {data-filename=app/models/user.js} import Model, { attr } from '@ember-data/model';

export default class UserModel extends Model {


Methods

add (attribute, messages) public

Module: @ember-data/model

Defined in ../packages/model/src/-private/errors.ts:216

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 = 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('username');
 // =>
 // [
 //   { attribute: 'username', message: 'This field is required' },
 // ]

clear public

Module: @ember-data/model

Defined in ../packages/model/src/-private/errors.ts:339

Manually clears all errors for the record. This will transition the record into a valid state, and will trigger the becameValid event and lifecycle method.

Example:

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

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

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

errors.clear();

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

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

errors.messages
// => []

errorsFor (attribute) : Array public

Module: @ember-data/model

Defined in ../packages/model/src/-private/errors.ts:117

attribute
String
returns
Array

Returns errors for a given attribute

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

has (attribute) : Boolean public

Module: @ember-data/model

Defined in ../packages/model/src/-private/errors.ts:399

attribute
String
returns
Boolean

true if there some errors on given attribute

Checks if there are error messages for the given attribute.

```js {data-filename=app/controllers/user/edit.js} import Controller from '@ember/controller'; import { action } from '@ember/object';

export default class UserEditController extends Controller {

remove (member) public

Module: @ember-data/model

Defined in ../packages/model/src/-private/errors.ts:288

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 = 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

Properties

isEmpty public

Module: @ember-data/model

Defined in ../packages/model/src/-private/errors.ts:205

true if we have no errors.

length public

Module: @ember-data/model

Defined in ../packages/model/src/-private/errors.ts:196

Total number of errors.

messages public

Module: @ember-data/model

Defined in ../packages/model/src/-private/errors.ts:155

An array containing all of the error messages for this record. This is useful for displaying all errors to the user.

{{#each @model.errors.messages as |message|}}
  <div class="error">
    {{message}}
  </div>
{{/each}}
On this page


Methods

  • add
  • clear
  • errorsFor
  • has
  • remove

Properties

  • isEmpty
  • length
  • messages
Team Sponsors Security Legal Branding Community Guidelines
Twitter GitHub Discord Mastodon

If you want help you can contact us by email, open an issue, or get realtime help by joining the Ember Discord.

© Copyright 2025 - Tilde Inc.
Ember.js is free, open source and always will be.


Ember is generously supported by
blue Created with Sketch.