PluginProbe
Packeta / 2.0.9
Packeta v2.0.9
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / deps / nette / forms / src / Forms / Controls / TextInput.php

TextInput.php in Packeta 2.0.9, at deps/nette/forms/src/Forms/Controls/TextInput.php

82 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 declare (strict_types=1);
8 namespace Packetery\Nette\Forms\Controls;
9
10 use Packetery\Nette;
11 use Packetery\Nette\Forms\Form;
12 /**
13 * Single line text input control.
14 */
15 class TextInput extends TextBase
16 {
17 /**
18 * @param string|object $label
19 */
20 public function __construct($label = null, int $maxLength = null)
21 {
22 parent::__construct($label);
23 $this->control->maxlength = $maxLength;
24 $this->setOption('type', 'text');
25 }
26 public function loadHttpData() : void
27 {
28 $this->setValue($this->getHttpData(Form::DATA_LINE));
29 }
30 /**
31 * Changes control's type attribute.
32 * @return static
33 */
34 public function setHtmlType(string $type)
35 {
36 $this->control->type = $type;
37 return $this;
38 }
39 /**
40 * @deprecated use setHtmlType()
41 * @return static
42 */
43 public function setType(string $type)
44 {
45 return $this->setHtmlType($type);
46 }
47 public function getControl() : \Packetery\Nette\Utils\Html
48 {
49 return parent::getControl()->addAttributes(['value' => $this->control->type === 'password' ? $this->control->value : $this->getRenderedValue(), 'type' => $this->control->type ?: 'text']);
50 }
51 /** @return static */
52 public function addRule($validator, $errorMessage = null, $arg = null)
53 {
54 foreach ($this->getRules() as $rule) {
55 if (!$rule->canExport() && !$rule->branch) {
56 return parent::addRule($validator, $errorMessage, $arg);
57 }
58 }
59 if ($this->control->type === null && \in_array($validator, [Form::EMAIL, Form::URL, Form::INTEGER], \true)) {
60 static $types = [Form::EMAIL => 'email', Form::URL => 'url', Form::INTEGER => 'number'];
61 $this->control->type = $types[$validator];
62 } elseif (\in_array($validator, [Form::MIN, Form::MAX, Form::RANGE], \true) && \in_array($this->control->type, ['number', 'range', 'datetime-local', 'datetime', 'date', 'month', 'week', 'time'], \true)) {
63 if ($validator === Form::MIN) {
64 $range = [$arg, null];
65 } elseif ($validator === Form::MAX) {
66 $range = [null, $arg];
67 } else {
68 $range = $arg;
69 }
70 if (isset($range[0]) && \is_scalar($range[0])) {
71 $this->control->min = isset($this->control->min) ? \max($this->control->min, $range[0]) : $range[0];
72 }
73 if (isset($range[1]) && \is_scalar($range[1])) {
74 $this->control->max = isset($this->control->max) ? \min($this->control->max, $range[1]) : $range[1];
75 }
76 } elseif ($validator === Form::PATTERN && \is_scalar($arg) && \in_array($this->control->type, [null, 'text', 'search', 'tel', 'url', 'email', 'password'], \true)) {
77 $this->control->pattern = $arg;
78 }
79 return parent::addRule($validator, $errorMessage, $arg);
80 }
81 }
82