Scala lift framework, ajax form that submits multiple values?
Asked Answered
S

3

6

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!

Subterfuge answered 17/4, 2009 at 11:47 Comment(0)
O
9

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')")
}
Oliviaolivie answered 9/9, 2009 at 22:9 Comment(2)
Thanks, this looks like an easy solution, going to try soon.Subterfuge
In the last snippet, there should be an = to prevent processEntryAdd automatically becoming Unit.Hollo
N
1

In response to this question David Pollak suggested using

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

on the lift mailing list.

Nunez answered 22/4, 2009 at 20:33 Comment(1)
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.Subterfuge
S
1

Here's the answer, scroll to the bottom, (ignore the first space after <)

http://www.assembla.com/wiki/show/liftweb/ajaxForm

"submit" -> (SHtml.hidden(auth) ++ < input type="submit" value="Login"/>)

Solfeggio answered 4/1, 2011 at 14:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.