Page MenuHomeSealhub

form.ts
No OneTemporary

import Prettier from "prettier";
import { curryImportPath } from "../utils/import-path.js";
import { getPrettierConfig } from "../utils/prettier.js";
const defaults = {
postimport: `
import { Fields } from "@sealcode/sealgen";
import type {FormDataValue} from "@sealcode/sealgen";`,
fields: `
password: new Fields.SimpleFormField(true),`,
controls: `
new Controls.SimpleInput(fields.password, {
label: "Password:",
type: "password",
}),`,
validate_method: `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: "" };
}
}`,
onsubmit: ` async onSubmit() {
//noop
return;
}`,
can_access: ` // eslint-disable-next-line @typescript-eslint/no-unused-vars
async canAccess(_: Context) {
return { canAccess: true, message: "" };
}`,
render: `async render(ctx: Context, data: FormData, show_field_errors: boolean) {
return html({ctx, title: "Form", description: "", body: await super.render(ctx, data, show_field_errors)});
}`,
get_initial_values: "",
other_methods: "",
};
export async function formTemplate(
action_name: string,
newfilefullpath: string,
_params: Partial<typeof defaults> = defaults
): Promise<string> {
const params = { ...defaults, ..._params };
const rel = curryImportPath(newfilefullpath);
const content = `import type { Context } from "koa";
import type { FormData } from "@sealcode/sealgen";
import { Form, Controls, fieldsToShape } from "@sealcode/sealgen";
import html from "${rel("src/back/html.js")}";
${params.postimport}
export const actionName = "${action_name}";
const fields = {
${params.fields}
};
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 = [
${params.controls}
];
${params.other_methods}
${params.get_initial_values}
${params.onsubmit}
${params.can_access}
${params.render}
})();
`;
return Prettier.format(content, await getPrettierConfig());
}

File Metadata

Mime Type
text/x-java
Expires
Mon, Dec 23, 00:57 (4 h, 7 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
556740
Default Alt Text
form.ts (2 KB)

Event Timeline