Example: Displaying Default Values

CruddyForms makes it easy to pre-fill one or more of the fields in your form.
    1
  • Define the schema using Typebox

        // ~/examples/basic/schema.ts
    import {Type} from "@sinclair/typebox";
    import type {FormInputCheckbox, FormInputText, FormTextArea} from "cruddy-forms";
    
    export const UsernameSchema = Type.Object( {
      username: Type.RegExp(
        /^\S*$/, // No whitespace
          {
            element: "input",
            hint: "Username may not contain spaces",
            inputType: "text",
            maxLength: 8,
            placeholder: "your username",
          } satisfies FormInputText,
        ),
    } );
    
    const EmailSchema = Type.Object( {
      email: Type.RegExp(
        /.+@example\.com/i,
          {
            element: "input",
            hint: "Must end in @example.com",
            inputType: "email",
            placeholder: "[email protected]",
          } satisfies FormInputText,
        ),
    } );
    
    const PASS_MIN = 6;
    const PasswordSchema = Type.Object( {
      password: Type.String( {
        element: "input",
        hint: `Use ${PASS_MIN} or more characters`,
        inputType: "password",
        minLength: PASS_MIN,
        placeholder: "your password",
        } satisfies FormInputText ),
    } );
    
    const CommentSchema = Type.Object( {
        comment: Type.String( {
            element: "textarea",
            inputType: "text",
            maxLength: 50,
            minLength: 10,
            rows: 5,
            } satisfies FormTextArea )
    } );
    
    const SignupSchema = Type.Object( {
        signup: Type.Boolean( {
            checked: false,
            element: "input",
            inputType: "checkbox",
            label: "Add me to the list!",
        } satisfies FormInputCheckbox ),
    
    } );
    
    export const Schema = Type.Composite( [
      UsernameSchema, EmailSchema, PasswordSchema, SignupSchema, CommentSchema
    ] );
      
    ts
    2
  • Generate the Form HTML

    Here we save the data submitted by the user and pass it to getHTML(), which will pre-populate these values in the form.

        // ~/examples/defaults/form.ts
    import {Form} from "cruddy-forms";
    import {Schema} from "~/examples/basic/schema.ts";
    
    export function getHTML(data?: FormData) {
      const form = new Form(Schema, "Submit", {action: "#form"});
      let defaults = {}
      if (data) {
        form.validate(data);
        defaults = {email: data.get("email"), username: data.get("username")}
      }
      return form.getHTML(defaults);
    }
      
    ts
  • 3
  • Serve the Form HTML and CSS

    The details of this step will depend on your chosen server-side rendering framework. Include the generated Form HTML from Step 2 in your server response, along with the normform.css file.
  • 4
  • Resulting Form (Try it!)

    Submit some data through the form and you'll see that it appears as default values when the page reloads.

    Username may not contain spaces
    Use 6 or more characters