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