1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (http://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: * @property-read bool $submittedBy
17: * @property mixed $validationScope
18: */
19: class SubmitButton extends Button implements Nette\Forms\ISubmitterControl
20: {
21: /** @var callable[] function (SubmitButton $sender); Occurs when the button is clicked and form is successfully validated */
22: public $onClick;
23:
24: /** @var callable[] function (SubmitButton $sender); Occurs when the button is clicked and form is not validated */
25: public $onInvalidClick;
26:
27: /** @var array */
28: private $validationScope;
29:
30:
31: /**
32: * @param string caption
33: */
34: public function __construct($caption = NULL)
35: {
36: parent::__construct($caption);
37: $this->control->type = 'submit';
38: $this->setOmitted(TRUE);
39: }
40:
41:
42: /**
43: * Loads HTTP data.
44: * @return void
45: */
46: public function loadHttpData()
47: {
48: parent::loadHttpData();
49: if ($this->isFilled()) {
50: $this->getForm()->setSubmittedBy($this);
51: }
52: }
53:
54:
55: /**
56: * Tells if the form was submitted by this button.
57: * @return bool
58: */
59: public function isSubmittedBy()
60: {
61: return $this->getForm()->isSubmitted() === $this;
62: }
63:
64:
65: /**
66: * Sets the validation scope. Clicking the button validates only the controls within the specified scope.
67: * @return self
68: */
69: public function setValidationScope(/*array*/$scope = NULL)
70: {
71: if ($scope === NULL || $scope === TRUE) {
72: $this->validationScope = NULL;
73: } else {
74: $this->validationScope = array();
75: foreach ($scope ?: array() as $control) {
76: if (!$control instanceof Nette\Forms\Container && !$control instanceof Nette\Forms\IControl) {
77: throw new Nette\InvalidArgumentException('Validation scope accepts only Nette\Forms\Container or Nette\Forms\IControl instances.');
78: }
79: $this->validationScope[] = $control;
80: }
81: }
82: return $this;
83: }
84:
85:
86: /**
87: * Gets the validation scope.
88: * @return array|NULL
89: */
90: public function getValidationScope()
91: {
92: return $this->validationScope;
93: }
94:
95:
96: /**
97: * Fires click event.
98: * @return void
99: */
100: public function click()
101: {
102: $this->onClick($this);
103: }
104:
105:
106: /**
107: * Generates control's HTML element.
108: * @param string
109: * @return Nette\Utils\Html
110: */
111: public function getControl($caption = NULL)
112: {
113: $scope = array();
114: foreach ((array) $this->validationScope as $control) {
115: $scope[] = $control->lookupPath('Nette\Forms\Form');
116: }
117: return parent::getControl($caption)->addAttributes(array(
118: 'formnovalidate' => $this->validationScope !== NULL,
119: 'data-nette-validation-scope' => $scope ?: NULL,
120: ));
121: }
122:
123: }
124: