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';
|
|
|
|
import {observer} from '@ember/object';
|
|
|
|
import {run} from '@ember/runloop';
|
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() {
|
2015-10-28 14:36:45 +03:00
|
|
|
let property = this.get('property');
|
|
|
|
let errors = this.get('errors');
|
|
|
|
let hasValidated = this.get('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
|
2017-01-31 22:27:11 +03:00
|
|
|
hasErrorObserver: observer('errors.[]', 'property', 'hasValidated.[]', function () {
|
|
|
|
run.once(this, 'setHasError');
|
|
|
|
// this.setHasError();
|
|
|
|
}).on('init')
|
2015-09-16 20:02:06 +03:00
|
|
|
|
|
|
|
});
|