HowTo style the error/warning/notice messages

From Lift

Jump to: navigation, search

Lift provides a feature to record error, warning, and notice (informational) messages during request processing, and to render those messages to (X)HTML output. To record an error/warning/notice message, call any of the following S methods during request processing...

S.error(n: String)
S.error(n: NodeSeq)
S.notice(n: String)
S.notice(n: NodeSeq)
S.warning(n: String)
S.warning(n: NodeSeq)

For example...

S.error("please login first!")
S.error(<b>you need to be an <i>administrator</i> to do that</b>)

In your XHTML templates, you can then use the following tag to output any of the error/warning/notice messages that were recorded during the request:

<lift:snippet type="msgs"/>

The error_report tag also takes optional title/label and CSS class name information. For example:

 <lift:snippet type="msgs">
   <lift:error_msg>Error!  The details are:</lift:error_msg>
   <lift:error_class>errorBox</lift:error_class>
   <lift:warning_msg>Whoops, I had a problem:</lift:warning_msg>
   <lift:warning_class>warningBox</lift:warning_class>
   <lift:notice_msg>Note:</lift:notice_msg>
   <lift:notice_class>noticeBox</lift:notice_class>
 </lift:snippet>

The errorBox, warningBox, and noticeBox are the class names which you can use to style the rendered output using CSS. For example:

 .errorBox {
   margin:.5em 1em .5em 1em;
   border:2px solid #633;
   background:#aff;
   padding:.5em 2em .5em 2em;
 }

MESSAGES UPDATES

More information about messages. In this context I'm referring to messages as being one of the Errors/Warnings/Notices.

Lift 0.7 code-base unified the messages for Non-Ajax as well as Ajax requests.

To reiterate the changes in this area:

1. error_report built in snippet was renamed to msgs
2. Now, when setting a message like S.error("Error message") we can associate the message with an ID. So we can call 
   S.error("msg_id", "Error message"). By this we're telling Lift that this error message must be rendered in the real estate owned by 
   <lift:msg id="msg_id"/>. That's right, that's a new snippet. It's role is to render only message associated with the id specified by
   id attribute of msg snippet. This also can be used to render validation messages near by form elements such as:
   <input type="text" id="username"><lift:msg id="username_msg"/>
3. You can use construct like S.error("msg_id", "Error message") for both Ajax and non Ajax request however styling the messages differs a bit:
   3.1 For Non-Ajax the styling is given by <lift:error_class> as in the above example.
   3.2. For Ajax we can not use the same information as for Non-Ajax because that would imply that Lift needs to keep more state on server side. 
        Momentarily this is to be avoided. So the alternative is to provide styling information through three LiftRules variables: 
        ajaxNoticeMeta/ajaxWarningMeta/ajaxErrorMeta : Can[AjaxMessageMeta] where AjaxMessageMeta is
        
        case class AjaxMessageMeta(title: Can[String], cssClass: Can[String])
 
        Note that title attribute is considered only by msgs snippet and not by msg snippet.


Notices also work for JsonHandler. For example:

  object json extends JsonHandler {
    def apply(in: Any): JsCmd =
      SetHtml("json_result", in match {
        case JsonCmd("show", _, p: String, _) => {
          S.error("A general Error");
          S.warning("who_msg", Text("Who field warning"))
          S.warning("who_msg", <i> Another</i>)
          S.error("sec_msg", Text("Invalid sec"))

          Text(p)
        }
        case JsonCmd("count", _, p: String, _) => Text(p.length+"
Characters")
        case x => <b>Problem... didn't handle JSON message {x}</b>
      })
  } 
Personal tools