form_field.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //iTop Form field
  2. ;
  3. $(function()
  4. {
  5. // the widget definition, where 'itop' is the namespace,
  6. // 'form_field' the widget name
  7. $.widget( 'itop.form_field',
  8. {
  9. // default options
  10. options:
  11. {
  12. validators: null,
  13. validate_callback: 'validate', // When using an anonymous function, use the 'me' parameter to acces the current widget : function(me){ return me.validate(); },
  14. on_validation_callback: function(data){ },
  15. get_current_value_callback: 'getCurrentValue',
  16. },
  17. // the constructor
  18. _create: function()
  19. {
  20. var me = this;
  21. this.element
  22. .addClass('form_field');
  23. this.element
  24. .bind('set_validators', function(oEvent, oData){
  25. oEvent.stopPropagation();
  26. me.options.validators = oData;
  27. });
  28. this.element
  29. .bind('validate get_current_value', function(oEvent, oData){
  30. oEvent.stopPropagation();
  31. var callback = me.options[oEvent.type+'_callback'];
  32. if(typeof callback === 'string')
  33. {
  34. return me[callback](oEvent, oData);
  35. }
  36. else if(typeof callback === 'function')
  37. {
  38. return callback(me, oEvent, oData);
  39. }
  40. else
  41. {
  42. console.log('Form field : callback type must be a function or a existing function name of the widget');
  43. return false;
  44. }
  45. });
  46. },
  47. // called when created, and later when changing options
  48. _refresh: function()
  49. {
  50. },
  51. // events bound via _bind are removed automatically
  52. // revert other modifications here
  53. _destroy: function()
  54. {
  55. this.element
  56. .removeClass('form_field');
  57. },
  58. // _setOptions is called with a hash of all options that are changing
  59. // always refresh when changing options
  60. _setOptions: function()
  61. {
  62. this._superApply(arguments);
  63. },
  64. // _setOption is called for each individual option that is changing
  65. _setOption: function( key, value )
  66. {
  67. this._super( key, value );
  68. },
  69. getCurrentValue: function()
  70. {
  71. var value = null;
  72. this.element.find(':input').each(function(iIndex, oElem){
  73. if($(oElem).is(':hidden') || $(oElem).is(':text') || $(oElem).is('textarea'))
  74. {
  75. value = $(oElem).val();
  76. }
  77. else if($(oElem).is('select'))
  78. {
  79. if($(oElem).is('select[multiple]'))
  80. {
  81. value = [];
  82. $(oElem).find('option:selected').each(function(){
  83. value.push($(this).val());
  84. });
  85. }
  86. else
  87. {
  88. value = $(oElem).val();
  89. }
  90. }
  91. else if($(oElem).is(':checkbox') || $(oElem).is(':radio'))
  92. {
  93. if(value === null)
  94. {
  95. value = [];
  96. }
  97. if($(oElem).is(':checked'))
  98. {
  99. value.push($(oElem).val());
  100. }
  101. }
  102. else
  103. {
  104. console.log('Form field : Input type not handle yet.');
  105. }
  106. });
  107. return value;
  108. },
  109. validate: function(oEvent, oData)
  110. {
  111. var oResult = { is_valid: true, error_messages: [] };
  112. // Doing data validation
  113. if(this.options.validators !== null)
  114. {
  115. var bMandatory = (this.options.validators.mandatory !== undefined);
  116. // Extracting value for the field
  117. var oValue = this.getCurrentValue();
  118. var aValueKeys = Object.keys(oValue);
  119. // This is just a safety check in case a field doesn't always return an object when no value assigned, so we have to check the mandatory validator here...
  120. // ... But this should never happen.
  121. if( (aValueKeys.length === 0) && bMandatory )
  122. {
  123. oResult.is_valid = false;
  124. oResult.error_messages.push(this.options.validators.mandatory.message);
  125. }
  126. // ... Otherwise, we check every validators
  127. else if(aValueKeys.length > 0)
  128. {
  129. var value = oValue[aValueKeys[0]];
  130. for(var sValidatorType in this.options.validators)
  131. {
  132. var oValidator = this.options.validators[sValidatorType];
  133. if(sValidatorType === 'mandatory')
  134. {
  135. // Works for string, array, object
  136. if($.isEmptyObject(value))
  137. {
  138. oResult.is_valid = false;
  139. oResult.error_messages.push(oValidator.message);
  140. }
  141. // ... In case of non empty array, we have to check if the value is not null
  142. else if($.isArray(value))
  143. {
  144. for(var i in value)
  145. {
  146. if(typeof value[i] === 'string')
  147. {
  148. if($.isEmptyObject(value[i]))
  149. {
  150. oResult.is_valid = false;
  151. oResult.error_messages.push(oValidator.message);
  152. }
  153. }
  154. else
  155. {
  156. console.log('Form field: mandatory validation not supported yet for the type "' + (typeof value[i]) +'"');
  157. }
  158. }
  159. }
  160. }
  161. else
  162. {
  163. var oRegExp = new RegExp(oValidator.reg_exp, "g");
  164. if(typeof value === 'string')
  165. {
  166. if(!oRegExp.test(value))
  167. {
  168. oResult.is_valid = false;
  169. oResult.error_messages.push(oValidator.message);
  170. }
  171. }
  172. else if($.isArray(value))
  173. {
  174. for(var i in value)
  175. {
  176. if(value[i] === 'string' && !oRegExp.test(value))
  177. {
  178. oResult.is_valid = false;
  179. oResult.error_messages.push(oValidator.message);
  180. }
  181. }
  182. }
  183. else
  184. {
  185. console.log('Form field: validation not supported yet for the type "' + (typeof value) +'"');
  186. }
  187. }
  188. }
  189. }
  190. }
  191. this.options.on_validation_callback(this, oResult);
  192. return oResult;
  193. },
  194. // Debug helper
  195. showOptions: function()
  196. {
  197. return this.options;
  198. }
  199. });
  200. });