Namespaces

  • 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

  • BaseControl
  • Button
  • Checkbox
  • HiddenField
  • ImageButton
  • 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 (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Forms\Controls;
  9: 
 10: use Nette,
 11:     Nette\Forms\Form,
 12:     Nette\Utils\Strings,
 13:     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: 
 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($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:     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 Nette\Callback($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 = Nette\Utils\Strings::substring($value, 0, $this->control->maxlength);
110:         }
111:         if (strcasecmp($this->control->getName(), 'input') === 0) {
112:             $value = Nette\Utils\Strings::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 === Nette\Forms\Rule::VALIDATOR && !$rule->isNegative
123:                 && ($rule->operation === Form::LENGTH || $rule->operation === Form::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 === Form::FLOAT) {
138:             $this->addFilter(array(__CLASS__, 'filterFloat'));
139: 
140:         } elseif ($operation === Form::LENGTH || $operation === Form::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:     /********************* validators ****************d*g**/
149: 
150: 
151:     /**
152:      * Min-length validator: has control's value minimal length?
153:      * @param  TextBase
154:      * @param  int  length
155:      * @return bool
156:      * @internal
157:      */
158:     public static function validateMinLength(TextBase $control, $length)
159:     {
160:         return Strings::length($control->getValue()) >= $length;
161:     }
162: 
163: 
164:     /**
165:      * Max-length validator: is control's value length in limit?
166:      * @param  TextBase
167:      * @param  int  length
168:      * @return bool
169:      * @internal
170:      */
171:     public static function validateMaxLength(TextBase $control, $length)
172:     {
173:         return Strings::length($control->getValue()) <= $length;
174:     }
175: 
176: 
177:     /**
178:      * Length validator: is control's value length in range?
179:      * @param  TextBase
180:      * @param  array  min and max length pair
181:      * @return bool
182:      */
183:     public static function validateLength(TextBase $control, $range)
184:     {
185:         if (!is_array($range)) {
186:             $range = array($range, $range);
187:         }
188:         return Validators::isInRange(Strings::length($control->getValue()), $range);
189:     }
190: 
191: 
192:     /**
193:      * Email validator: is control's value valid email address?
194:      * @param  TextBase
195:      * @return bool
196:      */
197:     public static function validateEmail(TextBase $control)
198:     {
199:         return Validators::isEmail($control->getValue());
200:     }
201: 
202: 
203:     /**
204:      * URL validator: is control's value valid URL?
205:      * @param  TextBase
206:      * @return bool
207:      */
208:     public static function validateUrl(TextBase $control)
209:     {
210:         return Validators::isUrl($control->getValue()) || Validators::isUrl('http://' . $control->getValue());
211:     }
212: 
213: 
214:     /** @deprecated */
215:     public static function validateRegexp(TextBase $control, $regexp)
216:     {
217:         return (bool) Strings::match($control->getValue(), $regexp);
218:     }
219: 
220: 
221:     /**
222:      * Matches control's value regular expression?
223:      * @return bool
224:      * @internal
225:      */
226:     public static function validatePattern(TextBase $control, $pattern)
227:     {
228:         return (bool) Strings::match($control->getValue(), "\x01^($pattern)\\z\x01u");
229:     }
230: 
231: 
232:     /**
233:      * Is a control's value decimal number?
234:      * @return bool
235:      * @internal
236:      */
237:     public static function validateInteger(TextBase $control)
238:     {
239:         return Validators::isNumericInt($control->getValue());
240:     }
241: 
242: 
243:     /**
244:      * Is a control's value float number?
245:      * @return bool
246:      * @internal
247:      */
248:     public static function validateFloat(TextBase $control)
249:     {
250:         return Validators::isNumeric(static::filterFloat($control->getValue()));
251:     }
252: 
253: 
254:     /**
255:      * Rangle validator: is a control's value number in specified range?
256:      * @param  TextBase
257:      * @param  array  min and max value pair
258:      * @return bool
259:      */
260:     public static function validateRange(TextBase $control, $range)
261:     {
262:         return Validators::isInRange($control->getValue(), $range);
263:     }
264: 
265: 
266:     /**
267:      * Float string cleanup.
268:      * @param  string
269:      * @return string
270:      * @internal
271:      */
272:     public static function filterFloat($s)
273:     {
274:         return str_replace(array(' ', ','), array('', '.'), $s);
275:     }
276: 
277: }
278: 
Nette 2.0 API documentation generated by ApiGen 2.8.0