views:

1521

answers:

2

I am just getting started with lift and I am now trying to change a normal form to an ajax form but the method processEntryAdd is never called.

def addUser(xhtml : Group) : NodeSeq = {

    var firstName = ""
    var lastName = ""

    def processEntryAdd() {
        Log.info("processEntryAdd: " + firstName + ", " + lastName)
    }

    SHtml.ajaxForm(
        bind("entry", xhtml,
             "firstName" -> SHtml.text(firstName, 
                 (x) => {
                     Log.info("Setting firstName to " + x); 
                     firstName = x
                 }),
             "lastName" -> SHtml.text(lastName, 
                 (x) => {
                     Log.info("Setting lastName to " + x); 
                     lastName = x
                 }),
             "submit" -> SHtml.submit("Add user", processEntryAdd),
        ))
}

Any idea how to achieve what I am trying to do, or why the code above doesn't work. The values of the two form fields are submitted when the button is pressed and the two local variables firstName and lastName are set but the function associated with SHtml.submit isn't called.

Thanks!

A: 

In response to this question David Pollak suggested using

"submit" -> SHtml.hidden("Add user", processEntryAdd) ++

on the lift mailing list.

Alexander Kellett
And that works, but unfortunately doesn't solve the whole problem. A part of the puzzle that I am still missing is re-rendering a part of the page after ajax response.
ivans
+4  A: 

This question is kind of old, but I recently needed to know this myself, and this is the best solution I've seen so far:

ajaxForm(
    bind("entry", xhtml,
         "firstName" -> text(firstName, firstName = _),
         "lastName" -> text(lastName, lastName = _),
         "submit" -> submit("Add user", processEntryAdd _),
    ) ++ hidden(processEntryAdd _)
)

By adding the processing to a hidden form element you get to keep the the submit button, without changing any view code.

You can add client side behaviour by having processEntryAdd() return a JsCmd:

def processEntryAdd() {
    Log.info("processEntryAdd: " + firstName + ", " + lastName)
    JsRaw("alert('process entry added')")
}
Erlend Halvorsen
Thanks, this looks like an easy solution, going to try soon.
ivans