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

nette / forms / 20836962018

09 Jan 2026 12:35AM UTC coverage: 93.481%. Remained the same
20836962018

push

github

dg
Container::setValues() and setDefaults() accepts array|Traversable|stdClass (BC break)

3 of 4 new or added lines in 1 file covered. (75.0%)

44 existing lines in 9 files now uncovered.

2065 of 2209 relevant lines covered (93.48%)

0.93 hits per line

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

95.14
/src/Forms/Controls/BaseControl.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 Nette\Forms\Control;
14
use Nette\Forms\Form;
15
use Nette\Forms\Rules;
16
use Nette\Utils\Html;
17
use Stringable;
18
use function array_unique, explode, func_get_arg, func_num_args, get_parent_class, implode, is_array, sprintf, str_contains;
19

20

21
/**
22
 * Base class that implements the basic functionality common to form controls.
23
 *
24
 * @property-read Form $form
25
 * @property-deprecated string $htmlName
26
 * @property-deprecated   string|bool|null $htmlId
27
 * @property   mixed $value
28
 * @property-deprecated   string|Stringable $caption
29
 * @property   bool $disabled
30
 * @property-deprecated   bool $omitted
31
 * @property-read Html $control
32
 * @property-read Html $label
33
 * @property-read Html $controlPrototype
34
 * @property-read Html $labelPrototype
35
 * @property   bool $required
36
 * @property-deprecated bool $filled
37
 * @property-read array $errors
38
 * @property-deprecated array $options
39
 * @property-read string $error
40
 */
