1: <?php
  2: 
  3:   4:   5:   6:   7:   8:   9:  10: 
 11: 
 12: namespace Nette\Forms;
 13: 
 14: use Nette;
 15: 
 16: 
 17: 
 18:  19:  20:  21:  22: 
 23: class TextInput extends TextBase
 24: {
 25: 
 26:      27:  28:  29:  30:  31: 
 32:     public function __construct($label = NULL, $cols = NULL, $maxLength = NULL)
 33:     {
 34:         parent::__construct($label);
 35:         $this->control->type = 'text';
 36:         $this->control->size = $cols;
 37:         $this->control->maxlength = $maxLength;
 38:         $this->filters[] = callback($this, 'sanitize');
 39:         $this->value = '';
 40:     }
 41: 
 42: 
 43: 
 44:      45:  46:  47: 
 48:     public function sanitize($value)
 49:     {
 50:         if ($this->control->maxlength && iconv_strlen($value, 'UTF-8') > $this->control->maxlength) {
 51:             $value = iconv_substr($value, 0, $this->control->maxlength, 'UTF-8');
 52:         }
 53:         return Nette\String::trim(strtr($value, "\r\n", '  '));
 54:     }
 55: 
 56: 
 57: 
 58:      59:  60:  61:  62: 
 63:     public function setPasswordMode($mode = TRUE)
 64:     {
 65:         $this->control->type = $mode ? 'password' : 'text';
 66:         return $this;
 67:     }
 68: 
 69: 
 70: 
 71:      72:  73:  74: 
 75:     public function getControl()
 76:     {
 77:         $control = parent::getControl();
 78:         if ($this->control->type !== 'password') {
 79:             $control->value = $this->getValue() === '' ? $this->translate($this->emptyValue) : $this->value;
 80:         }
 81:         return $control;
 82:     }
 83: 
 84: 
 85: 
 86:     public function notifyRule(Rule $rule)
 87:     {
 88:         if (is_string($rule->operation) && strcasecmp($rule->operation, ':length') === 0 && !$rule->isNegative) {
 89:             $this->control->maxlength = is_array($rule->arg) ? $rule->arg[1] : $rule->arg;
 90: 
 91:         } elseif (is_string($rule->operation) && strcasecmp($rule->operation, ':maxLength') === 0 && !$rule->isNegative) {
 92:             $this->control->maxlength = $rule->arg;
 93:         }
 94: 
 95:         parent::notifyRule($rule);
 96:     }
 97: 
 98: 
 99: }
100: