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!
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')")
}
In response to this question David Pollak suggested using
"submit" -> SHtml.hidden("Add user", processEntryAdd) ++
on the lift mailing list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With