2 * Description: implements a form
3 * Copyright (c) 2013 UPMC Sorbonne Universite
8 * It's a best practice to pass jQuery to an IIFE (Immediately Invoked Function
9 * Expression) that maps it to the dollar sign so it can't be overwritten by
10 * another library in the scope of its execution.
14 /***************************************************************************
15 * Method calling logic
16 ***************************************************************************/
18 $.fn.CreateForm = function( method ) {
19 if ( methods[method] ) {
20 return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
21 } else if ( typeof method === 'object' || ! method ) {
22 return methods.init.apply( this, arguments );
25 //$.error( 'Method ' + method + ' does not exist on jQuery.CreateForm' );
29 /***************************************************************************
31 ***************************************************************************/
36 * @brief Plugin initialization
37 * @param options : an associative array of setting values
38 * @return : a jQuery collection of objects on which the plugin is
39 * applied, which allows to maintain chainability of calls
41 init : function ( options ) {
42 return this.each(function() {
45 /* An object that will hold private variables and methods */
46 var form = new CreateForm(options);
47 $this.data('plugin', form);
49 $(this).on('validate.Form', form.validate);
50 $(this).on('validate', form.validate);
57 /***************************************************************************
59 ***************************************************************************/
61 function CreateForm(options) {
63 /* save a reference to this */
66 /* member variables */
67 this.options = options;
71 this.on_result = function(data) {
76 * \brief Validate the form
77 * \param validate_callback (function) a callback to be triggered when validation is done
78 * \return True if all fields match validation regex
80 this.validate = function(validate_callback) {
81 var frm = document.forms['form_'+options.plugin_uuid]
83 // $this = $('#' + options.plugin_uuid); // useless
85 // Loop on the fields and test regexp if present
88 $.each(options.columns, function(i, column) {
89 var value = frm.elements[column['field']].value;
90 var rx = column['validate_rx'];
92 if (rx && !value.match(rx)) {
93 str = column['validate_err'];
96 params[column['field']] = value;
97 $('#err-' + options.plugin_uuid + '-' + column['field']).html(str);
100 /* If the form correctly validates, we issue a create query */
102 /* Inform user about ongoing query: spinner */
104 /* Issue json query and wait for callback */
108 /* Note, if the create has already been done (or fails, or ... ?)
109 * shall we proceed to an update ? */
111 /* We always return false. Only the query callback is in charge of
112 * advancing to next step */