Reputation: 938
Im new to Play 2 and Scala, and im getting a strange Exception in my template:
Execution exception
-------------------
[NoSuchElementException: None.get]
In /home/nic/workspaces/scala-ide/scims/app/views/persons/detailTabs/personal.scala.html at line 4.
1. @(personId: Long, personDetailTabForm: Form[dto.PersonDetailTab])(implicit formOptions: dto.PersonFormOptions)
2. @implicitFieldConstructor = @{ helper.FieldConstructor(support.bs3HorizField.f) }
3.
4. @persons.detail("personal", personDetailTabForm.get.firstName) {
The personDetailTabForm is an empty form object defined as:
val personalDetailTabForm: Form[PersonDetailTab] = Form(
mapping(
"firstName" -> text.verifying(nonEmpty),
"middleName" -> text,
"lastName" -> text.verifying(nonEmpty),
"gender" -> text,
"dateOfBirth" -> jodaDate("yyyy-MM-dd"),
"ethnicity" -> text,
"maritalStatus" -> text,
"password" -> text
)(PersonDetailTab.apply)(PersonDetailTab.unapply)
)
Any ideas as to what's wrong here?
I was under the impression a variable would have to be an Option to get a None?
Cheers NFV
Upvotes: 4
Views: 3215
Reputation: 16245
You are calling get
on personDetailTabForm
- Looking up it's ScalaDoc: http://www.playframework.com/documentation/2.2.x/api/scala/index.html#play.api.data.Form - it seems that .get
returns the PersonDetailTab
value that the form holds - IF, as the docs say, 'the submission was a success'.
You're seeing the None.get
exception because most likely play.api.data.Form[T]
simply uses Option[T]
and get
returns Some[T]
when the form holds a valid value and None
otherwise.
So on your line 4, in the scala template, you have something like
personDetailTabForm.get.firstName
That's a String
, but you can expect a value only when the form's underlying PersonDetailTab
itself has a value. I am not sure what you want to do, but you're dealing with a case where a value you want to render in a template might not be there, for whatever reason. In which case:
@personDetailTabForm.value.map{ personDetailTab =>
@persons.detail("personal", personDetailTab.firstName) // { ... whatever else
// anything else you want to render
} getOrElse { // errors in the form; personDetailTabForm cannot yield a valid personDetailTab
<h3> oops, what went wrong here? </h2>
}
It all depends on what you want to do in personal.scala.html
. Form[T]
is a good way
to deal with input and validation of some T
thing, but if you are just displaying it,
and if you have a T
(in your case PersonDetailTab
) just pass that to the template as it is. If your PersonDetailTab
may or may not exist, then just use Option[PersonDetailTab]
instead Form[PersonDetailTab]
.
Upvotes: 4