2017-08-22 10:53:26 +03:00
|
|
|
import Mixin from '@ember/object/mixin';
|
|
|
|
import {A as emberA} from '@ember/array';
|
|
|
|
import {isEmpty} from '@ember/utils';
|
2020-05-06 20:06:42 +03:00
|
|
|
// eslint-disable-next-line ghost/ember/no-observers
|
2017-08-22 10:53:26 +03:00
|
|
|
import {observer} from '@ember/object';
|
2019-07-01 17:53:58 +03:00
|
|
|
import {on} from '@ember/object/evented';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {run} from '@ember/runloop';
|
2015-10-28 14:36:45 +03:00
|
|
|
|
2022-12-08 20:04:20 +03:00
|
|
|
/**
|
|
|
|
* Adds `success` or `error` classes to the element based on the passed
|
|
|
|
* in `DS.Errors` object, the `property` to inspect, and an array of
|
|
|
|
* validated property names in `hasValidated`
|
|
|
|
*/
|
2015-10-28 14:36:45 +03:00
|
|
|
export default Mixin.create({
|
2015-09-16 20:02:06 +03:00
|
|
|
|
|
|
|
errors: null,
|
|
|
|
property: '',
|
2015-10-28 14:36:45 +03:00
|
|
|
hasValidated: emberA(),
|
2015-09-16 20:02:06 +03:00
|
|
|
|
2017-01-31 22:27:11 +03:00
|
|
|
hasError: false,
|
|
|
|
|
|
|
|
setHasError() {
|
2019-03-06 16:53:54 +03:00
|
|
|
let property = this.property;
|
|
|
|
let errors = this.errors;
|
|
|
|
let hasValidated = this.hasValidated;
|
2015-09-16 20:02:06 +03:00
|
|
|
|
|
|
|
// if we aren't looking at a specific property we always want an error class
|
2017-01-31 22:27:11 +03:00
|
|
|
if (!property && errors && !errors.get('isEmpty')) {
|
|
|
|
this.set('hasError', true);
|
|
|
|
return;
|
2015-09-16 20:02:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we haven't yet validated this field, there is no validation class needed
|
2016-09-24 18:48:06 +03:00
|
|
|
if (!hasValidated || !hasValidated.includes(property)) {
|
2017-01-31 22:27:11 +03:00
|
|
|
this.set('hasError', false);
|
|
|
|
return;
|
2015-09-16 20:02:06 +03:00
|
|
|
}
|
|
|
|
|
2017-01-31 22:27:11 +03:00
|
|
|
if (errors && !isEmpty(errors.errorsFor(property))) {
|
|
|
|
this.set('hasError', true);
|
|
|
|
return;
|
2015-09-16 20:02:06 +03:00
|
|
|
}
|
|
|
|
|
2017-01-31 22:27:11 +03:00
|
|
|
this.set('hasError', false);
|
|
|
|
},
|
|
|
|
|
2018-01-11 17:16:42 +03:00
|
|
|
// eslint-disable-next-line ghost/ember/no-observers
|
2019-07-01 17:53:58 +03:00
|
|
|
hasErrorObserver: on('init', observer('errors.[]', 'property', 'hasValidated.[]', function () {
|
2017-01-31 22:27:11 +03:00
|
|
|
run.once(this, 'setHasError');
|
2019-07-01 17:53:58 +03:00
|
|
|
}))
|
2015-09-16 20:02:06 +03:00
|
|
|
|
|
|
|
});
|