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

nette / forms / 10256859813

05 Aug 2024 10:20PM UTC coverage: 93.004% (-0.07%) from 93.07%
10256859813

push

github

dg
added HTML attribute data-nette-error

1 of 1 new or added line in 1 file covered. (100.0%)

28 existing lines in 6 files now uncovered.

2087 of 2244 relevant lines covered (93.0%)

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

15

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

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

33

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

40

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

49

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

58

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

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

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

84

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

94

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

103

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

111
                return parent::getControl($caption)->addAttributes([
1✔
112
                        'formnovalidate' => $this->validationScope !== null,
1✔
113
                        'data-nette-validation-scope' => $scope ?: null,
1✔
114
                ]);
115
        }
116
}
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