• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / forms / 15763260878

19 Jun 2025 05:19PM UTC coverage: 93.011%. Remained the same
15763260878

push

github

dg
netteForms: restructured package, includes UMD and ESM (BC break)

2076 of 2232 relevant lines covered (93.01%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.67
/src/Forms/Controls/SubmitButton.php
1
<?php
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
declare(strict_types=1);
9

10
namespace Nette\Forms\Controls;
11

12
use Nette;
13
use Stringable;
14
use function is_string;
15

16

17
/**
18
 * Submittable button control.
19
 *
20
 * @property-read bool $submittedBy
21
 */
22
class SubmitButton extends Button implements Nette\Forms\SubmitterControl
23
{
24
        /**
25
         * Occurs when the button is clicked and form is successfully validated
26
         * @var array<callable(self, array|object): void|callable(Nette\Forms\Form, array|object): void|callable(array|object): void>
27
         */
28
        public array $onClick = [];
29

30
        /** @var array<callable(self): void>  Occurs when the button is clicked and form is not validated */
31
        public array $onInvalidClick = [];
32
        private ?array $validationScope = null;
33

34

35
        public function __construct(string|Stringable|null $caption = null)
1✔
36
        {
37
                parent::__construct($caption);
1✔
38
                $this->control->type = 'submit';
1✔
39
        }
1✔
40

41

42
        public function loadHttpData(): void
43
        {
44
                parent::loadHttpData();
1✔
45
                if ($this->isFilled()) {
1✔
46
                        $this->getForm()->setSubmittedBy($this);
1✔
47
                }
48
        }
1✔
49

50

51
        /**
52
         * Tells if the form was submitted by this button.
53
         */
54
        public function isSubmittedBy(): bool
55
        {
56
                return $this->getForm()->isSubmitted() === $this;
1✔
57
        }
58

59

60
        /**
61
         * Sets the validation scope. Clicking the button validates only the controls within the specified scope.
62
         * @param  ?iterable<Nette\Forms\Control|Nette\Forms\Container|string>  $scope
63
         */
64
        public function setValidationScope(?iterable $scope): static
1✔
65
        {
66
                if ($scope === null) {
1✔
67
                        $this->validationScope = null;
×
68
                        return $this;
×
69
                }
70

71
                $this->validationScope = [];
1✔
72
                foreach ($scope as $control) {
1✔
73
                        if (is_string($control)) {
1✔
74
                                $control = $this->getForm()->getComponent($control);
1✔
75
                        }
76
                        if (!$control instanceof Nette\Forms\Container && !$control instanceof Nette\Forms\Control) {
1✔
77
                                throw new Nette\InvalidArgumentException('Validation scope accepts only Nette\Forms\Container or Nette\Forms\Control instances.');
×
78
                        }
79

80
                        $this->validationScope[] = $control;
1✔
81
                }
82
                return $this;
1✔
83
        }
84

85

86
        /**
87
         * Gets the validation scope.
88
         * @return ?array<Nette\Forms\Control|Nette\Forms\Container>
89
         */
90
        public function getValidationScope(): ?array
91
        {
92
                return $this->validationScope;
1✔
93
        }
94

95

96
        /**
97
         * Fires click event.
98
         */
99
        public function click(): void
100
        {
101
                Nette\Utils\Arrays::invoke($this->onClick, $this);
×
102
        }
103

104

105
        public function getControl($caption = null): Nette\Utils\Html
1✔
106
        {
107
                $scope = [];
1✔
108
                foreach ((array) $this->validationScope as $control) {
1✔
109
                        $scope[] = $control->lookupPath(Nette\Forms\Form::class);
1✔
110
                }
111

112
                return parent::getControl($caption)->addAttributes([
1✔
113
                        'formnovalidate' => $this->validationScope !== null,
1✔
114
                        'data-nette-validation-scope' => $scope ?: null,
1✔
115
                ]);
116
        }
117
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc