1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Forms\Controls;
9:
10: use Nette,
11: Nette\Forms\Form,
12: Nette\Utils\Strings;
13:
14:
15: 16: 17: 18: 19: 20: 21:
22: abstract class TextBase extends BaseControl
23: {
24:
25: protected $emptyValue = '';
26:
27:
28: protected $rawValue = '';
29:
30:
31: 32: 33: 34: 35:
36: public function setValue($value)
37: {
38: if ($value === NULL) {
39: $value = '';
40: } elseif (!is_scalar($value) && !method_exists($value, '__toString')) {
41: throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or NULL, %s given in field '%s'.", gettype($value), $this->name));
42: }
43: $this->rawValue = $this->value = $value;
44: return $this;
45: }
46:
47:
48: 49: 50: 51:
52: public function getValue()
53: {
54: return $this->value === Strings::trim($this->translate($this->emptyValue)) ? '' : $this->value;
55: }
56:
57:
58: 59: 60: 61: 62:
63: public function setEmptyValue($value)
64: {
65: $this->emptyValue = (string) $value;
66: return $this;
67: }
68:
69:
70: 71: 72: 73:
74: public function getEmptyValue()
75: {
76: return $this->emptyValue;
77: }
78:
79:
80: 81: 82: 83: 84:
85: public function setMaxLength($length)
86: {
87: $this->control->maxlength = $length;
88: return $this;
89: }
90:
91:
92: 93: 94: 95: 96:
97: public function addFilter($filter)
98: {
99: $this->rules->addFilter($filter);
100: return $this;
101: }
102:
103:
104: public function getControl()
105: {
106: $el = parent::getControl();
107: if ($this->emptyValue !== '') {
108: $el->attrs['data-nette-empty-value'] = Strings::trim($this->translate($this->emptyValue));
109: }
110: if (isset($el->placeholder)) {
111: $el->placeholder = $this->translate($el->placeholder);
112: }
113: return $el;
114: }
115:
116:
117: public function addRule($validator, $message = NULL, $arg = NULL)
118: {
119: if ($validator === Form::LENGTH || $validator === Form::MAX_LENGTH) {
120: $tmp = is_array($arg) ? $arg[1] : $arg;
121: if (is_scalar($tmp)) {
122: $this->control->maxlength = isset($this->control->maxlength) ? min($this->control->maxlength, $tmp) : $tmp;
123: }
124: }
125: return parent::addRule($validator, $message, $arg);
126: }
127:
128: }
129: