PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Nette/Forms/Controls/SubmitButton.php

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