Andriy Drozdyuk
Andriy Drozdyuk

Reputation: 61121

Lift error-check multiple form fields

I'm following a tutorial on how to validate form fields, but it only demonstrates it for one field. How can I validate, and display errors for multiple fields?

I tried the following - but it always succeeds and does a redirect - no matter the errors:

def process() = {
    if (patientName == "Joe") {
      S.error("patientName", "Joe not allowed!")
    }
    if (birthdate == "22/22/2222") {
      S.error("birthdate", "Invalid date!")
    }
    S.notice("Success! You entered Patient name: " + patientName); S.redirectTo("/")    
}

Upvotes: 0

Views: 306

Answers (1)

Andriy Drozdyuk
Andriy Drozdyuk

Reputation: 61121

Ha! I figured it out. Beautiful.

 def process() = {
    if (patientName == "Joe") {
      S.error("Joe not allowed!")
    }
    if (birthdate == "22/22/2222") {
      S.error("birthdate", "Invalid birthdate!")
    }

    S.errors match {
        case Nil =>S.notice("Patient name: " + patientName); S.redirectTo("/")
        case _ =>
    }
  }

Upvotes: 1

Related Questions