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