1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
6: */
7:
8: namespace Nette\Forms\Controls;
9:
10: use Nette;
11:
12:
13: /**
14: * Submittable button control.
15: *
16: * @author David Grudl
17: *
18: * @property-read bool $submittedBy
19: * @property mixed $validationScope
20: */
21: class SubmitButton extends Button implements Nette\Forms\ISubmitterControl
22: {
23: /** @var array of function(SubmitButton $sender); Occurs when the button is clicked and form is successfully validated */
24: public $onClick;
25:
26: /** @var array of function(SubmitButton $sender); Occurs when the button is clicked and form is not validated */
27: public $onInvalidClick;
28:
29: /** @var mixed */
30: private $validationScope = TRUE;
31:
32:
33: /**
34: * @param string caption
35: */
36: public function __construct($caption = NULL)
37: {
38: parent::__construct($caption);
39: $this->control->type = 'submit';
40: }
41:
42:
43: /**
44: * Sets 'pressed' indicator.
45: * @param bool
46: * @return self
47: */
48: public function setValue($value)
49: {
50: if ($this->value = $value !== NULL) {
51: $this->getForm()->setSubmittedBy($this);
52: }
53: return $this;
54: }
55:
56:
57: /**
58: * Tells if the form was submitted by this button.
59: * @return bool
60: */
61: public function isSubmittedBy()
62: {
63: return $this->getForm()->isSubmitted() === $this;
64: }
65:
66:
67: /**
68: * Sets the validation scope. Clicking the button validates only the controls within the specified scope.
69: * @param mixed
70: * @return self
71: */
72: public function setValidationScope($scope)
73: {
74: $this->validationScope = (bool) $scope;
75: $this->control->formnovalidate = !$this->validationScope;
76: return $this;
77: }
78:
79:
80: /**
81: * Gets the validation scope.
82: * @return mixed
83: */
84: public function getValidationScope()
85: {
86: return $this->validationScope;
87: }
88:
89:
90: /**
91: * Fires click event.
92: * @return void
93: */
94: public function click()
95: {
96: $this->onClick($this);
97: }
98:
99:
100: /**
101: * Submitted validator: has been button pressed?
102: * @return bool
103: * @internal
104: */
105: public static function validateSubmitted(Nette\Forms\ISubmitterControl $control)
106: {
107: return $control->isSubmittedBy();
108: }
109:
110: }
111: