Handling Form data

Form data can be extracted with Request.current.bodyForm[MyData].
The MyData needs to have a FormDataRW given instance.

Create a file form_handling.sc and paste this code into it:

    //> using scala "3.4.0"
//> using dep ba.sake::sharaf:0.4.0

import io.undertow.Undertow
import scalatags.Text.all.*
import ba.sake.formson.FormDataRW
import ba.sake.hepek.html.HtmlPage
import ba.sake.sharaf.*, routing.*

object ContacUsView extends HtmlPage:
  override def bodyContent =
    form(action := "/handle-form", method := "POST")(
      div(
        label("Full Name: ", input(name := "fullName", autofocus))
      ),
      div(
        label("Email: ", input(name := "email", tpe := "email"))
      ),
      input(tpe := "Submit")
    )

case class ContactUsForm(fullName: String, email: String) derives FormDataRW

val routes = Routes:
  case GET() -> Path() =>
    Response.withBody(ContacUsView)

  case POST() -> Path("handle-form") =>
    val formData = Request.current.bodyForm[ContactUsForm]
    Response.withBody(s"Got form data: ${formData}")

Undertow.builder
  .addHttpListener(8181, "localhost")
  .setHandler(SharafHandler(routes))
  .build
  .start()

println(s"Server started at http://localhost:8181")

Then run it like this:

scala-cli form_handling.sc 

Now go to http://localhost:8181 and fill in the page with some data.

When you click the "Submit" button you will see a response like this:

Got form data: ContactUsForm(Bob,bob@example.com)