Form Data / Storage - 图1

Form

Form Data

Framework7 comes with some very useful methods, this makes working with forms as simple as possible:

Form Data App Methods

Using the following app methods we can easily convert all form fields values to data object and fill the form from data object:

app.form.convertToData(form)- convert form fields values to data object

  • form - HTMLElement or string (with CSS Selector) of form that should be converted to data object. Required.

  • Method returns object

app.form.fillFromData(form, data)- fill up form according to data object

  • form - HTMLElement or string (with CSS Selector) of form that should be converted to data object. Required.
  • formData - object with from data. Required.

  • Note that each input should have name attribute, otherwise its value will not be presented in data object

  • Checkboxes and “multiple” selects will be presented as Arrays

Form Data Events

Form data api will fire the following DOM events on form element and app instance:

DOM Events

EventTargetDescription
form:todataForm Element<form>Event will be triggered on form when calling app.form.convertToData
form:fromdataForm Element<form>Event will be triggered on form when calling app.form.fillFromData

App Events

Form Data api emit events on app instance as well:

EventTargetArgumentsDescription
formToDataapp(form, data)Event will be triggered on app.form.convertToData call
formFromDataapp(form, data)Event will be triggered on app.form.fillFromData call

Form Data Example

  1. <form class="list" id="my-form">
  2. <ul>
  3. <li>
  4. <div class="item-content item-input">
  5. <div class="item-inner">
  6. <div class="item-title item-label">Name</div>
  7. <div class="item-input-wrap">
  8. <input type="text" name="name" placeholder="Your name">
  9. </div>
  10. </div>
  11. </div>
  12. </li>
  13. <li>
  14. <div class="item-content item-input">
  15. <div class="item-inner">
  16. <div class="item-title item-label">E-mail</div>
  17. <div class="item-input-wrap">
  18. <input type="email" name="email" placeholder="E-mail">
  19. </div>
  20. </div>
  21. </div>
  22. </li>
  23. <li>
  24. <div class="item-content item-input">
  25. <div class="item-inner">
  26. <div class="item-title item-label">Gender</div>
  27. <div class="item-input-wrap">
  28. <select name="gender">
  29. <option value="male" selected>Male</option>
  30. <option value="female">Female</option>
  31. </select>
  32. </div>
  33. </div>
  34. </div>
  35. </li>
  36. <li>
  37. <div class="item-content">
  38. <div class="item-inner">
  39. <div class="item-title">Toggle</div>
  40. <div class="item-after">
  41. <label class="toggle toggle-init">
  42. <input type="checkbox" name="toggle" value="yes"><i class="toggle-icon"></i>
  43. </label>
  44. </div>
  45. </div>
  46. </div>
  47. </li>
  48. </ul>
  49. </form>
  50. <div class="block block-strong row">
  51. <div class="col"><a class="button convert-form-to-data" href="#">Get Data</a></div>
  52. <div class="col"><a class="button fill-form-from-data" href="#">Fill Form</a></div>
  53. </div>
  1. var app = new Framework7();
  2. var $$ = Dom7;
  3. $$('.convert-form-to-data').on('click', function(){
  4. var formData = app.form.convertToData('#my-form');
  5. alert(JSON.stringify(formData));
  6. });
  7. $$('.fill-form-from-data').on('click', function(){
  8. var formData = {
  9. 'name': 'John',
  10. 'email': 'john@doe.com',
  11. 'gender': 'female',
  12. 'toggle': ['yes'],
  13. }
  14. app.form.fillFromData('#my-form', formData);
  15. });

Form Storage

With form storage it is easy to store and parse forms data automatically, especially on Ajax loaded pages. And the most interesting part is that when you load this page again Framework7 will parse this data and fill up all form fields automatically!

To enable form storage for specific form, all you need is:

  • add form-store-data class to form
  • add id attribute to form. It will not work if form doesn’t have id attribute
  • make sure that all you inputs have name attributes, otherwise they will be ignored

After form storage is enabled with form-store-data class, then form data will be saved to localStorage on every form input change.

To ignore inputs for storage you can add no-store-data or ignore-store-data class to required input elements.

Otherwise you can use the following app methods to store/get/remove stored form data:

Form Storage App Methods

app.form.getFormData(formId)- get form data for the form with specified id attribute

  • formId - string - “id” attribute of required form. Required.
  • Method returns object with form data

app.form.storeFormData(formId, data)- store form data for the form with specified id attribute

  • formId - string - “id” attribute of required form. Required.
  • formJSON - object - JSON data to store

app.form.removeFormData(formId)- remove form data for the form with specified id attribute

  • formId - string - “id” attribute of required form. Required.

Form Storage Events

Form Storage api will fire the following DOM events on form element and app instance:

DOM Events

