I am new to scala and type-safe languages so I could be overlooking something basic. That said here's my problem.
Goal: I want to submit a form that is just one text input, and does not mirror my case class. It is going to end up as type: String
Issue: Can't get into success from fold
I have a form on the frontend that I opted to write in html instead of play's form helpers (willing to change if this is the issue)
<form method="POST" action="@routes.Application.shorten()">
<input id="urlLong" name="urlLong" type="text" placeholder="http://www.google.com/" class="span4"/>
<div class="form-actions">
<button type="reset" class="btn">Reset</button>
<button type="submit" class="btn btn-primary pull-right"><span class="icon-random"></span> Shrtn It!</button>
</div>
</form>
The controller that is handling the post action looks like this:
import ...
object Application extends Controller {
val newUrlForm = Form(
"urlLong" -> text
)
def shorten = Action { implicit request =>
val urlLong = newUrlForm.bindFromRequest.get
newUrlForm.fold(
hasErrors = { form =>
val message = "Somethings gone terribly wrong"
Redirect(routes.Application.dash()).flashing("error" -> message)
},
success = { form =>
val message = "Woot it was successfully added!"
Redirect(routes.Application.dash()).flashing("success" -> message)
}
}
...
}
I was trying to follow / modify the tutorial in the Play for Scala book, but they match their form to a case class, and Play's tutorial is a bit off from my use case as well. Along with your answer if you could include how you figured it out, that would be really useful so I can troubleshoot on my own better.
- http://www.playframework.com/documentation/2.0/ScalaForms
- http://www.playframework.com/documentation/api/2.0/scala/index.html#play.api.data.Forms$
Also if it matters I am using intellij idea as my ide