_FormMixin.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. define("dojox/form/manager/_FormMixin", [
  2. "dojo/_base/lang",
  3. "dojo/_base/kernel",
  4. "dojo/_base/event",
  5. "dojo/window",
  6. "./_Mixin",
  7. "dojo/_base/declare"
  8. ], function(lang, dojo, event, windowUtils, _Mixin, declare){
  9. var fm = lang.getObject("dojox.form.manager", true),
  10. aa = fm.actionAdapter;
  11. return declare("dojox.form.manager._FormMixin", null, {
  12. // summary:
  13. // Form manager's mixin for form-specific functionality.
  14. // description:
  15. // This mixin adds automated "onreset", and "onsubmit" event processing
  16. // if we are based on a form node, defines onReset(), onSubmit(),
  17. // reset(), submit(), and isValid() methods like dijit.form.Form.
  18. // It should be used together with dojox.form.manager.Mixin.
  19. // HTML <FORM> attributes (if we are based on the form element)
  20. name: "",
  21. action: "",
  22. method: "",
  23. encType: "",
  24. "accept-charset": "",
  25. accept: "",
  26. target: "",
  27. startup: function(){
  28. this.isForm = this.domNode.tagName.toLowerCase() == "form";
  29. if(this.isForm){
  30. this.connect(this.domNode, "onreset", "_onReset");
  31. this.connect(this.domNode, "onsubmit", "_onSubmit");
  32. }
  33. this.inherited(arguments);
  34. },
  35. // form-specific functionality
  36. _onReset: function(evt){
  37. // NOTE: this function is taken from dijit.formForm, it works only
  38. // for form-based managers.
  39. // create fake event so we can know if preventDefault() is called
  40. var faux = {
  41. returnValue: true, // the IE way
  42. preventDefault: function(){ // not IE
  43. this.returnValue = false;
  44. },
  45. stopPropagation: function(){}, currentTarget: evt.currentTarget, target: evt.target
  46. };
  47. // if return value is not exactly false, and haven't called preventDefault(), then reset
  48. if(!(this.onReset(faux) === false) && faux.returnValue){
  49. this.reset();
  50. }
  51. event.stop(evt);
  52. return false;
  53. },
  54. onReset: function(){
  55. // summary:
  56. // Callback when user resets the form. This method is intended
  57. // to be over-ridden. When the `reset` method is called
  58. // programmatically, the return value from `onReset` is used
  59. // to compute whether or not resetting should proceed
  60. return true; // Boolean
  61. },
  62. reset: function(){
  63. // summary:
  64. // Resets form widget values.
  65. this.inspectFormWidgets(aa(function(_, widget){
  66. if(widget.reset){
  67. widget.reset();
  68. }
  69. }));
  70. if(this.isForm){
  71. this.domNode.reset();
  72. }
  73. return this;
  74. },
  75. _onSubmit: function(evt){
  76. // NOTE: this function is taken from dijit.formForm, it works only
  77. // for form-based managers.
  78. if(this.onSubmit(evt) === false){ // only exactly false stops submit
  79. event.stop(evt);
  80. }
  81. },
  82. onSubmit: function(){
  83. // summary:
  84. // Callback when user submits the form. This method is
  85. // intended to be over-ridden, but by default it checks and
  86. // returns the validity of form elements. When the `submit`
  87. // method is called programmatically, the return value from
  88. // `onSubmit` is used to compute whether or not submission
  89. // should proceed
  90. return this.isValid(); // Boolean
  91. },
  92. submit: function(){
  93. // summary:
  94. // programmatically submit form if and only if the `onSubmit` returns true
  95. if(this.isForm){
  96. if(!(this.onSubmit() === false)){
  97. this.domNode.submit();
  98. }
  99. }
  100. },
  101. isValid: function(){
  102. // summary:
  103. // Make sure that every widget that has a validator function returns true.
  104. for(var name in this.formWidgets){
  105. var stop = false;
  106. aa(function(_, widget){
  107. if(!widget.get("disabled") && widget.isValid && !widget.isValid()){
  108. stop = true;
  109. }
  110. }).call(this, null, this.formWidgets[name].widget);
  111. if(stop){
  112. return false;
  113. }
  114. }
  115. return true;
  116. },
  117. validate: function(){
  118. var isValid = true,
  119. formWidgets = this.formWidgets,
  120. didFocus = false, name;
  121. for(name in formWidgets){
  122. aa(function(_, widget){
  123. // Need to set this so that "required" widgets get their
  124. // state set.
  125. widget._hasBeenBlurred = true;
  126. var valid = widget.disabled || !widget.validate || widget.validate();
  127. if(!valid && !didFocus){
  128. // Set focus of the first non-valid widget
  129. windowUtils.scrollIntoView(widget.containerNode || widget.domNode);
  130. widget.focus();
  131. didFocus = true;
  132. }
  133. isValid = isValid && valid;
  134. }).call(this, null, formWidgets[name].widget);
  135. }
  136. return isValid;
  137. }
  138. });
  139. });