Skip to content

Instantly share code, notes, and snippets.

@Shadowfiend
Last active December 19, 2015 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shadowfiend/5989309 to your computer and use it in GitHub Desktop.
Save Shadowfiend/5989309 to your computer and use it in GitHub Desktop.
Fully functional lift-formality example, with some additions for DateMidnight handling (these will move into lift-formality very soon). Drop this in a Lift project and invoke the Tester.doIt snippet on your form (data-lift="Tester.doIt") and you'll be in bidness.
class Tester {
import com.withoutincident.formality._
import Formality._
import Html5Validations._
import org.joda.time.DateMidnight
import org.joda.time.format.DateTimeFormat
private implicit val dateMidnightValueConverter = { value: String =>
val locale = S.locale
val pattern = DateTimeFormat.patternForStyle("S-", locale)
val formatter = DateTimeFormat.forPattern(pattern)
try {
Full(formatter.parseDateTime(value).toDateMidnight)
} catch {
case parseFailure: IllegalArgumentException =>
Failure("Invalid date. Expected a date with format " + pattern + ".", Full(parseFailure), Empty)
}
}
private implicit val dateMidnightValueSerializer = { value: DateMidnight =>
val locale = S.locale
val formatter = DateTimeFormat.forPattern(DateTimeFormat.patternForStyle("S-", locale))
formatter.print(value)
}
private def validDate = new Validation[DateMidnight] {
val locale = S.locale
val pattern = DateTimeFormat.patternForStyle("S-", locale)
def apply(value: DateMidnight) = {
Empty
}
override def binder(baseSelector: String) = {
(baseSelector + " [date-format]") #> pattern
}
}
def doIt = {
def tester(s: String) = { println(" OH SHIT " + s); js.JsCmds.Noop }
val bam =
field[String](".bam") ? notEmpty -> on("change", tester _)
val yam = field[DateMidnight](".yam") ? validDate
val sam =
field[String](".sam") ? notEmpty -> on("change", tester _)
//val dam = field[Int](".dam") ? inRange(0, 5)
val dam = field[Int](".dam") ? inRange(0, 5)
val zam =
field[String](".zam") ?
notEmpty ->
on("change", tester _)
case class Thing(bammer: String, yammer: DateMidnight, sammer: String, dammer: Int, zammer: String)
object FormThing extends FormUnapplier(Thing.apply _, Thing.unapply _)
val myForm =
form withField bam withField yam withField sam withField dam withField zam formalize() onSuccess {
case FormThing(thing) =>
println(thing)
} onSubmission {
case Full(zamValue) :+: damValue :+: samValue :+: yamValue :+: bamValue :+: HNil =>
println(zamValue)
case otherwise =>
println("Got " + otherwise)
}
SHtml.makeFormsAjax andThen
myForm.binder()
}
}
@Shadowfiend
Copy link
Author

Nil.::(that).::(this)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment