_FormMixin.js 4.6 KB

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