Class DS.Errors
Holds validation errors for a given record organized by attribute names.
Every DS.Model has an errors property that is an instance of
DS.Errors. This can be used to display validation error
messages returned from the server when a record.save() rejects.
This works automatically with DS.ActiveModelAdapter, but you
can implement ajaxError
in other adapters as well.
For Example, if you had an User model that looked like this:
import DS from 'ember-data';
export default DS.Model.extend({
username: attr('string'),
email: attr('string')
});
And you attempted to save a record that did not validate on the backend.
var user = store.createRecord('user', {
username: 'tomster',
email: 'invalidEmail'
});
user.save();
Your backend data store might return a response that looks like this. This response will be used to populate the error object.
{
"errors": {
"username": ["This username is already taken!"],
"email": ["Doesn't look like a valid email."]
}
}
Errors can be displayed to the user by accessing their property name to get an array of all the error objects for that property. Each error object is a JavaScript object with two keys:
messageA string containing the error message from the backendattributeThe name of the property associated with this error message
<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}}Methods
add (attribute, messages)
Defined in packages/ember-data/lib/system/model/errors.js:198
- attribute
- String
- messages
- (Array|String)
Adds error messages to a given attribute and sends
becameInvalid event to the record.
Example:
if (!user.get('username') {
user.get('errors').add('username', 'This field is required');
}clear
Defined in packages/ember-data/lib/system/model/errors.js:291
Removes all error messages and sends becameValid event
to the record.
Example:
import Ember from 'ember';
export default Ember.Route.extend({
actions: {
retrySave: function(user) {
user.get('errors').clear();
user.save();
}
}
});
errorsFor (attribute) : Array
Defined in packages/ember-data/lib/system/model/errors.js:125
- attribute
- String
- returns
- Array
Returns errors for a given attribute
var 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
Defined in packages/ember-data/lib/system/model/errors.js:332
- attribute
- String
- returns
- Boolean
true if there some errors on given attribute
Checks if there is error messages for the given attribute.
import Ember from 'ember';
export default Ember.Route.extend({
actions: {
save: function(user) {
if (user.get('errors').has('email')) {
return alert('Please update your email before attempting to save.');
}
user.save();
}
}
});
registerHandlers (target, becameInvalid, becameValid)
Defined in packages/ember-data/lib/system/model/errors.js:99
- target
- Object
- becameInvalid
- Function
- becameValid
- Function
Register with target handler
remove (attribute)
Defined in packages/ember-data/lib/system/model/errors.js:243
- attribute
- String
Removes all error messages from the given attribute and sends
becameValid event to the record if there no more errors left.
Example:
import DS from 'ember-data';
export default DS.Model.extend({
email: DS.attr('string'),
twoFactorAuth: DS.attr('boolean'),
phone: DS.attr('string')
});
import Ember from 'ember';
export default Ember.Route.extend({
actions: {
save: function(user) {
if (!user.get('twoFactorAuth')) {
user.get('errors').remove('phone');
}
user.save();
}
}
});
Properties
isEmpty
Defined in packages/ember-data/lib/system/model/errors.js:191
length
Defined in packages/ember-data/lib/system/model/errors.js:183
Total number of errors.
messages
Defined in packages/ember-data/lib/system/model/errors.js:147
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}}