EventTargetDescription
form:storedataForm Element<form>Event will be triggered right after form data saved

App Events

Form Storage api emit events on app instance as well:

EventTargetArgumentsDescription
formStoreDataapp(form, data)Event will be triggered right after form data saved

Form Storage Example

  1. <form class="list form-store-data" id="my-form">
  2. <ul>
  3. <li>
  4. <div class="item-content item-input">
  5. <div class="item-inner">
  6. <div class="item-title item-label">Name</div>
  7. <div class="item-input-wrap">
  8. <input type="text" name="name" placeholder="Your name">
  9. </div>
  10. </div>
  11. </div>
  12. </li>
  13. <li>
  14. <div class="item-content item-input">
  15. <div class="item-inner">
  16. <div class="item-title item-label">E-mail</div>
  17. <div class="item-input-wrap">
  18. <input type="email" name="email" placeholder="E-mail">
  19. </div>
  20. </div>
  21. </div>
  22. </li>
  23. <li>
  24. <div class="item-content item-input">
  25. <div class="item-inner">
  26. <div class="item-title item-label">Ignore from storage</div>
  27. <div class="item-input-wrap">
  28. <input class="no-store-data" type="text" name="text" placeholder="This value won't be stored">
  29. </div>
  30. </div>
  31. </div>
  32. </li>
  33. <li>
  34. <div class="item-content item-input">
  35. <div class="item-inner">
  36. <div class="item-title item-label">Gender</div>
  37. <div class="item-input-wrap">
  38. <select name="gender">
  39. <option value="male" selected>Male</option>
  40. <option value="female">Female</option>
  41. </select>
  42. </div>
  43. </div>
  44. </div>
  45. </li>
  46. <li>
  47. <div class="item-content">
  48. <div class="item-inner">
  49. <div class="item-title">Switch</div>
  50. <div class="item-after">
  51. <label class="toggle">
  52. <input type="checkbox" name="switch" value="yes"><i class="toggle-icon"></i>
  53. </label>
  54. </div>
  55. </div>
  56. </div>
  57. </li>
  58. </ul>
  59. </form>

Ajax Form Submit

Framework7 allows automatically send form data using Ajax.

It could be done in two ways

  • when user submits it (when he clicks on “submit” button) or when “submit” event triggered on form programmatically
  • when user change any form field or when “change” event triggered on form (or form field) programmatically

Send form data on submit

To enable Ajax form and send data automatically on submit, we just need to add form-ajax-submit class to form:

  1. <form action="send-here.html" method="GET" class="form-ajax-submit">
  2. ...
  3. </form>

And when user will submit this form, it automatically will be sended using Ajax with the following rules:

  • Form data will be sended to the file/url specified in form’s action attribute

  • Request method will be the same as specified in form’s method attribute

  • Content type will be the same as specified in form’s enctype attribute. By default (if not specified), it is application/x-www-form-urlencoded

Send form data on input change

Mostly we don’t use “submit” buttons in apps, so in this cases we need to submit form data when user changes any form fields. For this case we need to use form-ajax-submit-onchange class:

  1. <form action="send-here.html" method="GET" class="form-ajax-submit-onchange">
  2. ...
  3. </form>

And when user will change any form filed, form data automatically will be sended using Ajax with the same rules as in previous case.

Ajax Form Submit Events

Sometimes we need to get actual XHR repsonse from the file/url where we send form data with Ajax. We can use special events for that:

Dom Events

EventTargetDescription
formajax:successForm Element<form class=”form-ajax-submit”>Event will be triggered after successful Ajax request
formajax:completeForm Element<form class=”form-ajax-submit”>Event will be triggered after Ajax request completed
formajax:beforesendForm Element<form class=”form-ajax-submit”>Event will be triggered right before Ajax request
formajax:errorForm Element<form class=”form-ajax-submit”>Event will be triggered on Ajax request error
  1. var app = new Framework7();
  2. var $$ = Dom7;
  3. $$('form.form-ajax-submit').on('formajax:success', function (e) {
  4. var xhr = e.detail.xhr; // actual XHR object
  5. var data = e.detail.data; // Ajax response from action file
  6. // do something with response data
  7. });

App Events

EventTargetArgumentsDescription
formAjaxSuccessapp(formEl, data, xhr)Event will be triggered after successful Ajax request
formAjaxCompleteapp(formEl, data, xhr)Event will be triggered after Ajax request completed
formAjaxBeforeSendapp(formEl, data, xhr)Event will be triggered right before Ajax request
formAjaxErrorapp(formEl, data, xhr)Event will be triggered on Ajax request error
  1. var app = new Framework7();
  2. app.on('formAjaxSuccess', function (formEl, data, xhr) {
  3. // do something with response data
  4. });

← Floating Action Button / FAB

Gauge →