email-dns-tools/assets/scripts/fields/EnumField.js
2026-01-14 15:46:37 +01:00

40 lines
1.0 KiB
JavaScript

import { Field } from "./Field.js";
import { ValidationError } from "../ValidationError.js";
export class EnumField extends Field {
separator = "=";
constructor(key, values) {
super(key);
this.values = values;
this.optionLabels = values;
}
validate(value) {
if (this.values.includes(value))
return true;
throw new ValidationError(`Invalid value for field "${this.key}" - must be one of: ${this.values.join(", ")}`);
}
getInputHtml() {
return `<select id="${this.id}" name="${this.key}" ${this.isRequired ? "required" : ""}>` +
(this.isRequired || this.defaultValue ? "" : `<option value="" selected>&lt;not set&gt;</option>`) +
this.values.map((value, i) =>
`<option value="${value}" ${this.defaultValue === value ? "selected" : ""}>
${this.optionLabels[i] + (this.defaultValue === value ? " (Default)" : "")}
</option>`
) +
`</select>`;
}
getInputValue() {
return document.getElementById(this.id).value;
}
options(options) {
this.optionLabels = options;
return this;
}
}