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