Chapter 3. Forms Processing in Lift
This chapter looks at how to process form data with Lift: submitting forms, working with form elements. The end result of a form submission can be records being updated in a database, so you may also find Chapter 7 or Chapter 8 useful, as they discuss relational databases and MongoDB, respectively.
To the extent that form processing is passing data to a server, there are also recipes in Chapter 5 that are relevant to form processing.
You’ll find many of the examples from this chapter as source code at https://github.com/LiftCookbook/cookbook_forms.
Plain Old Form Processing
Problem
You want to process form data in a regular, old-fashioned, non-Ajax way.
Solution
Extract form values with S.param, process the values, and produce some output.
For example, we can show a form, process an input value, and give a message back as a notice. The template is a regular HTML form, with the addition of a snippet:
<formdata-lift="Plain"action="/plain"method="post"><inputtype="text"name="name"placeholder="What's your name?"><inputtype="submit"value="Go"></form>
In the snippet, we can pick out the value of the field name with S.param("name"):
packagecode.snippetimportnet.liftweb.common.Fullimportnet.liftweb.http.Simportnet.liftweb.util.PassThruobjectPlain{defrender=S.param("name")match{caseFull(name)=>S.notice("Hello "+name)S.redirectTo("/plain")case_=>PassThru}}
The first time through this snippet, there will be no parameter, so we just ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access