Packages

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • none

Classes

  • Overview
  • Package
  • 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 (http://davidgrudl.com)
  6:  * @package Nette\Forms\Controls
  7:  */
  8: 
  9: 
 10: 
 11: /**
 12:  * Implements the basic functionality common to text input controls.
 13:  *
 14:  * @author     David Grudl
 15:  *
 16:  * @property   string $emptyValue
 17:  * @package Nette\Forms\Controls
 18:  */
 19: abstract class NTextBase extends NFormControl
 20: {
 21:     /** @var string */
 22:     protected $emptyValue = '';
 23: 
 24:     /** @var array */
 25:     protected $filters = array();
 26: 
 27: 
 28:     /**
 29:      * @param  string  label
 30:      */
 31:     public function __construct($label = NULL)
 32:     {
 33:         parent::__construct($label);
 34:         $this->addFilter($this->sanitize);
 35:     }
 36: 
 37: 
 38:     /**
 39:      * Sets control's value.
 40:      * @param  string
 41:      * @return self
 42:      */
 43:     public function setValue($value)
 44:     {
 45:         $this->value = is_array($value) ? '' : str_replace("\r\n", "\n", $value);
 46:         return $this;
 47:     }
 48: 
 49: 
 50:     /**
 51:      * Returns control's value.
 52:      * @return string
 53:      */
 54:     public function getValue()
 55:     {
 56:         $value = $this->value;
 57:         foreach ($this->filters as $filter) {
 58:             $value = (string) $filter->invoke($value);
 59:         }
 60:         return $value === $this->translate($this->emptyValue) ? '' : $value;
 61:     }
 62: 
 63: 
 64:     /**
 65:      * Sets the special value which is treated as empty string.
 66:      * @param  string
 67:      * @return self
 68:      */
 69:     public function setEmptyValue($value)
 70:     {
 71:         $this->emptyValue = (string) $value;
 72:         return $this;
 73:     }
 74: 
 75: 
 76:     /**
 77:      * Returns the special value which is treated as empty string.
 78:      * @return string
 79:      */
 80:     public function getEmptyValue()
 81:     {
 82:         return $this->emptyValue;
 83:     }
 84: 
 85: 
 86:     /**
 87:      * Appends input string filter callback.
 88:      * @param  callable
 89:      * @return self
 90:      */
 91:     public function addFilter($filter)
 92:     {
 93:         $this->filters[] = new NCallback($filter);
 94:         return $this;
 95:     }
 96: 
 97: 
 98:     /**
 99:      * Filter: removes unnecessary whitespace and shortens value to control's max length.
100:      * @return string
101:      */
102:     public function sanitize($value)
103:     {
104:         if ($this->control->maxlength) {
105:             $value = NStrings::substring($value, 0, $this->control->maxlength);
106:         }
107:         if (strcasecmp($this->control->getName(), 'input') === 0) {
108:             $value = NStrings::trim(strtr($value, "\r\n", '  '));
109:         }
110:         return $value;
111:     }
112: 
113: 
114:     public function getControl()
115:     {
116:         $control = parent::getControl();
117:         foreach ($this->getRules() as $rule) {
118:             if ($rule->type === NRule::VALIDATOR && !$rule->isNegative
119:                 && ($rule->operation === NForm::LENGTH || $rule->operation === NForm::MAX_LENGTH)
120:             ) {
121:                 $control->maxlength = is_array($rule->arg) ? $rule->arg[1] : $rule->arg;
122:             }
123:         }
124:         if ($this->emptyValue !== '') {
125:             $control->data('nette-empty-value', $this->translate($this->emptyValue));
126:         }
127:         return $control;
128:     }
129: 
130: 
131:     public function addRule($operation, $message = NULL, $arg = NULL)
132:     {
133:         if ($operation === NForm::FLOAT) {
134:             $this->addFilter(array(__CLASS__, 'filterFloat'));
135: 
136:         } elseif ($operation === NForm::LENGTH || $operation === NForm::MAX_LENGTH) {
137:             $tmp = is_array($arg) ? $arg[1] : $arg;
138:             $this->control->maxlength = is_scalar($tmp) ? $tmp : NULL;
139:         }
140:         return parent::addRule($operation, $message, $arg);
141:     }
142: 
143: 
144:     /********************* validators ****************d*g**/
145: 
146: 
147:     /**
148:      * Min-length validator: has control's value minimal length?
149:      * @param  NTextBase
150:      * @param  int  length
151:      * @return bool
152:      * @internal
153:      */
154:     public static function validateMinLength(NTextBase $control, $length)
155:     {
156:         return NStrings::length($control->getValue()) >= $length;
157:     }
158: 
159: 
160:     /**
161:      * Max-length validator: is control's value length in limit?
162:      * @param  NTextBase
163:      * @param  int  length
164:      * @return bool
165:      * @internal
166:      */
167:     public static function validateMaxLength(NTextBase $control, $length)
168:     {
169:         return NStrings::length($control->getValue()) <= $length;
170:     }
171: 
172: 
173:     /**
174:      * Length validator: is control's value length in range?
175:      * @param  NTextBase
176:      * @param  array  min and max length pair
177:      * @return bool
178:      */
179:     public static function validateLength(NTextBase $control, $range)
180:     {
181:         if (!is_array($range)) {
182:             $range = array($range, $range);
183:         }
184:         return NValidators::isInRange(NStrings::length($control->getValue()), $range);
185:     }
186: 
187: 
188:     /**
189:      * Email validator: is control's value valid email address?
190:      * @param  NTextBase
191:      * @return bool
192:      */
193:     public static function validateEmail(NTextBase $control)
194:     {
195:         return NValidators::isEmail($control->getValue());
196:     }
197: 
198: 
199:     /**
200:      * URL validator: is control's value valid URL?
201:      * @param  NTextBase
202:      * @return bool
203:      */
204:     public static function validateUrl(NTextBase $control)
205:     {
206:         return NValidators::isUrl($control->getValue()) || NValidators::isUrl('http://' . $control->getValue());
207:     }
208: 
209: 
210:     /** @deprecated */
211:     public static function validateRegexp(NTextBase $control, $regexp)
212:     {
213:         return (bool) NStrings::match($control->getValue(), $regexp);
214:     }
215: 
216: 
217:     /**
218:      * Matches control's value regular expression?
219:      * @return bool
220:      * @internal
221:      */
222:     public static function validatePattern(NTextBase $control, $pattern)
223:     {
224:         return (bool) NStrings::match($control->getValue(), "\x01^($pattern)\\z\x01u");
225:     }
226: 
227: 
228:     /**
229:      * Is a control's value decimal number?
230:      * @return bool
231:      * @internal
232:      */
233:     public static function validateInteger(NTextBase $control)
234:     {
235:         return NValidators::isNumericInt($control->getValue());
236:     }
237: 
238: 
239:     /**
240:      * Is a control's value float number?
241:      * @return bool
242:      * @internal
243:      */
244:     public static function validateFloat(NTextBase $control)
245:     {
246:         return NValidators::isNumeric(self::filterFloat($control->getValue()));
247:     }
248: 
249: 
250:     /**
251:      * Rangle validator: is a control's value number in specified range?
252:      * @param  NTextBase
253:      * @param  array  min and max value pair
254:      * @return bool
255:      */
256:     public static function validateRange(NTextBase $control, $range)
257:     {
258:         return NValidators::isInRange($control->getValue(), $range);
259:     }
260: 
261: 
262:     /**
263:      * Float string cleanup.
264:      * @param  string
265:      * @return string
266:      * @internal
267:      */
268:     public static function filterFloat($s)
269:     {
270:         return str_replace(array(' ', ','), array('', '.'), $s);
271:     }
272: 
273: }
274: 
Nette Framework 2.0.18 (for PHP 5.2, prefixed) API documentation generated by ApiGen 2.8.0