Source for file SubmitButton.php

Documentation is available at SubmitButton.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Forms
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: require_once dirname(__FILE__'/../../Forms/Controls/Button.php';
  23. 23:  
  24. 24: require_once dirname(__FILE__'/../../Forms/ISubmitterControl.php';
  25. 25:  
  26. 26:  
  27. 27:  
  28. 28: /**
  29. 29:  * Submittable button control.
  30. 30:  *
  31. 31:  * @author     David Grudl
  32. 32:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  33. 33:  * @package    Nette\Forms
  34. 34:  *
  35. 35:  * @property   mixed $validationScope 
  36. 36:  * @property-read bool $submittedBy 
  37. 37:  */
  38. 38: class SubmitButton extends Button implements ISubmitterControl
  39. 39: {
  40. 40:     /** @var array of function(SubmitButton $sender); Occurs when the button is clicked and form is successfully validated */
  41. 41:     public $onClick;
  42. 42:  
  43. 43:     /** @var array of function(SubmitButton $sender); Occurs when the button is clicked and form is not validated */
  44. 44:     public $onInvalidClick;
  45. 45:  
  46. 46:     /** @var mixed */
  47. 47:     private $validationScope TRUE;
  48. 48:  
  49. 49:  
  50. 50:  
  51. 51:     /**
  52. 52:      * @param  string  caption
  53. 53:      */
  54. 54:     public function __construct($caption NULL)
  55. 55:     {
  56. 56:         parent::__construct($caption);
  57. 57:         $this->control->type 'submit';
  58. 58:     }
  59. 59:  
  60. 60:  
  61. 61:  
  62. 62:     /**
  63. 63:      * Sets 'pressed' indicator.
  64. 64:      * @param  bool 
  65. 65:      * @return SubmitButton  provides a fluent interface
  66. 66:      */
  67. 67:     public function setValue($value)
  68. 68:     {
  69. 69:         $this->value = is_scalar($value&& (bool) $value;
  70. 70:         $form $this->getForm();
  71. 71:         if ($this->value || !is_object($form->isSubmitted())) {
  72. 72:             $this->value = TRUE;
  73. 73:             $form->setSubmittedBy($this);
  74. 74:         }
  75. 75:         return $this;
  76. 76:     }
  77. 77:  
  78. 78:  
  79. 79:  
  80. 80:     /**
  81. 81:      * Tells if the form was submitted by this button.
  82. 82:      * @return bool 
  83. 83:      */
  84. 84:     public function isSubmittedBy()
  85. 85:     {
  86. 86:         return $this->getForm()->isSubmitted(=== $this;
  87. 87:     }
  88. 88:  
  89. 89:  
  90. 90:  
  91. 91:     /**
  92. 92:      * Sets the validation scope. Clicking the button validates only the controls within the specified scope.
  93. 93:      * @param  mixed 
  94. 94:      * @return SubmitButton  provides a fluent interface
  95. 95:      */
  96. 96:     public function setValidationScope($scope)
  97. 97:     {
  98. 98:         // TODO: implement groups
  99. 99:         $this->validationScope = (bool) $scope;
  100. 100:         return $this;
  101. 101:     }
  102. 102:  
  103. 103:  
  104. 104:  
  105. 105:     /**
  106. 106:      * Gets the validation scope.
  107. 107:      * @return mixed 
  108. 108:      */
  109. 109:     final public function getValidationScope()
  110. 110:     {
  111. 111:         return $this->validationScope;
  112. 112:     }
  113. 113:  
  114. 114:  
  115. 115:  
  116. 116:     /**
  117. 117:      * Fires click event.
  118. 118:      * @return void 
  119. 119:      */
  120. 120:     public function click()
  121. 121:     {
  122. 122:         $this->onClick($this);
  123. 123:     }
  124. 124:  
  125. 125:  
  126. 126:  
  127. 127:     /**
  128. 128:      * Submitted validator: has been button pressed?
  129. 129:      * @param  ISubmitterControl 
  130. 130:      * @return bool 
  131. 131:      */
  132. 132:     public static function validateSubmitted(ISubmitterControl $control)
  133. 133:     {
  134. 134:         return $control->isSubmittedBy();
  135. 135:     }
  136. 136: