Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
    • Templating
    • Utils
  • 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, 2011 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: 
 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:     /**
 38:      * Sets control's value.
 39:      * @param  string
 40:      * @return TextBase  provides a fluent interface
 41:      */
 42:     public function setValue($value)
 43:     {
 44:         $this->value = is_scalar($value) ? (string) $value : '';
 45:         return $this;
 46:     }
 47: 
 48: 
 49: 
 50:     /**
 51:      * Returns control's value.
 52:      * @return string
 53:      */
 54:     public function getValue()
 55:     {
 56:         $value = $this->value;
 57:         foreach ($this->filters as $filter) {
 58:             $value = (string) $filter($value);
 59:         }
 60:         return $value === $this->translate($this->emptyValue) ? '' : $value;
 61:     }
 62: 
 63: 
 64: 
 65:     /**
 66:      * Sets the special value which is treated as empty string.
 67:      * @param  string
 68:      * @return TextBase  provides a fluent interface
 69:      */
 70:     public function setEmptyValue($value)
 71:     {
 72:         $this->emptyValue = (string) $value;
 73:         return $this;
 74:     }
 75: 
 76: 
 77: 
 78:     /**
 79:      * Returns the special value which is treated as empty string.
 80:      * @return string
 81:      */
 82:     final public function getEmptyValue()
 83:     {
 84:         return $this->emptyValue;
 85:     }
 86: 
 87: 
 88: 
 89:     /**
 90:      * Appends input string filter callback.
 91:      * @param  callback
 92:      * @return TextBase  provides a fluent interface
 93:      */
 94:     public function addFilter($filter)
 95:     {
 96:         $this->filters[] = callback($filter);
 97:         return $this;
 98:     }
 99: 
100: 
101: 
102:     public function getControl()
103:     {
104:         $control = parent::getControl();
105:         foreach ($this->getRules() as $rule) {
106:             if ($rule->type === Nette\Forms\Rule::VALIDATOR && !$rule->isNegative
107:                 && ($rule->operation === Form::LENGTH || $rule->operation === Form::MAX_LENGTH)
108:             ) {
109:                 $control->maxlength = is_array($rule->arg) ? $rule->arg[1] : $rule->arg;
110:             }
111:         }
112:         if ($this->emptyValue !== '') {
113:             $control->data('nette-empty-value', $this->translate($this->emptyValue));
114:         }
115:         return $control;
116:     }
117: 
118: 
119: 
120:     public function addRule($operation, $message = NULL, $arg = NULL)
121:     {
122:         if ($operation === Form::FLOAT) {
123:             $this->addFilter(callback(__CLASS__, 'filterFloat'));
124:         }
125:         return parent::addRule($operation, $message, $arg);
126:     }
127: 
128: 
129: 
130:     /**
131:      * Min-length validator: has control's value minimal length?
132:      * @param  TextBase
133:      * @param  int  length
134:      * @return bool
135:      */
136:     public static function validateMinLength(TextBase $control, $length)
137:     {
138:         return Strings::length($control->getValue()) >= $length;
139:     }
140: 
141: 
142: 
143:     /**
144:      * Max-length validator: is control's value length in limit?
145:      * @param  TextBase
146:      * @param  int  length
147:      * @return bool
148:      */
149:     public static function validateMaxLength(TextBase $control, $length)
150:     {
151:         return Strings::length($control->getValue()) <= $length;
152:     }
153: 
154: 
155: 
156:     /**
157:      * Length validator: is control's value length in range?
158:      * @param  TextBase
159:      * @param  array  min and max length pair
160:      * @return bool
161:      */
162:     public static function validateLength(TextBase $control, $range)
163:     {
164:         if (!is_array($range)) {
165:             $range = array($range, $range);
166:         }
167:         $len = Strings::length($control->getValue());
168:         return ($range[0] === NULL || $len >= $range[0]) && ($range[1] === NULL || $len <= $range[1]);
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:         $atom = "[-a-z0-9!#$%&'*+/=?^_`{|}~]"; // RFC 5322 unquoted characters in local-part
181:         $localPart = "(?:\"(?:[ !\\x23-\\x5B\\x5D-\\x7E]*|\\\\[ -~])+\"|$atom+(?:\\.$atom+)*)"; // quoted or unquoted
182:         $chars = "a-z0-9\x80-\xFF"; // superset of IDN
183:         $domain = "[$chars](?:[-$chars]{0,61}[$chars])"; // RFC 1034 one domain component
184:         return (bool) Strings::match($control->getValue(), "(^$localPart@(?:$domain?\\.)+[-$chars]{2,19}\\z)i");
185:     }
186: 
187: 
188: 
189:     /**
190:      * URL validator: is control's value valid URL?
191:      * @param  TextBase
192:      * @return bool
193:      */
194:     public static function validateUrl(TextBase $control)
195:     {
196:         $chars = "a-z0-9\x80-\xFF";
197:         return (bool) Strings::match(
198:             $control->getValue(),
199:             "#^(?:https?://|)(?:[$chars](?:[-$chars]{0,61}[$chars])?\\.)+[-$chars]{2,19}(/\S*)?$#i"
200:         );
201:     }
202: 
203: 
204: 
205:     /** @deprecated */
206:     public static function validateRegexp(TextBase $control, $regexp)
207:     {
208:         return (bool) Strings::match($control->getValue(), $regexp);
209:     }
210: 
211: 
212: 
213:     /**
214:      * Regular expression validator: matches control's value regular expression?
215:      * @param  TextBase
216:      * @param  string
217:      * @return bool
218:      */
219:     public static function validatePattern(TextBase $control, $pattern)
220:     {
221:         return (bool) Strings::match($control->getValue(), "\x01^($pattern)$\x01u");
222:     }
223: 
224: 
225: 
226:     /**
227:      * Integer validator: is a control's value decimal number?
228:      * @param  TextBase
229:      * @return bool
230:      */
231:     public static function validateInteger(TextBase $control)
232:     {
233:         return (bool) Strings::match($control->getValue(), '/^-?[0-9]+$/');
234:     }
235: 
236: 
237: 
238:     /**
239:      * Float validator: is a control's value float number?
240:      * @param  TextBase
241:      * @return bool
242:      */
243:     public static function validateFloat(TextBase $control)
244:     {
245:         return (bool) Strings::match($control->getValue(), '/^-?[0-9]*[.,]?[0-9]+$/');
246:     }
247: 
248: 
249: 
250:     /**
251:      * Rangle validator: is a control's value number in specified range?
252:      * @param  TextBase
253:      * @param  array  min and max value pair
254:      * @return bool
255:      */
256:     public static function validateRange(TextBase $control, $range)
257:     {
258:         return ($range[0] === NULL || $control->getValue() >= $range[0])
259:             && ($range[1] === NULL || $control->getValue() <= $range[1]);
260:     }
261: 
262: 
263: 
264:     /**
265:      * Float string cleanup.
266:      * @param  string
267:      * @return string
268:      */
269:     public static function filterFloat($s)
270:     {
271:         return str_replace(array(' ', ','), array('', '.'), $s);
272:     }
273: 
274: }
275: 
Nette Framework 2.0beta1 API API documentation generated by ApiGen 2.3.0