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
  • PHP

Classes

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