1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Forms\Controls;
9:
10: use Nette;
11: use Nette\Forms\Form;
12:
13:
14: 15: 16:
17: class TextInput extends TextBase
18: {
19:
20: 21: 22: 23:
24: public function __construct($label = NULL, $maxLength = NULL)
25: {
26: parent::__construct($label);
27: $this->control->maxlength = $maxLength;
28: $this->setOption('type', 'text');
29: }
30:
31:
32: 33: 34: 35:
36: public function loadHttpData()
37: {
38: $this->setValue($this->getHttpData(Form::DATA_LINE));
39: }
40:
41:
42: 43: 44: 45: 46:
47: public function setHtmlType($type)
48: {
49: return $this->setType($type);
50: }
51:
52:
53: 54: 55: 56: 57:
58: public function setType($type)
59: {
60: $this->control->type = $type;
61: return $this;
62: }
63:
64:
65: 66: 67: 68:
69: public function getControl()
70: {
71: return parent::getControl()->addAttributes([
72: 'value' => $this->control->type === 'password' ? $this->control->value : $this->getRenderedValue(),
73: 'type' => $this->control->type ?: 'text',
74: ]);
75: }
76:
77:
78: public function addRule($validator, $message = NULL, $arg = NULL)
79: {
80: if ($this->control->type === NULL && in_array($validator, [Form::EMAIL, Form::URL, Form::INTEGER], TRUE)) {
81: static $types = [Form::EMAIL => 'email', Form::URL => 'url', Form::INTEGER => 'number'];
82: $this->control->type = $types[$validator];
83:
84: } elseif (in_array($validator, [Form::MIN, Form::MAX, Form::RANGE], TRUE)
85: && in_array($this->control->type, ['number', 'range', 'datetime-local', 'datetime', 'date', 'month', 'week', 'time'], TRUE)
86: ) {
87: if ($validator === Form::MIN) {
88: $range = [$arg, NULL];
89: } elseif ($validator === Form::MAX) {
90: $range = [NULL, $arg];
91: } else {
92: $range = $arg;
93: }
94: if (isset($range[0]) && is_scalar($range[0])) {
95: $this->control->min = isset($this->control->min) ? max($this->control->min, $range[0]) : $range[0];
96: }
97: if (isset($range[1]) && is_scalar($range[1])) {
98: $this->control->max = isset($this->control->max) ? min($this->control->max, $range[1]) : $range[1];
99: }
100:
101: } elseif ($validator === Form::PATTERN && is_scalar($arg)
102: && in_array($this->control->type, [NULL, 'text', 'search', 'tel', 'url', 'email', 'password'], TRUE)
103: ) {
104: $this->control->pattern = $arg;
105: }
106:
107: return parent::addRule($validator, $message, $arg);
108: }
109:
110: }
111: