show validation message
This commit is contained in:
parent
a546fb6fe0
commit
bdd279c4f8
@ -12,6 +12,7 @@ import { NotifyController } from "@/controllers/notify";
|
|||||||
import { type APIUser } from "@/index";
|
import { type APIUser } from "@/index";
|
||||||
import { isApiError } from "@/utils/api";
|
import { isApiError } from "@/utils/api";
|
||||||
import type { AuthState } from "@/utils/AuthService";
|
import type { AuthState } from "@/utils/AuthService";
|
||||||
|
import { maxLengthValidator } from "@/utils/form";
|
||||||
import { AppStateService } from "@/utils/state";
|
import { AppStateService } from "@/utils/state";
|
||||||
import { formatAPIUser } from "@/utils/user";
|
import { formatAPIUser } from "@/utils/user";
|
||||||
|
|
||||||
@ -45,6 +46,8 @@ export class OrgForm extends TailwindElement {
|
|||||||
readonly _api = new APIController(this);
|
readonly _api = new APIController(this);
|
||||||
readonly _notify = new NotifyController(this);
|
readonly _notify = new NotifyController(this);
|
||||||
|
|
||||||
|
private readonly validateOrgNameMax = maxLengthValidator(40);
|
||||||
|
|
||||||
readonly _renameOrgTask = new Task(this, {
|
readonly _renameOrgTask = new Task(this, {
|
||||||
autoRun: false,
|
autoRun: false,
|
||||||
task: async ([id, name, slug]) => {
|
task: async ([id, name, slug]) => {
|
||||||
@ -72,9 +75,9 @@ export class OrgForm extends TailwindElement {
|
|||||||
autocomplete="off"
|
autocomplete="off"
|
||||||
value=${this.name === this.orgId ? "" : this.name}
|
value=${this.name === this.orgId ? "" : this.name}
|
||||||
minlength="2"
|
minlength="2"
|
||||||
maxlength="40"
|
|
||||||
help-text=${msg("You can change this in your org settings later.")}
|
help-text=${msg("You can change this in your org settings later.")}
|
||||||
required
|
required
|
||||||
|
@sl-input=${this.validateOrgNameMax.validate}
|
||||||
></sl-input>
|
></sl-input>
|
||||||
</div>
|
</div>
|
||||||
<div class="mb-5">
|
<div class="mb-5">
|
||||||
|
|||||||
@ -39,17 +39,27 @@ export function getHelpText(maxLength: number, currentLength: number) {
|
|||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
export function maxLengthValidator(maxLength: number): MaxLengthValidator {
|
export function maxLengthValidator(maxLength: number): MaxLengthValidator {
|
||||||
const helpText = msg(str`Maximum ${maxLength} characters`);
|
const validityHelpText = msg(str`Maximum ${maxLength} characters`);
|
||||||
|
let origHelpText: null | string = null;
|
||||||
|
|
||||||
const validate = (e: CustomEvent) => {
|
const validate = (e: CustomEvent) => {
|
||||||
const el = e.target as SlTextarea | SlInput;
|
const el = e.target as SlTextarea | SlInput;
|
||||||
const helpText = getHelpText(maxLength, el.value.length);
|
|
||||||
|
if (origHelpText === null && el.helpText) {
|
||||||
|
origHelpText = el.helpText;
|
||||||
|
}
|
||||||
|
|
||||||
|
const validityText = getHelpText(maxLength, el.value.length);
|
||||||
|
const isInvalid = el.value.length > maxLength;
|
||||||
|
|
||||||
el.setCustomValidity(
|
el.setCustomValidity(
|
||||||
el.value.length > maxLength
|
isInvalid
|
||||||
? msg(str`Please shorten this text to ${maxLength} or less characters.`)
|
? msg(str`Please shorten this text to ${maxLength} or less characters.`)
|
||||||
: "",
|
: "",
|
||||||
);
|
);
|
||||||
el.helpText = helpText;
|
|
||||||
|
el.helpText = isInvalid ? validityText : origHelpText || validityHelpText;
|
||||||
};
|
};
|
||||||
|
|
||||||
return { helpText, validate };
|
return { helpText: validityHelpText, validate };
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user