Skip to content

Form handling - binding, validation & render #185

@vim89

Description

@vim89

Problem

Cask has no form handling utilities. Users must manually:

  • Render forms with scalatags
  • Parse form data
  • Validate input (manual if-else chains)
  • Display errors (manual)
// Current - everything manual
@cask.get("/users/new")
def newUser() = {
  import scalatags.Text.all._
  html(
    body(
      form(action := "/users", method := "post")(
        input(name := "name", `type` := "text"),
        input(name := "email", `type` := "email"),
        button("Create User")
      )
    )
  ).render
}

@cask.post("/users")
def create(name: String, email: String) = {
  // Manual validation
  if (name.isEmpty) cask.Abort(400, "Name required")
  if (!email.contains("@")) cask.Abort(400, "Invalid email")

  User.create(name, email)
  cask.Redirect("/users")
}
  • Repetitive validation code
  • No validation, form handling, error display

Solution

binding, simple validation, rendering with scalatags.

  • form data binding to scalatags
  • simple validations - throw Throwables
  • Scalatags from functions

Questions -

  • What form helpers are essential (text, email, password, select, etc.)?
  • repopulation after errors?
  • Multi-step forms
  • No field-level error tracking

References

  • scalatags for rendering
  • uPickle for data binding

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions