How to persist Ajax form fields within session?

29 views
Skip to first unread message

Sriram N

unread,
Apr 11, 2015, 9:42:42 PM4/11/15
to lif...@googlegroups.com

Lift 2.6.x => I have an Ajax form with many text and other fields (mostly text).  The user's choices in the fields persist so long as they remain on the same page as just the results div changes after validating and processing the form data.  However, if they navigate to another page and come back, all the fields have the default values specified in the view template for the form (usually blank with placeholder grey text). 

What is the recommended way to set up a form template that will pre-populate (if they exist) the user's choices (within the active session) else do what it is doing currently. I imagine that this would involve using the S object (which I haven't so far) and constructing the template to pull the values from the S object.  An example of a form with one text field, one radio, one checkbox, and one dropdown would be highly appreciated.

p.s. I spent much time looking at Play and decided to come back to the warmer climes here.


Sriram N

unread,
Apr 12, 2015, 11:18:13 AM4/12/15
to lif...@googlegroups.com
I ended up doing the following for my use case which resolved my question here.

This scheme persists last set of form values from user till server restart (across sessions)

def render = {
...
var fieldname1 = initialize this from the map for this user
The map is defined in a singleton object
val fields = new mutable.HashMap[String, mutable.HashMap[String,String]]
  of the form Map(useremail, Map(fieldname -> fieldvalue))
...

def process():JsCmd = {
  ...
  update the key,value combinations for this user 
  key is fieldname1 and value is stored in the var with the same name
  process def is called on submission
  ...
} // end process

...

"@fieldname1" #> SHtml.text(fieldname1, fieldname1 = _) &
"type=submit" #> ajaxSubmit("Query", process)
} // end render

The first param of text is used to initialize the form field
The second param could be written as formvalue => fieldname1 = formvalue 
Clearly this assignment only happens when a value is actually received
So on page load only the first param is used to assign.
On page submit  fieldname1 is updated and process runs.

Antonio Salazar Cardozo

unread,
Apr 13, 2015, 4:22:17 PM4/13/15
to lif...@googlegroups.com
Are these changes persisted? If so, why not load them from the database when you
load the page? If not, you can use a SessionVar to persist the values within the current
session.

Keep in mind that the map you're storing is going to be subject to concurrent access,
and things could start behaving very poorly unless you at least make it a ConcurrentHashMap.
Thanks,
Antonio
Reply all
Reply to author
Forward
0 new messages