1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (http://nette.org)
5: *
6: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
7: *
8: * For the full copyright and license information, please view
9: * the file license.txt that was distributed with this source code.
10: */
11:
12: namespace Nette\Forms\Controls;
13:
14: use Nette;
15:
16:
17:
18: /**
19: * Single line text input control.
20: *
21: * @author David Grudl
22: * @property-write $type
23: */
24: class TextInput extends TextBase
25: {
26:
27: /**
28: * @param string control name
29: * @param string label
30: * @param int width of the control
31: * @param int maximum number of characters the user may enter
32: */
33: public function __construct($label = NULL, $cols = NULL, $maxLength = NULL)
34: {
35: parent::__construct($label);
36: $this->control->type = 'text';
37: $this->control->size = $cols;
38: $this->control->maxlength = $maxLength;
39: $this->addFilter($this->sanitize);
40: $this->value = '';
41: }
42:
43:
44:
45: /**
46: * Filter: removes unnecessary whitespace and shortens value to control's max length.
47: * @return string
48: */
49: public function sanitize($value)
50: {
51: if ($this->control->maxlength && Nette\Utils\Strings::length($value) > $this->control->maxlength) {
52: $value = Nette\Utils\Strings::substring($value, 0, $this->control->maxlength);
53: }
54: return Nette\Utils\Strings::trim(strtr($value, "\r\n", ' '));
55: }
56:
57:
58:
59: /**
60: * Changes control's type attribute.
61: * @param string
62: * @return BaseControl provides a fluent interface
63: */
64: public function setType($type)
65: {
66: $this->control->type = $type;
67: return $this;
68: }
69:
70:
71:
72: /** @deprecated */
73: public function setPasswordMode($mode = TRUE)
74: {
75: $this->control->type = $mode ? 'password' : 'text';
76: return $this;
77: }
78:
79:
80:
81: /**
82: * Generates control's HTML element.
83: * @return Nette\Utils\Html
84: */
85: public function getControl()
86: {
87: $control = parent::getControl();
88: foreach ($this->getRules() as $rule) {
89: if ($rule->isNegative || $rule->type !== Nette\Forms\Rule::VALIDATOR) {
90:
91: } elseif ($rule->operation === Nette\Forms\Form::RANGE && $control->type !== 'text') {
92: list($control->min, $control->max) = $rule->arg;
93:
94: } elseif ($rule->operation === Nette\Forms\Form::PATTERN) {
95: $control->pattern = $rule->arg;
96: }
97: }
98: if ($control->type !== 'password') {
99: $control->value = $this->getValue() === '' ? $this->translate($this->emptyValue) : $this->value;
100: }
101: return $control;
102: }
103:
104: }
105: