Rahul Rastogi
Rahul Rastogi

Reputation: 4698

get the value of scala Some(text)

I have some fields in my form whose value is optional. Now, if the value are inserted in these fields, then this returns Some(*the_input_value*) And if not inserted then None is returned. I want to get the value null if not inserted and inserted value if inserted. How?Help! here is my class:

case class Registration(
fname:Option[String], 
lname:Option[String],user_email:String,
user_password:String,
gender:Option[String]
)

here is my form mapping:

val registrationForm=Form(
mapping(
  "fname"->optional(text), 
  "lname"->optional(text),
  "user_email"->text, 
  "user_password"->text,
  "gender"->optional(text)
  )(Registration.apply)(Registration.unapply)

);

here is my form:

@(registrationForm:Form[Registration])

@helper.form(routes.Application.registration_process){      
    @helper.inputText(registrationForm("fname"),'_label->"First Name")
@helper.inputText(registrationForm("lname"),'_label->"Last Name")
@helper.inputText(registrationForm("user_email"),'_label->"Email")
@helper.inputPassword(registrationForm("user_password"),'_label->"Password")
@helper.inputPassword(registrationForm("re-password"))
@helper.inputText(registrationForm("gender"),'_label->"Gender")

<input type="submit" value="Register">
}
}

Upvotes: 0

Views: 1241

Answers (1)

4lex1v
4lex1v

Reputation: 21557

Best and shortest way is to use orNull:

scala> val op = Option("String")
op: Option[String] = Some(String)

scala> val res = op.orNull
res: String = String

But i highly recommend not to use null. Better op.getOrElse("")

Upvotes: 2

Related Questions