Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

Classes

  • BaseControl
  • Button
  • Checkbox
  • CheckboxList
  • ChoiceControl
  • CsrfProtection
  • HiddenField
  • ImageButton
  • MultiChoiceControl
  • MultiSelectBox
  • RadioList
  • SelectBox
  • SubmitButton
  • TextArea
  • TextBase
  • TextInput
  • UploadControl
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Forms\Controls;
  9: 
 10: use Nette;
 11: use Nette\Forms\Form;
 12: use Nette\Utils\Strings;
 13: use Nette\Utils\Validators;
 14: 
 15: 
 16: /**
 17:  * Implements the basic functionality common to text input controls.
 18:  *
 19:  * @author     David Grudl
 20:  *
 21:  * @property   string $emptyValue
 22:  */
 23: abstract class TextBase extends BaseControl
 24: {
 25:     /** @var string */
 26:     protected $emptyValue = '';
 27: 
 28:     /** @var array */
 29:     protected $filters = array();
 30: 
 31:     /** @var mixed unfiltered submitted value */
 32:     protected $rawValue = '';
 33: 
 34: 
 35:     /**
 36:      * Sets control's value.
 37:      * @param  string
 38:      * @return self
 39:      */
 40:     public function setValue($value)
 41:     {
 42:         if ($value === NULL) {
 43:             $value = '';
 44:         } elseif (!is_scalar($value) && !method_exists($value, '__toString')) {
 45:             throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or NULL, %s given in field '%s'.", gettype($value), $this->name));
 46:         }
 47:         $this->rawValue = $this->value = $value;
 48:         return $this;
 49:     }
 50: 
 51: 
 52:     /**
 53:      * Returns control's value.
 54:      * @return string
 55:      */
 56:     public function getValue()
 57:     {
 58:         $value = $this->value;
 59:         if (!empty($this->control->maxlength)) {
 60:             $value = Strings::substring($value, 0, $this->control->maxlength);
 61:         }
 62:         foreach ($this->filters as $filter) {
 63:             $value = (string) call_user_func($filter, $value);
 64:         }
 65:         return $value === Strings::trim($this->translate($this->emptyValue)) ? '' : $value;
 66:     }
 67: 
 68: 
 69:     /**
 70:      * Sets the special value which is treated as empty string.
 71:      * @param  string
 72:      * @return self
 73:      */
 74:     public function setEmptyValue($value)
 75:     {
 76:         $this->emptyValue = (string) $value;
 77:         return $this;
 78:     }
 79: 
 80: 
 81:     /**
 82:      * Returns the special value which is treated as empty string.
 83:      * @return string
 84:      */
 85:     public function getEmptyValue()
 86:     {
 87:         return $this->emptyValue;
 88:     }
 89: 
 90: 
 91:     /**
 92:      * Sets the maximum number of allowed characters.
 93:      * @param  int
 94:      * @return self
 95:      */
 96:     public function setMaxLength($length)
 97:     {
 98:         $this->control->maxlength = $length;
 99:         return $this;
100:     }
101: 
102: 
103:     /**
104:      * Appends input string filter callback.
105:      * @param  callable
106:      * @return self
107:      * @deprecated
108:      */
109:     public function addFilter($filter)
110:     {
111:         $this->filters[] = Nette\Utils\Callback::check($filter);
112:         return $this;
113:     }
114: 
115: 
116:     public function getControl()
117:     {
118:         $el = parent::getControl();
119:         if ($this->emptyValue !== '') {
120:             $el->attrs['data-nette-empty-value'] = Strings::trim($this->translate($this->emptyValue));
121:         }
122:         if (isset($el->placeholder)) {
123:             $el->placeholder = $this->translate($el->placeholder);
124:         }
125:         return $el;
126:     }
127: 
128: 
129:     public function addRule($validator, $message = NULL, $arg = NULL)
130:     {
131:         if ($validator === Form::LENGTH || $validator === Form::MAX_LENGTH) {
132:             $tmp = is_array($arg) ? $arg[1] : $arg;
133:             if (is_scalar($tmp)) {
134:                 $this->control->maxlength = isset($this->control->maxlength) ? min($this->control->maxlength, $tmp) : $tmp;
135:             }
136:         }
137:         return parent::addRule($validator, $message, $arg);
138:     }
139: 
140: 
141:     /********************* validators ****************d*g**/
142: 
143: 
144:     /**
145:      * Is control's value valid email address?
146:      * @return bool
147:      * @internal
148:      */
149:     public static function validateEmail(TextBase $control)
150:     {
151:         return Validators::isEmail($control->getValue());
152:     }
153: 
154: 
155:     /**
156:      * Is control's value valid URL?
157:      * @return bool
158:      * @internal
159:      */
160:     public static function validateUrl(TextBase $control)
161:     {
162:         if (Validators::isUrl($value = $control->getValue())) {
163:             return TRUE;
164: 
165:         } elseif (Validators::isUrl($value = "http://$value")) {
166:             $control->setValue($value);
167:             return TRUE;
168:         }
169:         return FALSE;
170:     }
171: 
172: 
173:     /** @deprecated */
174:     public static function validateRegexp(TextBase $control, $regexp)
175:     {
176:         trigger_error('Validator REGEXP is deprecated; use PATTERN instead (which is matched against the entire value and is case sensitive).', E_USER_DEPRECATED);
177:         return (bool) Strings::match($control->getValue(), $regexp);
178:     }
179: 
180: 
181:     /**
182:      * Matches control's value regular expression?
183:      * @return bool
184:      * @internal
185:      */
186:     public static function validatePattern(TextBase $control, $pattern)
187:     {
188:         return (bool) Strings::match($control->getValue(), "\x01^($pattern)\\z\x01u");
189:     }
190: 
191: 
192:     /**
193:      * Is a control's value decimal number?
194:      * @return bool
195:      * @internal
196:      */
197:     public static function validateInteger(TextBase $control)
198:     {
199:         if (Validators::isNumericInt($value = $control->getValue())) {
200:             if (!is_float($tmp = $value * 1)) { // bigint leave as string
201:                 $control->setValue($tmp);
202:             }
203:             return TRUE;
204:         }
205:         return FALSE;
206:     }
207: 
208: 
209:     /**
210:      * Is a control's value float number?
211:      * @return bool
212:      * @internal
213:      */
214:     public static function validateFloat(TextBase $control)
215:     {
216:         $value = self::filterFloat($control->getValue());
217:         if (Validators::isNumeric($value)) {
218:             $control->setValue((float) $value);
219:             return TRUE;
220:         }
221:         return FALSE;
222:     }
223: 
224: 
225:     /**
226:      * Float string cleanup.
227:      * @param  string
228:      * @return string
229:      * @internal
230:      */
231:     public static function filterFloat($s)
232:     {
233:         return str_replace(array(' ', ','), array('', '.'), $s);
234:     }
235: 
236: }
237: 
Nette 2.2 API documentation generated by ApiGen 2.8.0