2016-06-30 13:21:47 +03:00
|
|
|
import Mixin from 'ember-metal/mixin';
|
|
|
|
import {isEmpty} from 'ember-utils';
|
|
|
|
import {A as emberA} from 'ember-array/utils';
|
2017-01-31 22:27:11 +03:00
|
|
|
import observer from 'ember-metal/observer';
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
|
|
|
hasErrorObserver: observer('errors.[]', 'property', 'hasValidated.[]', function () {
|
|
|
|
run.once(this, 'setHasError');
|
|
|
|
// this.setHasError();
|
|
|
|
}).on('init')
|
2015-09-16 20:02:06 +03:00
|
|
|
|
|
|
|
});
|