Sunday, 30 August 2009

Writing a Create Form in Lift

The create form is one of the other pages CRUDify trait provides. To hand roll your own form deactivate the 'Create Item' menu in the Item class by overriding the createMenuLoc method on the Item class.

     override def createMenuLoc = Empty

Add a file called create.html in the /src/main/webapp/item/ folder.

create item folder

The create.html file should contain some sample text to test the page with.

     <lift:surround with="default" at="content">
         <p>Some text to test the create page</p>
     </lift:surround>

Add the create page to the SiteMap in the Boot.scala using a new Menu item.

    val entries = Menu(Loc("Home", List("index"), "Home")) ::
                    Menu(Loc("ItemList", List("item", "list"), "Item List")) ::
                    Menu(Loc("ItemCreate", List("item", "create"), "Create Item")) ::
                    User.sitemap ::: Item.menus

Run the app and click on the new 'Create Item' on the left to view the test page.

Add a snippet to src/scala/com/shopping/snippet called CreateItem.

package com.shopping.snippet

import com.shopping._
import model._
import net.liftweb._

import util._

import scala.xml._

class CreateItem {

    def create(html : NodeSeq): NodeSeq = {
        val item = new Item()
        item.toForm(Full("save"), {_.save})
    }

}

The CreateItem snippet creates a new instance of the Item class can calls the toForm method on the new instance which is inherited from the Mapper class. The toForm method uses the Item class fields to generate input fields. The first parameter on the toForm method takes a Lift Box and the second a function. A Lift Box is a container which can declare if it is full or not, in a similar way to the Scala Option class but adds several features. The String passed into the constructor of the Full class (a subclass of Box) is the text that appears on the submit button. The function parameter is called when the submit button is pressed. To use the CreateItem snippet add <lift:CreateItem.create form="POST"/> to the create.html file.

<lift:surround with="default" at="content">
    <lift:CreateItem.create form="POST"/>
</lift:surround>

Run the app using mvn jetty:run and try creating some shopping list items and viewing them in the list view.

Correct the order the fields appear on the form by overriding the fieldOrder method in the Item MetaMapper object.

override def fieldOrder = List(name, amount)

The code for this application is available on Git hub here http://github.com/oliverdaff/Lift-Shopping/tree/master and has been tagged with createItem.

While I was writing this post I discovered that the CRUDify trait should be applied to the Item MetaMapper rather than the mapper class. This has been updated in Git hub against this tag.

6 comments:

  1. Thanks for the posts on CRUDify, have you looked into using TableSorter for the list in view, also, have you looked at TableEditor?

    ReplyDelete
  2. Hi Randin, I've only just started exploring the Lift framework and haven't looked at the TableSorter or TableEditor yet. Are these things you have used before and recommend I take a look at or are you looking for further info on them?

    ReplyDelete
  3. Just suggestions really, TableSorter is a jquery widget for lift, as for TableEditor it's a newer command.

    http://wiki.github.com/dpp/liftweb/example-use-tableeditor-to-manage-simple-lists

    I hope you don't mind but I mentioned your blog in the Lift group.

    ReplyDelete
  4. OF course any into on those and other things are welcome, I would use it.

    ReplyDelete
  5. David Pollak asked if it was ok to post it on the Liftweb wiki, you can post of course, if not I could post part or all of it with your credit listed also with a link here. Here's the thread...

    http://groups.google.com/group/liftweb/browse_thread/thread/c43732fdd80ef506

    ReplyDelete
  6. Sure no problem Randin.

    I've posted a wiki page linking back to these posts on the Lift git hub wiki http://wiki.github.com/dpp/liftweb/example-crudify-a-entity

    ReplyDelete