2021-01-26 09:05:10 +01:00
|
|
|
import Component from "@ember/component";
|
2021-02-16 01:43:00 +01:00
|
|
|
import { equal } from "@ember/object/computed";
|
2024-05-02 14:57:56 +02:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2021-01-26 09:05:10 +01:00
|
|
|
|
|
|
|
export default Component.extend({
|
2021-03-28 11:06:49 +02:00
|
|
|
classNames: ["validator"],
|
2021-02-15 09:19:14 +01:00
|
|
|
classNameBindings: ["isValid", "isInvalid"],
|
|
|
|
validMessageKey: null,
|
|
|
|
invalidMessageKey: null,
|
|
|
|
isValid: null,
|
2021-02-16 01:43:00 +01:00
|
|
|
isInvalid: equal("isValid", false),
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2021-02-15 09:19:14 +01:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
2021-02-01 14:58:37 +01:00
|
|
|
|
2021-02-15 09:19:14 +01:00
|
|
|
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) => {
|
|
|
|
return ajax("/realtime-validations", {
|
|
|
|
data: {
|
2021-02-16 01:43:00 +01:00
|
|
|
type: this.get("type"),
|
2021-02-15 09:19:14 +01:00
|
|
|
...params,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
2021-02-01 14:58:37 +01:00
|
|
|
|
2021-02-15 09:19:14 +01:00
|
|
|
didInsertElement() {
|
2024-11-22 17:11:51 +01:00
|
|
|
this._super(...arguments);
|
2021-02-15 09:19:14 +01:00
|
|
|
this.appEvents.on("custom-wizard:validate", this, this.checkIsValid);
|
|
|
|
},
|
2021-01-30 18:46:04 +01:00
|
|
|
|
2021-02-15 09:19:14 +01:00
|
|
|
willDestroyElement() {
|
2024-11-22 17:11:51 +01:00
|
|
|
this._super(...arguments);
|
2021-02-15 09:19:14 +01:00
|
|
|
this.appEvents.off("custom-wizard:validate", this, this.checkIsValid);
|
|
|
|
},
|
2021-02-01 14:58:37 +01:00
|
|
|
|
2021-02-15 09:19:14 +01:00
|
|
|
checkIsValid() {
|
|
|
|
this.set("isValid", this.validate());
|
|
|
|
},
|
|
|
|
});
|