Elgg Blog: Elgg 1.8: Sticky Forms

By Cash

You have just spent the last hour writing a blog post. It is a devasting rebuttal of every argument that your blogospheric archnemsis has devised. You know that everyone who reads it will be in awe of your logic, your persuasive power, and your grasp of grammar. The mental image of he_who_should_not_be_named crying at his utter defeat fills you with a deep sense of satisfaction.

You click the submit button and scream in horror as you see the error message about the missing title. There staring you in the face is a blank text box where your masterpiece was supposed to be. The back button brings you no satisfaction.

Sticky forms help to prevent the above situation. Instead of returning you to a blank form when an error occurs, the text you submitted is redisplayed in the form (that's why it is called sticky). Elgg's blog plugin has had auto-saving of drafts for a while now. But there were many forms that were not sticky in Elgg 1.7 and those that were used custom code to accomplish this.

Elgg 1.8 adds an API for making forms sticky. It works like this:

  1. Call elgg_make_sticky_form(<name of form>) at the top of your action.
  2. Call elgg_clear_sticky_form(<name of form>) in the action once the form data has been validated.
  3. Use elgg_is_sticky_form(<name of form>) at the top of your form view to check if there is sticky data to display from a previous submission.
  4. Access all the sticky variables with extract(elgg_get_sticky_values(<name of form>)) in the form.
  5. Clear the sticky variables with elgg_clear_sticky_form(<name of form>) as they are now in the updated form.

For examples on how these functions are being used in Elgg, check the useradd form/action or the bookmarks plugin's save form/action along with its bookmarks_prepare_form_vars() function.

Help prevent web form submission rage by making your forms sticky!

Latest comments