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: * @package Nette\Forms\Controls
11: */
12:
13:
14:
15: /**
16: * Single line text input control.
17: *
18: * @author David Grudl
19: * @property-write $type
20: * @package Nette\Forms\Controls
21: */
22: class NTextInput extends NTextBase
23: {
24:
25: /**
26: * @param string control name
27: * @param string label
28: * @param int width of the control
29: * @param int maximum number of characters the user may enter
30: */
31: public function __construct($label = NULL, $cols = NULL, $maxLength = NULL)
32: {
33: parent::__construct($label);
34: $this->control->type = 'text';
35: $this->control->size = $cols;
36: $this->control->maxlength = $maxLength;
37: $this->addFilter($this->sanitize);
38: $this->value = '';
39: }
40:
41:
42:
43: /**
44: * Filter: removes unnecessary whitespace and shortens value to control's max length.
45: * @return string
46: */
47: public function sanitize($value)
48: {
49: if ($this->control->maxlength && NStrings::length($value) > $this->control->maxlength) {
50: $value = NStrings::substring($value, 0, $this->control->maxlength);
51: }
52: return NStrings::trim(strtr($value, "\r\n", ' '));
53: }
54:
55:
56:
57: /**
58: * Changes control's type attribute.
59: * @param string
60: * @return NFormControl provides a fluent interface
61: */
62: public function setType($type)
63: {
64: $this->control->type = $type;
65: return $this;
66: }
67:
68:
69:
70: /** @deprecated */
71: public function setPasswordMode($mode = TRUE)
72: {
73: $this->control->type = $mode ? 'password' : 'text';
74: return $this;
75: }
76:
77:
78:
79: /**
80: * Generates control's HTML element.
81: * @return NHtml
82: */
83: public function getControl()
84: {
85: $control = parent::getControl();
86: foreach ($this->getRules() as $rule) {
87: if ($rule->isNegative || $rule->type !== NRule::VALIDATOR) {
88:
89: } elseif ($rule->operation === NForm::RANGE && $control->type !== 'text') {
90: list($control->min, $control->max) = $rule->arg;
91:
92: } elseif ($rule->operation === NForm::PATTERN) {
93: $control->pattern = $rule->arg;
94: }
95: }
96: if ($control->type !== 'password') {
97: $control->value = $this->getValue() === '' ? $this->translate($this->emptyValue) : $this->value;
98: }
99: return $control;
100: }
101:
102: }
103: