Extreme Java When vanilla Java is not enough

12Nov/090

Customizing LiftWeb’s error/warning/notice messages

After some CRUD experiments with LiftWeb, I'm really in love with the "view first" paradigm. Specially because the XHTML bindings are very webdesigner-friendly.

One big limitation is the <lift:msgs> snippet - it produces a mandatory "Error" title. This obligatory text is unacceptable when developing enterprise applications. Hopefully, there's an workaround: you can create a snippet named "Msgs" with a "render" method. Your snippet will "override" the builtin (without overriding the class), like this:

package mypkg.snippets
class Msgs {
  def msgs(cls : String, ms : List[NodeSeq]) = ms match {
    case Nil => Nil
    case x => <div class={ cls }>{ ms.flatMap(m => <p>{ m }</p>) }</ul>
  }
  def render(xml : NodeSeq) : NodeSeq =
    <div id={ LiftRules.noticesContainerId }>
      { msgs("msgError", noIdMessages(errors)) }
      { msgs("msgWarning", noIdMessages(warnings)) }
      { msgs("msgNotice", noIdMessages(notices)) }
    </div>
}

This little snippet will emit messages using <div> and <p> instead of <ul> and <li> (with that hardcoded title block). And, since you have the control over the snippet, you can put stub code inside <lift:msgs/> and allow your webdesigner use their favorite WYSIWYG editor:

<lift:msgs>
  <div class="msgError">
    <p>name must have 3 charactes</p>
  </div>
</lift:msgs>
Tagged as: , , No Comments