[2.0] how to bind a LocalTime in a Form

974 views
Skip to first unread message

Daniel Berndt

unread,
Feb 28, 2012, 11:42:40 AM2/28/12
to play-framework
Hello again,

I am trying to create a form wrapping a class which contains a field
with a LocalTime.
Now I can't see how to tell what format the input has to be in order
to be bound to a LocalTime.

if I use "form.fill(new ClassWithLocalTime(LocalTime.now())" the field
will be filled with a string like that: "17:35:11.249"
However, when I submit this form it constantly claims that the
LocalTime field has an "Invalid value"

So is there a way to properly bind the LocalTime field?

Cheers,
Daniel

Guillaume Bort

unread,
Feb 28, 2012, 12:17:40 PM2/28/12
to play-fr...@googlegroups.com
Are you using Java or Scala? Both use different approach. The Java API
use the Spring data binder.

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>

--
Guillaume Bort

Daniel Berndt

unread,
Feb 28, 2012, 6:54:47 PM2/28/12
to play-framework
Yes, I am using Java. Is there any nice resource/code sample of how to
apply the DataBinder for a play form? I did not find the spring api
docs too enlightening...

Cheers,
Daniel

Guillaume Bort

unread,
Feb 29, 2012, 4:26:50 AM2/29/12
to play-fr...@googlegroups.com
You can use:

play.data.format.Formatters.register(...) methods to register you own
formatter. You can use both SimpleFormatter (that will use a formatter
for all LocalTime members) or AnnotationFormatter that will also check
an annotation.

Daniel Berndt

unread,
Feb 29, 2012, 5:19:36 AM2/29/12
to play-framework
Excellent! I got it working now.

In case anyone else is interested, here is the code:

Formatters.register(LocalTime.class, new
Formatters.SimpleFormatter<LocalTime>() {

private Pattern timePattern = Pattern.compile("([012]?\\d)[\\s:._-]+
([012345]?\\d)");

@Override
public LocalTime parse(String input, Locale l) throws ParseException
{
Matcher m = timePattern.matcher(input);
if (!m.find()) throw new ParseException("No valid Input",0);
return new LocalTime(Integer.valueOf(m.group(1)),
Integer.valueOf(m.group(2)));
}

@Override
public String print(LocalTime localTime, Locale l) {
return localTime.toString("HH:mm");
}
});


I've put it into the "public void onStart(Application app)" method of
the Global.java

­Abhishek ­Pande

unread,
Apr 3, 2013, 9:31:21 AM4/3/13
to play-fr...@googlegroups.com, daniel...@googlemail.com
How to do this in scala? Do we need to implement any trait?

­Abhishek ­Pande

unread,
Apr 13, 2013, 12:50:45 PM4/13/13
to play-fr...@googlegroups.com, daniel...@googlemail.com

-------------------------

package helpers


import play.api.data.format.Formatter

import play.api.data.format.Formats._

import scala.Some

import play.api.data.FormError

object Formats extends play.data.format.Formats{

  /**

   * Formatter for the `org.joda.time.LocalTime` type.

   *

   * @param pattern a date pattern as specified in `org.joda.time.format.DateTimeFormat`.

   */

  def jodaLocalTimeFormat(pattern: String): Formatter[org.joda.time.LocalTime] = new Formatter[org.joda.time.LocalTime] {


    import org.joda.time.LocalTime


    override val format = Some(("format.date", Seq(pattern)))


    def bind(key: String, data: Map[String, String]) = {


      stringFormat.bind(key, data).right.flatMap { s =>

        scala.util.control.Exception.allCatch[LocalTime]

          .either(LocalTime.parse(s, org.joda.time.format.DateTimeFormat.forPattern(pattern)))

          .left.map(e => Seq(FormError(key, "error.time", Nil)))

      }

    }


    def unbind(key: String, value: LocalTime) = Map(key -> value.toString(pattern))

  }


  /**

   * Default formatter for `org.joda.time.LocalTime` type with pattern `HH:mm:ss`.

   *

   * @param pattern a date pattern as specified in `org.joda.time.format.DateTimeFormat`.

   */

  implicit val jodaLocalTimeFormat: Formatter[org.joda.time.LocalTime] = jodaLocalTimeFormat("HH:mm:ss")

}

------------------------

Test it and let me know if it is working for you.

Regards,
Abhishek

On Friday, April 5, 2013 2:22:12 PM UTC+5:30, ANUBHAV SINGH wrote:
In terms of scala how can i do this ?
Reply all
Reply to author
Forward
0 new messages