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

  • Button
  • Checkbox
  • FormControl
  • HiddenField
  • ImageButton
  • MultiSelectBox
  • RadioList
  • SelectBox
  • SubmitButton
  • TextArea
  • TextBase
  • TextInput
  • UploadControl
  • 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 TextBase extends FormControl
 24: {
 25:     /** @var string */
 26:     protected $emptyValue = '';
 27: 
 28:     /** @var array */
 29:     protected $filters = array();
 30: 
 31: 
 32: 
 33:     /**
 34:      * Sets control's value.
 35:      * @param  string
 36:      * @return TextBase  provides a fluent interface
 37:      */
 38:     public function setValue($value)
 39:     {
 40:         $this->value = is_array($value) ? '' : (string) $value;
 41:         return $this;
 42:     }
 43: 
 44: 
 45: 
 46:     /**
 47:      * Returns control's value.
 48:      * @return string
 49:      */
 50:     public function getValue()
 51:     {
 52:         $value = $this->value;
 53:         foreach ($this->filters as $filter) {
 54:             $value = (string) $filter->invoke($value);
 55:         }
 56:         return $value === $this->translate($this->emptyValue) ? '' : $value;
 57:     }
 58: 
 59: 
 60: 
 61:     /**
 62:      * Sets the special value which is treated as empty string.
 63:      * @param  string
 64:      * @return TextBase  provides a fluent interface
 65:      */
 66:     public function setEmptyValue($value)
 67:     {
 68:         $this->emptyValue = (string) $value;
 69:         return $this;
 70:     }
 71: 
 72: 
 73: 
 74:     /**
 75:      * Returns the special value which is treated as empty string.
 76:      * @return string
 77:      */
 78:     final public function getEmptyValue()
 79:     {
 80:         return $this->emptyValue;
 81:     }
 82: 
 83: 
 84: 
 85:     /**
 86:      * Appends input string filter callback.
 87:      * @param  callable
 88:      * @return TextBase  provides a fluent interface
 89:      */
 90:     public function addFilter($filter)
 91:     {
 92:         $this->filters[] = new Callback($filter);
 93:         return $this;
 94:     }
 95: 
 96: 
 97: 
 98:     public function getControl()
 99:     {
100:         $control = parent::getControl();
101:         foreach ($this->getRules() as $rule) {
102:             if ($rule->type === Rule::VALIDATOR && !$rule->isNegative
103:                 && ($rule->operation === Form::LENGTH || $rule->operation === Form::MAX_LENGTH)
104:             ) {
105:                 $control->maxlength = is_array($rule->arg) ? $rule->arg[1] : $rule->arg;
106:             }
107:         }
108:         if ($this->emptyValue !== '') {
109:             $control->data('nette-empty-value', $this->translate($this->emptyValue));
110:         }
111:         return $control;
112:     }
113: 
114: 
115: 
116:     public function addRule($operation, $message = NULL, $arg = NULL)
117:     {
118:         if ($operation === Form::FLOAT) {
119:             $this->addFilter(array(__CLASS__, 'filterFloat'));
120:         }
121:         return parent::addRule($operation, $message, $arg);
122:     }
123: 
124: 
125: 
126:     /**
127:      * Min-length validator: has control's value minimal length?
128:      * @param  TextBase
129:      * @param  int  length
130:      * @return bool
131:      */
132:     public static function validateMinLength(TextBase $control, $length)
133:     {
134:         return Strings::length($control->getValue()) >= $length;
135:     }
136: 
137: 
138: 
139:     /**
140:      * Max-length validator: is control's value length in limit?
141:      * @param  TextBase
142:      * @param  int  length
143:      * @return bool
144:      */
145:     public static function validateMaxLength(TextBase $control, $length)
146:     {
147:         return Strings::length($control->getValue()) <= $length;
148:     }
149: 
150: 
151: 
152:     /**
153:      * Length validator: is control's value length in range?
154:      * @param  TextBase
155:      * @param  array  min and max length pair
156:      * @return bool
157:      */
158:     public static function validateLength(TextBase $control, $range)
159:     {
160:         if (!is_array($range)) {
161:             $range = array($range, $range);
162:         }
163:         return Validators::isInRange(Strings::length($control->getValue()), $range);
164:     }
165: 
166: 
167: 
168:     /**
169:      * Email validator: is control's value valid email address?
170:      * @param  TextBase
171:      * @return bool
172:      */
173:     public static function validateEmail(TextBase $control)
174:     {
175:         return Validators::isEmail($control->getValue());
176:     }
177: 
178: 
179: 
180:     /**
181:      * URL validator: is control's value valid URL?
182:      * @param  TextBase
183:      * @return bool
184:      */
185:     public static function validateUrl(TextBase $control)
186:     {
187:         return Validators::isUrl($control->getValue()) || Validators::isUrl('http://' . $control->getValue());
188:     }
189: 
190: 
191: 
192:     /** @deprecated */
193:     public static function validateRegexp(TextBase $control, $regexp)
194:     {
195:         return (bool) Strings::match($control->getValue(), $regexp);
196:     }
197: 
198: 
199: 
200:     /**
201:      * Regular expression validator: matches control's value regular expression?
202:      * @param  TextBase
203:      * @param  string
204:      * @return bool
205:      */
206:     public static function validatePattern(TextBase $control, $pattern)
207:     {
208:         return (bool) Strings::match($control->getValue(), "\x01^($pattern)\\z\x01u");
209:     }
210: 
211: 
212: 
213:     /**
214:      * Integer validator: is a control's value decimal number?
215:      * @param  TextBase
216:      * @return bool
217:      */
218:     public static function validateInteger(TextBase $control)
219:     {
220:         return Validators::isNumericInt($control->getValue());
221:     }
222: 
223: 
224: 
225:     /**
226:      * Float validator: is a control's value float number?
227:      * @param  TextBase
228:      * @return bool
229:      */
230:     public static function validateFloat(TextBase $control)
231:     {
232:         return Validators::isNumeric(self::filterFloat($control->getValue()));
233:     }
234: 
235: 
236: 
237:     /**
238:      * Rangle validator: is a control's value number in specified range?
239:      * @param  TextBase
240:      * @param  array  min and max value pair
241:      * @return bool
242:      */
243:     public static function validateRange(TextBase $control, $range)
244:     {
245:         return Validators::isInRange($control->getValue(), $range);
246:     }
247: 
248: 
249: 
250:     /**
251:      * Float string cleanup.
252:      * @param  string
253:      * @return string
254:      */
255:     public static function filterFloat($s)
256:     {
257:         return str_replace(array(' ', ','), array('', '.'), $s);
258:     }
259: 
260: }
261: 
Nette Framework 2.0.8 (for PHP 5.2, un-prefixed) API API documentation generated by ApiGen 2.8.0