Customizing How FormState is Saved

By default, the Form engine will store the state of the actual Form objects as a rather long Base64 encoded string. While this is a very simple approach, it is not secure, can gradually produce performance problems as the formstate grows, and may reach size limitations.

QCubed resolves this by offering the ability to store/handle the formstate in various ways. You can store the formstate data in PHP Sessions, in a database, in an external cache, or directly on the filesystem. For all of these methods, you end up only passing a small key back to the user so the form can keep track of the location of the data.

Since the FormState handler is encapsulated in its own class, you can even define your own formstate handler.

In our example here, we use SessionHandler to store the formstate data in the PHP Session, and we will only store the session key (in this case, a short string) on the page as a hidden form variable.

If you use your browser's "View Source" functionality, you will see that the Qform__FormState hidden form variable is now a lot shorter (likely about 20 bytes). Compare this to the first example where the form state was easily over 1 KB. This is because the bulk of the form state is being stored as a PHP Session Variable, which is located on the server, itself.

The following formstate handlers are available to you:

  • DefaultHandler: Stores the formstate as hidden input on the form
  • DatabaseHandler: Places the formstate in a SQL database
  • FileHandler: Stores formstates as files in a folder, similar to how PHP stores sessions by default
  • SessionHandler: Stores the formstate as variable in the PHP Session
  • RedisHandler: Stores the formstate in a Redis database

See the Formstate.cfg.php file in the /project/includes/configuration/active directory for details on how to choose which formstate handler to use and configure it.

Click the button to change my message.