Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala lift framework, ajax form that submits multiple values?

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!

like image 703
ivans Avatar asked Apr 17 '09 11:04

ivans


2 Answers

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')")
}
like image 191
Erlend Halvorsen Avatar answered Sep 28 '22 06:09

Erlend Halvorsen


In response to this question David Pollak suggested using

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

on the lift mailing list.

like image 30
Alexander Kellett Avatar answered Sep 28 '22 07:09

Alexander Kellett