jilen
jilen

Reputation: 5763

playframework 2 - empty string for optional form field

I have a form

case class UserUpdateForm(
  id:Long, name: String, 
  remark: Option[String], location: Option[String])

I define the fields as

"id" -> of[Long],
"remarks" -> optional(text)

the remark field is None, Not Some("") I am excepting,

So, how can I bind an empty string to optional text field

Upvotes: 2

Views: 1842

Answers (3)

makingthematrix
makingthematrix

Reputation: 395

I stumbled upon the same thing some months ago. I didn't find any easy way around it, so I decided to play along.

Basically, "remarks" -> optional(text) will always return None when text is an empty string. So instead of treating an empty string as a sign of no updates, fill the remarks field in the form with the original value and then, after you get it back:

remarks match {
  case None => // set remarks to ""
  case originalRemark => // do nothing
  case _ => // set remarks to the new value
}

Hope it helps. It's my first entry here, on Stack Overflow :)

Upvotes: 0

jilen
jilen

Reputation: 5763

case class OptionalText(wrapped: Mapping[String], val constraints: Seq[Constraint[Option[String]]] = Nil) extends Mapping[Option[String]] {
    override val format: Option[(String, Seq[Any])] = wrapped.format
    val key = wrapped.key

    def verifying(addConstraints: Constraint[Option[String]]*): Mapping[Option[String]] = {
      this.copy(constraints = constraints ++ addConstraints.toSeq)
    }

    def bind(data: Map[String, String]): Either[Seq[FormError], Option[String]] = {
      data.keys.filter(p => p == key || p.startsWith(key + ".") || p.startsWith(key + "[")).map(k => data.get(k)).collect { case Some(v) => v }.headOption.map { _ =>
        wrapped.bind(data).right.map(Some(_))
      }.getOrElse {
        Right(None)
      }.right.flatMap(applyConstraints)
    }

    def unbind(value: Option[String]): (Map[String, String], Seq[FormError]) = {
      val errors = collectErrors(value)
      value.map(wrapped.unbind(_)).map(r => r._1 -> (r._2 ++ errors)).getOrElse(Map.empty -> errors)
    }

    def withPrefix(prefix: String): Mapping[Option[String]] = {
      copy(wrapped = wrapped.withPrefix(prefix))
    }

    val mappings: Seq[Mapping[_]] = wrapped.mappings
  }

  val textOpt = new OptionalText(text)

Finally I copied the OptionalMapping class and exclude the empty filter part

Upvotes: 2

char101
char101

Reputation: 1281

Use

"remarks" -> default(text, "")

Upvotes: -1

Related Questions