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