1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (http://nette.org)
5: *
6: * Copyright (c) 2004, 2011 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: */
23: class SubmitButton extends Button implements ISubmitterControl
24: {
25: /** @var array of function(SubmitButton $sender); Occurs when the button is clicked and form is successfully validated */
26: public $onClick;
27:
28: /** @var array of function(SubmitButton $sender); Occurs when the button is clicked and form is not validated */
29: public $onInvalidClick;
30:
31: /** @var mixed */
32: private $validationScope = TRUE;
33:
34:
35:
36: /**
37: * @param string caption
38: */
39: public function __construct($caption = NULL)
40: {
41: parent::__construct($caption);
42: $this->control->type = 'submit';
43: }
44:
45:
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:
64:
65: /**
66: * Tells if the form was submitted by this button.
67: * @return bool
68: */
69: public function isSubmittedBy()
70: {
71: return $this->getForm()->isSubmitted() === $this;
72: }
73:
74:
75:
76: /**
77: * Sets the validation scope. Clicking the button validates only the controls within the specified scope.
78: * @param mixed
79: * @return SubmitButton provides a fluent interface
80: */
81: public function setValidationScope($scope)
82: {
83: // TODO: implement groups
84: $this->validationScope = (bool) $scope;
85: $this->control->formnovalidate = !$this->validationScope;
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: