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