41
abstract class BaseControl extends Nette\ComponentModel\Component implements Control
42
{
43
        public static string $idMask = 'frm-%s';
44

45
        protected mixed $value = null;
46
        protected Html $control;
47
        protected Html $label;
48

49
        /** @var bool|bool[] */
50
        protected bool|array $disabled = false;
51

52
        /** @var callable[][]  extension methods */
53
        private static array $extMethods = [];
54
        private string|Stringable|null $caption;
55

56
        /** @var string[] */
57
        private array $errors = [];
58
        private ?bool $omitted = null;
59
        private Rules $rules;
60

61
        /** true means autodetect */
62
        private Nette\Localization\Translator|bool|null $translator = true;
63

64
        /** @var array<string, mixed> */
65
        private array $options = [];
66

67

68
        public function __construct(string|Stringable|null $caption = null)
1✔
69
        {
70
                $this->control = Html::el('input', ['type' => null, 'name' => null]);
1✔
71
                $this->label = Html::el('label');
1✔
72
                $this->caption = $caption;
1✔
73
                $this->rules = new Rules($this);
1✔
74
                $this->setValue(null);
1✔
75
                $this->monitor(Form::class, function (Form $form): void {
1✔
76
                        if (!$this->isDisabled() && $form->isAnchored() && $form->isSubmitted()) {
1✔
77
                                $this->loadHttpData();
1✔
78
                        }
79
                });
1✔
80
        }
1✔
81

82

83
        /**
84
         * Sets textual caption or label.
85
         */
86
        public function setCaption(string|Stringable|null $caption): static
87
        {
UNCOV
88
                $this->caption = $caption;
×
UNCOV
89
                return $this;
×
90
        }
91

92

93
        public function getCaption(): string|Stringable|null
94
        {
95
                return $this->caption;
1✔
96
        }
97

98

99
        /**
100
         * Returns form.
101
         * @return ($throw is true ? Form : ?Form)
102
         */
103
        public function getForm(bool $throw = true): ?Form
1✔
104
        {
105
                return $this->lookup(Form::class, $throw);
1✔
106
        }
107

108

109
        /**
110
         * Loads HTTP data.
111
         */
112
        public function loadHttpData(): void
113
        {
114
                $this->setValue($this->getHttpData(Form::DataText));
1✔
115
        }
1✔
116

117

118
        /**
119
         * Loads HTTP data.
120
         */
121
        protected function getHttpData($type, ?string $htmlTail = null): mixed
1✔
122
        {
123
                return $this->getForm()->getHttpData($type, $this->getHtmlName() . $htmlTail);
1✔
124
        }
125

126

127
        /**
128
         * Returns HTML name of control.
129
         */
130
        public function getHtmlName(): string
131
        {
132
                return $this->control->name ?? Nette\Forms\Helpers::generateHtmlName($this->lookupPath(Form::class));
1✔
133
        }
134

135

136
        /********************* interface Control ****************d*g**/
137

138

139
        /**
140
         * Sets control's value.
141
         * @return static
142
         * @internal
143
         */
144
        public function setValue(mixed $value)
1✔
145
        {
146
                $this->value = $value;
1✔
147
                return $this;
1✔
148
        }
149

150

151
        /**
152
         * Returns control's value.
153
         * @return mixed
154
         */
155
        public function getValue()
156
        {
157
                return $this->value;
1✔
158
        }
159

160

161
        /**
162
         * Is control filled?
163
         */
164
        public function isFilled(): bool
165
        {
166
                $value = $this->getValue();
1✔
167
                return $value !== null && $value !== [] && $value !== '';
1✔
168
        }
169

170

171
        /**
172
         * Sets control's default value.
173
         * @return static
174
         */
175
        public function setDefaultValue($value)
176
        {
177
                $form = $this->getForm(throw: false);
1✔
178
                if ($this->isDisabled() || !$form || !$form->isAnchored() || !$form->isSubmitted()) {
1✔
179
                        $this->setValue($value);
1✔
180
                }
181

182
                return $this;
1✔
183
        }
184

185

186
        /**
187
         * Disables or enables control.
188
         * @return static
189
         */
190
        public function setDisabled(bool $state = true)
1✔
191
        {
192
                $this->disabled = $state;
1✔
193
                if ($state) {
1✔
194
                        $this->setValue(null);
1✔
195
                } elseif (($form = $this->getForm(throw: false)) && $form->isAnchored() && $form->isSubmitted()) {
1✔
196
                        $this->loadHttpData();
1✔
197
                }
198

199
                return $this;
1✔
200
        }
201

202

203
        /**
204
         * Is control disabled?
205
         */
206
        public function isDisabled(): bool
207
        {
208
                return $this->disabled === true;
1✔
209
        }
210

211

212
        /**
213
         * Sets whether control value is excluded from $form->getValues() result.
214
         */
215
        public function setOmitted(bool $state = true): static
1✔
216
        {
217
                $this->omitted = $state;
1✔
218
                return $this;
1✔
219
        }
220

221

222
        /**
223
         * Is control value excluded from $form->getValues() result?
224
         */
225
        public function isOmitted(): bool
226
        {
227
                return $this->omitted || ($this->isDisabled() && $this->omitted === null);
1✔
228
        }
229

230

231
        /********************* rendering ****************d*g**/
232

233

234
        /**
235
         * Generates control's HTML element.
236
         * @return Html|string
237
         */
238
        public function getControl()
239
        {
240
                $this->setOption('rendered', true);
1✔
241
                $el = clone $this->control;
1✔
242
                return $el->addAttributes([
1✔
243
                        'name' => $this->getHtmlName(),
1✔
244
                        'id' => $this->getHtmlId(),
1✔
245
                        'required' => $this->isRequired(),
1✔
246
                        'disabled' => $this->isDisabled(),
1✔
247
                        'data-nette-rules' => Nette\Forms\Helpers::exportRules($this->rules) ?: null,
1✔
248
                ]);
249
        }
250

251

252
        /**
253
         * Generates label's HTML element.
254
         * @return Html|string|null
255
         */
256
        public function getLabel(string|Stringable|null $caption = null)
1✔
257
        {
258
                $label = clone $this->label;
1✔
259
                $label->for = $this->getHtmlId();
1✔
260
                $caption ??= $this->caption;
1✔
261
                $translator = $this->getForm()->getTranslator();
1✔
262
                $label->setText($translator && !$caption instanceof Nette\HtmlStringable ? $translator->translate($caption) : $caption);
1✔
263
                return $label;
1✔
264
        }
265

266

267
        public function getControlPart(): ?Html
268
        {
269
                return $this->getControl();
1✔
270
        }
271

272

273
        public function getLabelPart(): ?Html
274
        {
275
                return $this->getLabel();
1✔
276
        }
277

278

279
        /**
280
         * Returns control's HTML element template.
281
         */
282
        public function getControlPrototype(): Html
283
        {
284
                return $this->control;
1✔
285
        }
286

287

288
        /**
289
         * Returns label's HTML element template.
290
         */
291
        public function getLabelPrototype(): Html
292
        {
UNCOV
293
                return $this->label;
×
294
        }
295

296

297
        /**
298
         * Changes control's HTML id.
299
         */
300
        public function setHtmlId(string|bool|null $id): static
1✔
301
        {
302
                $this->control->id = $id;
1✔
303
                return $this;
1✔
304
        }
305

306

307
        /**
308
         * Returns control's HTML id.
309
         */
310
        public function getHtmlId(): string|bool|null
311
        {
312
                if (!isset($this->control->id)) {
1✔
313
                        $form = $this->getForm();
1✔
314
                        $prefix = $form instanceof Nette\Application\UI\Form || $form->getName() === null
1✔
315
                                ? ''
1✔
316
                                : $form->getName() . '-';
1✔
317
                        $this->control->id = sprintf(self::$idMask, $prefix . $this->lookupPath());
1✔
318
                }
319

320
                return $this->control->id;
1✔
321
        }
322

323

324
        /**
325
         * Changes control's HTML attribute.
326
         */
327
        public function setHtmlAttribute(string $name, mixed $value = true): static
1✔
328
        {
329
                $this->control->$name = $value;
1✔
330
                if (
331
                        $name === 'name'
1✔
332
                        && ($form = $this->getForm(throw: false))
1✔
333
                        && !$this->isDisabled()
1✔
334
                        && $form->isAnchored()
1✔
335
                        && $form->isSubmitted()
1✔
336
                ) {
337
                        $this->loadHttpData();
1✔
338
                }
339

340
                return $this;
1✔
341
        }
342

343

344
        /**
345
         * @deprecated  use setHtmlAttribute()
346
         */
347
        public function setAttribute(string $name, mixed $value = true): static
348
        {
UNCOV
349
                return $this->setHtmlAttribute($name, $value);
×
350
        }
351

352

353
        /********************* translator ****************d*g**/
354

355

356
        /**
357
         * Sets translate adapter.
358
         */
359
        public function setTranslator(?Nette\Localization\Translator $translator): static
1✔
360
        {
361
                $this->translator = $translator;
1✔
362
                return $this;
1✔
363
        }
364

365

366
        /**
367
         * Returns translate adapter.
368
         */
369
        public function getTranslator(): ?Nette\Localization\Translator
370
        {
371
                if ($this->translator === true) {
1✔
372
                        return $this->getForm(throw: false)
1✔
373
                                ? $this->getForm()->getTranslator()
1✔
374
                                : null;
1✔
375
                }
376

377
                return $this->translator;
1✔
378
        }
379

380

381
        /**
382
         * Returns translated string.
383
         */
384
        public function translate($value, ...$parameters): mixed
1✔
385
        {
386
                if ($translator = $this->getTranslator()) {
1✔
387
                        $tmp = is_array($value) ? [&$value] : [[&$value]];
1✔
388
                        foreach ($tmp[0] as &$v) {
1✔
389
                                if ($v != null && !$v instanceof Nette\HtmlStringable) { // intentionally ==
1✔
390
                                        $v = $translator->translate($v, ...$parameters);
1✔
391
                                }
392
                        }
393
                }
394

395
                return $value;
1✔
396
        }
397

398

399
        /********************* rules ****************d*g**/
400

401

402
        /**
403
         * Adds a validation rule.
404
         * @return static
405
         */
406
        public function addRule(
1✔
407
                callable|string $validator,
408
                string|Stringable|null $errorMessage = null,
409
                mixed $arg = null,
410
        ) {
411
                $this->rules->addRule($validator, $errorMessage, $arg);
1✔
412
                return $this;
1✔
413
        }
414

415

416
        /**
417
         * Adds a validation condition a returns new branch.
418
         */
419
        public function addCondition($validator, $value = null): Rules
1✔
420
        {
421
                return $this->rules->addCondition($validator, $value);
1✔
422
        }
423

424

425
        /**
426
         * Adds a validation condition based on another control a returns new branch.
427
         */
428
        public function addConditionOn(Control $control, $validator, $value = null): Rules
1✔
429
        {
430
                return $this->rules->addConditionOn($control, $validator, $value);
1✔
431
        }
432

433

434
        /**
435
         * Adds an input filter callback.
436
         */
437
        public function addFilter(callable $filter): static
1✔
438
        {
439
                $this->getRules()->addFilter($filter);
1✔
440
                return $this;
1✔
441
        }
442

443

444
        public function getRules(): Rules
445
        {
446
                return $this->rules;
1✔
447
        }
448

449

450
        /**
451
         * Makes control mandatory.
452
         */
453
        public function setRequired(string|Stringable|bool $value = true): static
1✔
454
        {
455
                $this->rules->setRequired($value);
1✔
456
                return $this;
1✔
457
        }
458

459

460
        /**
461
         * Is control mandatory?
462
         */
463
        public function isRequired(): bool
464
        {
465
                return $this->rules->isRequired();
1✔
466
        }
467

468

469
        /**
470
         * Performs the server side validation.
471
         */
472
        public function validate(): void
473
        {
474
                if ($this->isDisabled()) {
1✔
475
                        return;
1✔
476
                }
477

478
                $this->cleanErrors();
1✔
479
                $this->rules->validate();
1✔
480
        }
1✔
481

482

483
        /**
484
         * Adds error message to the list.
485
         */
486
        public function addError(string|Stringable $message, bool $translate = true): void
1✔
487
        {
488
                $this->errors[] = $translate ? $this->translate($message) : $message;
1✔
489
        }
1✔
490

491

492
        /**
493
         * Returns errors corresponding to control.
494
         */
495
        public function getError(): ?string
496
        {
497
                return $this->errors ? implode(' ', array_unique($this->errors)) : null;
1✔
498
        }
499

500

501
        /**
502
         * Returns errors corresponding to control.
503
         */
504
        public function getErrors(): array
505
        {
506
                return array_unique($this->errors);
1✔
507
        }
508

509

510
        public function hasErrors(): bool
511
        {
512
                return (bool) $this->errors;
1✔
513
        }
514

515

516
        public function cleanErrors(): void
517
        {
518
                $this->errors = [];
1✔
519
        }
1✔
520

521

522
        /********************* user data ****************d*g**/
523

524

525
        /**
526
         * Sets user-specific option.
527
         */
528
        public function setOption($key, mixed $value): static
1✔
529
        {
530
                if ($value === null) {
1✔
UNCOV
531
                        unset($this->options[$key]);
×
532
                } else {
533
                        $this->options[$key] = $value;
1✔
534
                }
535

536
                return $this;
1✔
537
        }
538

539

540
        /**
541
         * Returns user-specific option.
542
         */
543
        public function getOption($key): mixed
544
        {
545
                return $this->options[$key] ?? null;
1✔
546
        }
547

548

549
        /**
550
         * Returns user-specific options.
551
         * @return array<string, mixed>
552
         */
553
        public function getOptions(): array
554
        {
UNCOV
555
                return $this->options;
×
556
        }
557

558

559
        /********************* extension methods ****************d*g**/
560

561

562
        public function __call(string $name, array $args)
1✔
563
        {
564
                $class = static::class;
1✔
565
                do {
566
                        if (isset(self::$extMethods[$name][$class])) {
1✔
567
                                return (self::$extMethods[$name][$class])($this, ...$args);
1✔
568
                        }
569

570
                        $class = get_parent_class($class);
1✔
571
                } while ($class);
1✔
572

573
                return parent::__call($name, $args);
1✔
574
        }
575

576

577
        public static function extensionMethod(string $name, /*callable*/ $callback): void
1✔
578
        {
579
                if (str_contains($name, '::')) { // back compatibility
1✔
UNCOV
580
                        [, $name] = explode('::', $name);
×
581
                }
582

583
                self::$extMethods[$name][static::class] = $callback;
1✔
584
        }
1✔
585
}
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

© 2026 Coveralls, Inc