Class Errors
Holds validation errors for a given record, organized by attribute names.
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:
app/models/user.js | |
1 2 3 4 5 6 |
import Model, { attr } from '@ember-data/model'; export default Model.extend({ username: attr('string'), email: attr('string') }); |
And you attempted to save a record that did not validate on the backend:
1 2 3 4 5 |
let user = store.createRecord('user', { username: 'tomster', email: 'invalidEmail' }); user.save(); |
Your backend would be expected to return an error response that described the problem, so that error messages can be generated on the app.
API responses will be translated into instances of Errors
differently,
depending on the specific combination of adapter and serializer used. You
may want to check the documentation or the source code of the libraries
that you are using, to know how they expect errors to be communicated.
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:
message
A string containing the error message from the backendattribute
The name of the property associated with this error message
You can also access the special messages
property on the error
object to get an array of all the error strings.
add (attribute, messages)
Defined in ../model/addon/-private/errors.js:199
- 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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
Defined in ../model/addon/-private/errors.js:109
- attribute
- String
- returns
- Array
Returns errors for a given attribute
1 2 3 4 5 6 7 8 |
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
Defined in ../model/addon/-private/errors.js:435
- attribute
- String
- returns
- Boolean
- true if there some errors on given attribute
Checks if there are error messages for the given attribute.
app/routes/user/edit.js | |
1 2 3 4 5 6 7 8 9 10 11 12 |
import Route from '@ember/routing/route'; export default Route.extend({ actions: { save: function(user) { if (user.get('errors').has('email')) { return alert('Please update your email before attempting to save.'); } user.save(); } } }); |
remove (member)
Defined in ../model/addon/-private/errors.js: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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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 |