form_field.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. value = [];
  80. $(oElem).find('option:selected').each(function(){
  81. value.push($(this).val());
  82. });
  83. }
  84. else if($(oElem).is(':checkbox') || $(oElem).is(':radio'))
  85. {
  86. if(value === null)
  87. {
  88. value = [];
  89. }
  90. if($(oElem).is(':checked'))
  91. {
  92. value.push($(oElem).val());
  93. }
  94. }
  95. else
  96. {
  97. console.log('Form field : Input type not handle yet.');
  98. }
  99. });
  100. return value;
  101. },
  102. validate: function(oEvent, oData)
  103. {
  104. var oResult = { is_valid: true, error_messages: [] };
  105. // Doing data validation
  106. if(this.options.validators !== null)
  107. {
  108. var bMandatory = (this.options.validators.mandatory !== undefined);
  109. // Extracting value for the field
  110. var oValue = this.getCurrentValue();
  111. var aValueKeys = Object.keys(oValue);
  112. // 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...
  113. // ... But this should never happen.
  114. if( (aValueKeys.length === 0) && bMandatory )
  115. {
  116. oResult.is_valid = false;
  117. oResult.error_messages.push(this.options.validators.mandatory.message);
  118. }
  119. // ... Otherwise, we check every validators
  120. else if(aValueKeys.length > 0)
  121. {
  122. var value = oValue[aValueKeys[0]];
  123. for(var sValidatorType in this.options.validators)
  124. {
  125. var oValidator = this.options.validators[sValidatorType];
  126. if(sValidatorType === 'mandatory')
  127. {
  128. // Works for string, array, object
  129. if($.isEmptyObject(value))
  130. {
  131. oResult.is_valid = false;
  132. oResult.error_messages.push(oValidator.message);
  133. }
  134. // ... In case of non empty array, we have to check if the value is not null
  135. else if($.isArray(value))
  136. {
  137. for(var i in value)
  138. {
  139. if(typeof value[i] === 'string')
  140. {
  141. if($.isEmptyObject(value[i]))
  142. {
  143. oResult.is_valid = false;
  144. oResult.error_messages.push(oValidator.message);
  145. }
  146. }
  147. else
  148. {
  149. console.log('Form field: mandatory validation not supported yet for the type "' + (typeof value[i]) +'"');
  150. }
  151. }
  152. }
  153. }
  154. else
  155. {
  156. var oRegExp = new RegExp(oValidator.reg_exp, "g");
  157. if(typeof value === 'string')
  158. {
  159. if(!oRegExp.test(value))
  160. {
  161. oResult.is_valid = false;
  162. oResult.error_messages.push(oValidator.message);
  163. }
  164. }
  165. else if($.isArray(value))
  166. {
  167. for(var i in value)
  168. {
  169. if(value[i] === 'string' && !oRegExp.test(value))
  170. {
  171. oResult.is_valid = false;
  172. oResult.error_messages.push(oValidator.message);
  173. }
  174. }
  175. }
  176. else
  177. {
  178. console.log('Form field: validation not supported yet for the type "' + (typeof value) +'"');
  179. }
  180. }
  181. }
  182. }
  183. }
  184. this.options.on_validation_callback(this, oResult);
  185. return oResult;
  186. },
  187. // Debug helper
  188. showOptions: function()
  189. {
  190. return this.options;
  191. }
  192. });
  193. });