Enhance Wizard form processing capability
Hello. I'm playing with Lift's wizard currently, and meeting some
problems.
Generally, how can add a form to a wizard screen? I override vendForm
method and it does work. But my feeling that this might not be the
recommended way, and I can only use ajax way to submit that form,
otherwise I will lose current screen. And what's more, as I return a
template from vendForm, say A.edit() will process this template, if I
define a WizardVar, I have to initialize it in vendForm before I can
use it in A.edit(), and if not, after I return from A.edit, that
WizardVar will be reinitialized.
Well, it is a little bit confusing, so I guess I should post my code
as well. Thanks guys.
=====================================================================
package com.honnix.ma.snippet
import java.util.regex.Pattern
import scala.reflect.Manifest
import scala.xml.NodeSeq
import net.liftweb.common._
import net.liftweb.http._
import net.liftweb.util._
import net.liftweb.wizard.Wizard
import S._
import SHtml._
import com.honnix.ma.model.{NetworkElement => NetworkElementModel,
Function => FunctionModel}
object NetworkElementWizard extends Wizard with Loggable {
object networkElementVar extends WizardVar[NetworkElementModel] (
NetworkElementModel.create.name("")
)
val name = new Screen {
val name = new Field with StringField {
lazy val existingNetworkElements = NetworkElementModel.findAll
def name = S ? "Name"
override def required_? = true
def nameUnique(msg: => String): String => List[FieldError] =
s => if (existingNetworkElements.exists(_.name.is == s)) msg else Nil
override def validations = valRegex(Pattern.compile("[a-zA-Z]+"), "invalid name") _ ::
nameUnique("function name is not unique") ::
super.validations
}
}
val function = new Screen {
val function = new Field {
type ValueType = FunctionModel
def name = S ? "Function"
lazy val manifest = buildIt[FunctionModel]
def default = FunctionModel.create
def atLeastOneFunction(msg: => String): (FunctionModel) => List[FieldError] =
(f) => if (networkElementVar.functions.isEmpty) msg else Nil
override def validations = atLeastOneFunction("at least one function") :: super.validations
}
override protected def vendForm[T](implicit manifest: Manifest[T]) = {
networkElementVar.is.name(name.name.is) // ============= here I have to initialize networkElementVar explicitly ==============
Full((x: T, y: T => Unit) =>
<lift:embed what="function" />) // ================== this template will be processed by A.edit, and inside A.edit I will make use of networkElementVar ====================
}
}
def finish {
networkElementVar.save
}
}
problems.
Generally, how can add a form to a wizard screen? I override vendForm
method and it does work. But my feeling that this might not be the
recommended way, and I can only use ajax way to submit that form,
otherwise I will lose current screen. And what's more, as I return a
template from vendForm, say A.edit() will process this template, if I
define a WizardVar, I have to initialize it in vendForm before I can
use it in A.edit(), and if not, after I return from A.edit, that
WizardVar will be reinitialized.
Well, it is a little bit confusing, so I guess I should post my code
as well. Thanks guys.
=====================================================================
package com.honnix.ma.snippet
import java.util.regex.Pattern
import scala.reflect.Manifest
import scala.xml.NodeSeq
import net.liftweb.common._
import net.liftweb.http._
import net.liftweb.util._
import net.liftweb.wizard.Wizard
import S._
import SHtml._
import com.honnix.ma.model.{NetworkElement => NetworkElementModel,
Function => FunctionModel}
object NetworkElementWizard extends Wizard with Loggable {
object networkElementVar extends WizardVar[NetworkElementModel] (
NetworkElementModel.create.name("")
)
val name = new Screen {
val name = new Field with StringField {
lazy val existingNetworkElements = NetworkElementModel.findAll
def name = S ? "Name"
override def required_? = true
def nameUnique(msg: => String): String => List[FieldError] =
s => if (existingNetworkElements.exists(_.name.is == s)) msg else Nil
override def validations = valRegex(Pattern.compile("[a-zA-Z]+"), "invalid name") _ ::
nameUnique("function name is not unique") ::
super.validations
}
}
val function = new Screen {
val function = new Field {
type ValueType = FunctionModel
def name = S ? "Function"
lazy val manifest = buildIt[FunctionModel]
def default = FunctionModel.create
def atLeastOneFunction(msg: => String): (FunctionModel) => List[FieldError] =
(f) => if (networkElementVar.functions.isEmpty) msg else Nil
override def validations = atLeastOneFunction("at least one function") :: super.validations
}
override protected def vendForm[T](implicit manifest: Manifest[T]) = {
networkElementVar.is.name(name.name.is) // ============= here I have to initialize networkElementVar explicitly ==============
Full((x: T, y: T => Unit) =>
<lift:embed what="function" />) // ================== this template will be processed by A.edit, and inside A.edit I will make use of networkElementVar ====================
}
}
def finish {
networkElementVar.save
}
}
Leave a comment