📜 ⬆️ ⬇️

jQuery State form or simple form control

Good time% username%!

I want to submit to the public one interesting plugin to control the state of the form.
What is a “form state”? This is a cast of the values ​​that were set in the form at the time of its initialization. There is nothing superfluous in the plugin, no “preference and harlots”, only what it was designed for, namely control over changing values ​​in the form. What is it for? Well, there are all sorts of situations, for example, for logging changes that have been made to the data. You can try to control the event leaving the page, suddenly, the user has entered something, and forgot to save?

Who cares , you can play on jsfiddle .
')
By itself, the plugin is extremely simple. Initialization:
$(document).ready(function() { $('form').state_form(); }); 

or extended option:
 $(document).ready(function() { $('form').state_form({ //name input in form inputName: 'changed_state', //is default //add input with changes in form insertInForm: 1, //is defaul //function before form submit //call if form has changes ifChanged: function() { //is default return true; }, //field for excluded exclude: ['field1', 'field2'] }); }); 


Check for status changes:
 $('form').state_form('is_changed'); 

Get modified fields:
 $('form').state_form('get_changes'); 


A handler is attached to the form submit event, which before it collects all the changes, encodes them in json, creates a field in the form where it inserts the resulting string, and the changes are sent to the server. This feature can be disabled.

UPD: updated the plugin to 0.0.3, where he added methods of working with the state history. Thanks to RubaXa for suggestions and criticism!

Source code github.com/Slavenin/state_form
I would be happy for comments and suggestions!
Thank you for attention!

Source: https://habr.com/ru/post/207338/


All Articles