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

nette / forms / 8860576145

27 Apr 2024 02:35PM UTC coverage: 93.067% (-0.05%) from 93.113%
8860576145

push

github

dg
added HTML attribute data-nette-error

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

33 existing lines in 5 files now uncovered.

2094 of 2250 relevant lines covered (93.07%)

0.93 hits per line

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

97.62
/src/Forms/Controls/TextBase.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\Form;
14
use Nette\Utils\Strings;
15
use Stringable;
16

17

18
/**
19
 * Implements the basic functionality common to text input controls.
20
 */
21
abstract class TextBase extends BaseControl
22
{
23
        protected string $emptyValue = '';
24
        protected mixed $rawValue = '';
25
        private bool $nullable = false;
26

27

28
        /**
29
         * Sets control's value.
30
         * @internal
31
         */
32
        public function setValue($value): static
33
        {
34
                if ($value === null) {
1✔
35
                        $value = '';
1✔
36
                } elseif (!is_scalar($value) && !$value instanceof Stringable) {
1✔
37
                        throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or null, %s given in field '%s'.", get_debug_type($value), $this->getName()));
1✔
38
                }
39

40
                $this->value = $value;
1✔
41
                $this->rawValue = (string) $value;
1✔
42
                return $this;
1✔
43
        }
44

45

46
        /**
47
         * Returns control's value.
48
         * @return mixed
49
         */
50
        public function getValue(): mixed
51
        {
52
                $value = $this->value === Strings::trim($this->translate($this->emptyValue))
1✔
53
                        ? ''
1✔
54
                        : $this->value;
1✔
55
                return $this->nullable && $value === '' ? null : $value;
1✔
56
        }
57

58

59
        /**
60
         * Sets whether getValue() returns null instead of empty string.
61
         */
62
        public function setNullable(bool $value = true): static
1✔
63
        {
64
                $this->nullable = $value;
1✔
65
                return $this;
1✔
66
        }
67

68

69
        public function isNullable(): bool
70
        {
71
                return $this->nullable;
1✔
72
        }
73

74

75
        /**
76
         * Sets the special value which is treated as empty string.
77
         */
78
        public function setEmptyValue(string $value): static
1✔
79
        {
80
                $this->emptyValue = $value;
1✔
81
                return $this;
1✔
82
        }
83

84

85
        /**
86
         * Returns the special value which is treated as empty string.
87
         */
88
        public function getEmptyValue(): string
89
        {
UNCOV
90
                return $this->emptyValue;
×
91
        }
92

93

94
        /**
95
         * Sets the maximum number of allowed characters.
96
         */
97
        public function setMaxLength(int $length): static
1✔
98
        {
99
                $this->control->maxlength = $length;
1✔
100
                return $this;
1✔
101
        }
102

103

104
        public function getControl(): Nette\Utils\Html
105
        {
106
                $el = parent::getControl();
1✔
107
                if ($this->emptyValue !== '') {
1✔
108
                        $el->attrs['data-nette-empty-value'] = Strings::trim($this->translate($this->emptyValue));
1✔
109
                }
110

111
                if (isset($el->placeholder)) {
1✔
112
                        $el->placeholder = $this->translate($el->placeholder);
1✔
113
                }
114

115
                return $el;
1✔
116
        }
117

118

119
        protected function getRenderedValue(): ?string
120
        {
121
                return $this->rawValue === ''
1✔
122
                        ? ($this->emptyValue === '' ? null : $this->translate($this->emptyValue))
1✔
123
                        : $this->rawValue;
1✔
124
        }
125

126

127
        public function addRule(
1✔
128
                callable|string $validator,
129
                string|Stringable|null $errorMessage = null,
130
                mixed $arg = null,
131
        ): static
132
        {
133
                foreach ($this->getRules() as $rule) {
1✔
134
                        if (!$rule->canExport() && !$rule->branch) {
1✔
135
                                return parent::addRule($validator, $errorMessage, $arg);
1✔
136
                        }
137
                }
138

139
                if ($validator === Form::Length || $validator === Form::MaxLength) {
1✔
140
                        $tmp = is_array($arg) ? $arg[1] : $arg;
1✔
141
                        if (is_scalar($tmp)) {
1✔
142
                                $this->control->maxlength = isset($this->control->maxlength)
1✔
143
                                        ? min($this->control->maxlength, $tmp)
1✔
144
                                        : $tmp;
1✔
145
                        }
146
                }
147

148
                return parent::addRule($validator, $errorMessage, $arg);
1✔
149
        }
150
}
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