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

contributte / forms-bootstrap / #71

22 Apr 2024 10:51AM UTC coverage: 83.039%. Remained the same
#71

Pull #95

github

dakorpar
compativility with nette forms 3.2.2
Pull Request #95: Nette forms3.2.2

705 of 849 relevant lines covered (83.04%)

5.64 hits per line

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

88.1
/src/BootstrapForm.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\FormsBootstrap;
4

5
use Contributte\FormsBootstrap\Enums\BootstrapVersion;
6
use Contributte\FormsBootstrap\Traits\AddRowTrait;
7
use Contributte\FormsBootstrap\Traits\BootstrapContainerTrait;
8
use Nette\Application\UI\Form;
9
use Nette\ComponentModel\IContainer;
10
use Nette\Forms\FormRenderer;
11
use Nette\InvalidArgumentException;
12
use Nette\Utils\Html;
13

14
/**
15
 * Form rendered using Bootstrap 4
16
 *
17
 * @property bool $ajax
18
 * @property int  $renderMode
19
 * @property bool $showValidation     If valid fields should explicitly be green if valid
20
 * @property bool $autoShowValidation If true, valid inputs will be explicitly green on unsuccessful submit
21
 */
22
class BootstrapForm extends Form
23
{
24

25
        use BootstrapContainerTrait;
26
        use AddRowTrait;
27

28
        /** @var bool */
29
        public static $allwaysUseNullable = false;
30

31
        /** @var string Class to be added if this is ajax. Defaults to 'ajax' */
32
        public $ajaxClass = 'ajax';
33

34
        /** @var Html */
35
        protected $elementPrototype;
36

37
        /** @var int */
38
        private static $bootstrapVersion = BootstrapVersion::V4;
39

40
        /** @var bool */
41
        private $isAjax = true;
42

43
        /** @var bool */
44
        private $showValidation = false;
45

46
        /** @var bool */
47
        private $autoShowValidation = true;
48

49
        /**
50
         * @param IContainer|null $container
51
         */
52
        public function __construct($container = null)
53
        {
54
                parent::__construct($container);
47✔
55

56
                $this->setRenderer(new BootstrapRenderer());
47✔
57

58
                $prototype = Html::el('form', [
47✔
59
                        'action' => '',
47✔
60
                        'method' => self::Post,
47✔
61
                        'class' => [],
47✔
62
                ]);
47✔
63
                $this->elementPrototype = $prototype;
47✔
64

65
                /**
66
                 * @param BootstrapForm $form
67
                 */
68
                $this->onError[] = function ($form): void {
47✔
69
                        $form->showValidation = $this->autoShowValidation;
×
70
                };
47✔
71
        }
72

73
        public static function switchBootstrapVersion(int $version): void
74
        {
75
                self::$bootstrapVersion = in_array($version, [BootstrapVersion::V4, BootstrapVersion::V5], true)
7✔
76
                        ? $version
7✔
77
                        : BootstrapVersion::V4;
×
78
        }
79

80
        public static function getBootstrapVersion(): int
81
        {
82
                return self::$bootstrapVersion;
27✔
83
        }
84

85
        public function getElementPrototype(): Html
86
        {
87
                return $this->elementPrototype;
20✔
88
        }
89

90
        /**
91
         * @return BootstrapRenderer
92
         */
93
        public function getRenderer(): FormRenderer
94
        {
95
                /** @var BootstrapRenderer $renderer */
96
                $renderer = parent::getRenderer();
14✔
97

98
                return $renderer;
14✔
99
        }
100

101
        public function setRenderer(?FormRenderer $renderer = null): static
102
        {
103
                if (!$renderer instanceof BootstrapRenderer) {
47✔
104
                        throw new InvalidArgumentException('Renderer must be a BootstrapRenderer class');
1✔
105
                }
106

107
                parent::setRenderer($renderer);
47✔
108

109
                return $this;
47✔
110
        }
111

112
        public function getRenderMode(): int
113
        {
114
                return $this->getRenderer()->getMode();
1✔
115
        }
116

117
        /**
118
         * @return bool if form is ajax. True by default.
119
         */
120
        public function isAjax(): bool
121
        {
122
                return $this->isAjax;
1✔
123
        }
124

125
        public function isAutoShowValidation(): bool
126
        {
127
                return $this->autoShowValidation;
1✔
128
        }
129

130
        public function setAutoShowValidation(bool $autoShowValidation): BootstrapForm
131
        {
132
                $this->autoShowValidation = $autoShowValidation;
1✔
133

134
                return $this;
1✔
135
        }
136

137
        /**
138
         * If valid fields should explicitly be green
139
         */
140
        public function isShowValidation(): bool
141
        {
142
                return $this->showValidation;
12✔
143
        }
144

145
        /**
146
         * If valid fields should explicitly be green
147
         *
148
         * @return static
149
         */
150
        public function setShowValidation(bool $showValidation)
151
        {
152
                $this->showValidation = $showValidation;
×
153

154
                return $this;
×
155
        }
156

157
        /**
158
         * @return static
159
         */
160
        public function setAjax(bool $isAjax = true)
161
        {
162
                $this->isAjax = $isAjax;
1✔
163

164
                BootstrapUtils::standardizeClass($this->getElementPrototype());
1✔
165
                $prototypeClass = $this->getElementPrototype()->class;
1✔
166

167
                $present = in_array($this->ajaxClass, $prototypeClass);
1✔
168
                if ($present && !$isAjax) {
1✔
169
                        // remove the class
170
                        $prototypeClass = array_diff($prototypeClass, [$this->ajaxClass]);
×
171
                } elseif (!$present && $isAjax) {
1✔
172
                        // add class
173
                        $prototypeClass[] = $this->ajaxClass;
1✔
174
                }
175

176
                $this->getElementPrototype()->class = $prototypeClass;
1✔
177

178
                return $this;
1✔
179
        }
180

181
        /**
182
         * @return static
183
         */
184
        public function setRenderMode(int $renderMode)
185
        {
186
                $this->getRenderer()->setMode($renderMode);
1✔
187

188
                return $this;
1✔
189
        }
190

191
}
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