2021-01-26 09:05:10 +01:00
|
|
|
import Component from "@ember/component";
|
|
|
|
import { not } from "@ember/object/computed";
|
2021-02-01 14:58:37 +01:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
import { getToken } from "wizard/lib/ajax";
|
2021-01-26 09:05:10 +01:00
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
classNameBindings: ['isValid', 'isInvalid'],
|
|
|
|
validMessageKey: null,
|
|
|
|
invalidMessageKey: null,
|
|
|
|
isValid: null,
|
|
|
|
isInvalid: not('isValid'),
|
|
|
|
layoutName: 'components/validator', // useful for sharing the template with extending components
|
2021-02-01 14:58:37 +01:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
if (this.get('validation.backend')) {
|
|
|
|
// set a function that can be called as often as it need to
|
|
|
|
// from the derived component
|
|
|
|
this.backendValidate = (params) => {
|
2021-02-11 14:54:32 +01:00
|
|
|
return ajax('/realtime-validations', {
|
2021-02-01 14:58:37 +01:00
|
|
|
data: {
|
|
|
|
validation: this.get('name'),
|
|
|
|
authenticity_token: getToken(),
|
|
|
|
...params
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-01-30 18:46:04 +01:00
|
|
|
didInsertElement() {
|
2021-02-01 14:58:37 +01:00
|
|
|
this.appEvents.on('custom-wizard:validate', this, this.checkIsValid);
|
2021-01-26 09:05:10 +01:00
|
|
|
},
|
2021-01-30 18:46:04 +01:00
|
|
|
|
|
|
|
willDestroyElement() {
|
2021-02-01 14:58:37 +01:00
|
|
|
this.appEvents.off('custom-wizard:validate', this, this.checkIsValid);
|
|
|
|
},
|
|
|
|
|
|
|
|
checkIsValid() {
|
|
|
|
this.set('isValid', this.validate());
|
2021-01-30 18:46:04 +01:00
|
|
|
}
|
2021-01-26 09:05:10 +01:00
|
|
|
});
|