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

Classes

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