Page MenuHomeSealhub

form.ts
No OneTemporary

import Prettier from "prettier";
import { curryImportPath } from "../utils/import-path.js";
import { getPrettierConfig } from "../utils/prettier.js";
export async function formTemplate(
action_name: string,
newfilefullpath: string
): Promise<string> {
const rel = curryImportPath(newfilefullpath);
const content = `import type { Context } from "koa";
import type { FormData, FormDataValue } from "@sealcode/sealgen";
import { Form, Fields, Controls, fieldsToShape } from "@sealcode/sealgen";
import html from "${rel("src/back/html.js")}";
import { Users } from "${rel("src/back/collections/collections.js")}";
export const actionName = "${action_name}";
const fields = {
email: new Fields.CollectionField(true, Users.fields.email),
password: new Fields.SimpleFormField(true),
};
export const ${action_name}Shape = fieldsToShape(fields);
export default new (class ${action_name}Form extends Form<typeof fields, void> {
defaultSuccessMessage = "Formularz wypełniony poprawnie";
fields = fields;
controls = [
new Controls.SimpleInput(fields.email, { label: "Email:", type: "email" }),
new Controls.SimpleInput(fields.password, {
label: "Password:",
type: "password",
}),
];
async validateValues(
ctx: Context,
data: Record<string, FormDataValue>
): Promise<{ valid: boolean; error: string }> {
// just a silly example, please remove this with better logic
const { parsed: email } = await this.fields.email.getValue(ctx, data);
const { parsed: password } = await this.fields.password.getValue(ctx, data);
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
if ((password || "").length > ((email as string) || "").length) {
return { valid: false, error: "Password cannot be longer than email" };
} else {
return { valid: true, error: "" };
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async canAccess(_: Context) {
return { canAccess: true, message: "" };
}
async onSubmit() {
//noop
return;
}
async render(ctx: Context, data: FormData, show_field_errors: boolean) {
return html(ctx, "${action_name}", await super.render(ctx, data, show_field_errors));
}
})();
`;
return Prettier.format(content, await getPrettierConfig());
}

File Metadata

Mime Type
text/x-java
Expires
Wed, May 7, 19:45 (1 d, 6 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
625602
Default Alt Text
form.ts (2 KB)

Event Timeline