Hi!
Cool to see me there:
* @author Alex Siman
You can also set attribute "maxlength" of HTML input tag based of JPA
size anno. See
http://www.w3schools.com/TAGS/att_input_maxlength.asp
Checkout full code for more ideas. You can create more generic
architecture and wrap existent JPA entities with methods for printing
entity fields to HTML:
package xxx.snippet
import scala.xml._
import net.liftweb.http._
import net.liftweb.http.S._
import net.liftweb.http.SHtml._
import net.liftweb.util.Helpers._
import xxx.model._
class AdminProduct {
// Holder to store entered values for Product between requests.
object Product extends RequestVar[Gift](new Gift())
Product.is.requestObject = Product
println("AdminProduct: Product.is.requestObject = Product")
def render(xhtml: Group): NodeSeq = {
println("--> AdminProduct.render()")
def doSubmit() = {
println("-->--> AdminProduct.doSubmit()")
// Do smth... Ex open/close JPA session
println("<--<-- AdminProduct.doSubmit()")
val valid = Product.is.validate
val product = Product.is
if (!valid) {
S.error("Form has errors.")
} else {
val msg = ("AdminProduct.doSubmit: Product saved: {" +
product.title + " @ $" + product.price + "}")
println(msg)
notice(msg)
redirectTo("/checkout/product/saved")
}
}
println("<-- AdminProduct.render()")
// Request scoped.
bind("product", xhtml,
"title" -> Product.is.titleToForm(),
"price" -> Product.is.descToForm(),
"desc" -> Product.is.priceToForm(),
"submit" -> Product.is.formField("", submit("Save", doSubmit)))
}
}
package xxx.model
import scala.xml._
import net.liftweb.http.SHtml._
import net.liftweb.http.S._
import net.liftweb.util.Helpers._
import net.liftweb.http.RequestVar
import net.liftweb.http.LiftSession._
case class Gift() {
var title = ""
var desc = ""
var price = "0.0"
val titleId = "title"
val descId = "desc"
val priceId = "price"
var requestObject: RequestVar[Gift] = null
private def getProduct() = {
requestObject.is
}
def validate(): Boolean = {
var valid = true
// TODO: Replace by JPA validators.
title match {
case "" => {
valid = false
error(titleId, "Title required.")
}
case _ =>
}
desc match {
case _ =>
}
// TODO: Price must be converted to Double/BigDecimal etc.
price match {
case "0.0" => {
valid = false
error(priceId, "Price required.")
error(priceId, "Price must be > $0.")
}
case "100.0" => {
valid = false
error(priceId, "Price must be < $100.")
}
case _ =>
}
valid
}
def formField(label: String, input: Elem): NodeSeq = {
// ... Body from prev. email.
}
def titleToForm() = {
formField("Title", text(title, getProduct().title = _, ("id",
titleId)))
}
def descToForm() = {
formField("Description", textarea(desc, getProduct().desc = _,
("id", descId)))
}
def priceToForm() = {
formField("Price", text(price, getProduct().price = _, ("id",
priceId)))
}
// TODO: Also there can be more complex method:
//def toForm() = {...}
}
On 8 дек, 02:20, wstrange <
warren.stra...@gmail.com> wrote:
> After some experimenting I have field validation working with JSR 303
> annotations. See:
>
>
http://wstrange.wordpress.com/2009/12/07/inline-field-validation-in-s...