Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F22393847
form.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
2 KB
Referenced Files
None
Subscribers
None
form.ts
View Options
import
{
formatWithPrettier
}
from
"../utils/prettier.js"
;
const
defaults
=
{
postimport
:
`
import { Fields } from "@sealcode/sealgen";
import type {FormDataValue} from "@sealcode/sealgen";`
,
get_fields
:
`()=>(<const>{ password: new Fields.Password(true) })`
,
get_controls
:
`
(fields: ReturnType<typeof getFields>)=>[
fields.password?.getControl()
]`
,
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.getParsedValue(ctx, data);
const { parsed: password } = await this.fields.password.getParsedValue(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: "", css_clumps: ["admin-forms"], body: await super.render(ctx, data, show_field_errors)});
}`
,
get_initial_values
:
""
,
other_methods
:
""
,
success_message
:
"Success!"
,
};
export
async
function
formTemplate
(
action_name
:
string
,
_params
:
Partial
<
typeof
defaults
>
=
defaults
)
:
Promise
<
string
>
{
const
params
=
{
...
defaults
,
...
_params
};
const
content
=
`import type { Context } from "koa";
import type { FormData, BreadcrumbLabel } from "@sealcode/sealgen";
import { Form, Controls } from "@sealcode/sealgen";
import html from "src/back/html.js";
${
params
.
postimport
}
export const actionName = "
${
action_name
}
";
export const breadcrumbLabel: BreadcrumbLabel = "
${
action_name
}
";
const getFields =
${
params
.
get_fields
}
;
export default new (class
${
action_name
}
Form extends Form<ReturnType<typeof getFields>, void> {
defaultSuccessMessage = "Formularz wypełniony poprawnie";
getFields = getFields;
getControls =
${
params
.
get_controls
}
;
${
params
.
other_methods
}
${
params
.
get_initial_values
}
${
params
.
onsubmit
}
${
params
.
can_access
}
${
params
.
render
}
})();
`
;
return
formatWithPrettier
(
content
);
}
File Metadata
Details
Attached
Mime Type
application/javascript
Expires
Fri, Nov 27, 16:10 (1 d, 16 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1199287
Default Alt Text
form.ts (2 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline