Namespaces

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • Button
  • Checkbox
  • ConventionalRenderer
  • FileUpload
  • Form
  • FormContainer
  • FormControl
  • FormGroup
  • HiddenField
  • ImageButton
  • InstantClientScript
  • MultiSelectBox
  • RadioList
  • Rule
  • Rules
  • SelectBox
  • SubmitButton
  • TextArea
  • TextBase
  • TextInput

Interfaces

  • IFormControl
  • IFormRenderer
  • INamingContainer
  • ISubmitterControl
  • Overview
  • Namespace
  • Class
  • Tree
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://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;
 13: 
 14: use Nette;
 15: 
 16: 
 17: 
 18: /**
 19:  * Implements the basic functionality common to text input controls.
 20:  *
 21:  * @author     David Grudl
 22:  *
 23:  * @property   string $emptyValue
 24:  */
 25: abstract class TextBase extends FormControl
 26: {
 27:     /** @var string */
 28:     protected $emptyValue = '';
 29: 
 30:     /** @var array */
 31:     protected $filters = array();
 32: 
 33: 
 34: 
 35:     /**
 36:      * Sets control's value.
 37:      * @param  string
 38:      * @return TextBase  provides a fluent interface
 39:      */
 40:     public function setValue($value)
 41:     {
 42:         $this->value = is_scalar($value) ? (string) $value : '';
 43:         return $this;
 44:     }
 45: 
 46: 
 47: 
 48:     /**
 49:      * Returns control's value.
 50:      * @return string
 51:      */
 52:     public function getValue()
 53:     {
 54:         $value = $this->value;
 55:         foreach ($this->filters as $filter) {
 56:             $value = (string) $filter($value);
 57:         }
 58:         return $value === $this->translate($this->emptyValue) ? '' : $value;
 59:     }
 60: 
 61: 
 62: 
 63:     /**
 64:      * Sets the special value which is treated as empty string.
 65:      * @param  string
 66:      * @return TextBase  provides a fluent interface
 67:      */
 68:     public function setEmptyValue($value)
 69:     {
 70:         $this->emptyValue = (string) $value;
 71:         return $this;
 72:     }
 73: 
 74: 
 75: 
 76:     /**
 77:      * Returns the special value which is treated as empty string.
 78:      * @return string
 79:      */
 80:     final public function getEmptyValue()
 81:     {
 82:         return $this->emptyValue;
 83:     }
 84: 
 85: 
 86: 
 87:     /**
 88:      * Appends input string filter callback.
 89:      * @param  callback
 90:      * @return TextBase  provides a fluent interface
 91:      */
 92:     public function addFilter($filter)
 93:     {
 94:         $this->filters[] = callback($filter);
 95:         return $this;
 96:     }
 97: 
 98: 
 99: 
100:     public function notifyRule(Rule $rule)
101:     {
102:         if (is_string($rule->operation) && strcasecmp($rule->operation, ':float') === 0) {
103:             $this->addFilter(array(__CLASS__, 'filterFloat'));
104:         }
105: 
106:         parent::notifyRule($rule);
107:     }
108: 
109: 
110: 
111:     /**
112:      * Min-length validator: has control's value minimal length?
113:      * @param  TextBase
114:      * @param  int  length
115:      * @return bool
116:      */
117:     public static function validateMinLength(TextBase $control, $length)
118:     {
119:         return iconv_strlen($control->getValue(), 'UTF-8') >= $length;
120:     }
121: 
122: 
123: 
124:     /**
125:      * Max-length validator: is control's value length in limit?
126:      * @param  TextBase
127:      * @param  int  length
128:      * @return bool
129:      */
130:     public static function validateMaxLength(TextBase $control, $length)
131:     {
132:         return iconv_strlen($control->getValue(), 'UTF-8') <= $length;
133:     }
134: 
135: 
136: 
137:     /**
138:      * Length validator: is control's value length in range?
139:      * @param  TextBase
140:      * @param  array  min and max length pair
141:      * @return bool
142:      */
143:     public static function validateLength(TextBase $control, $range)
144:     {
145:         if (!is_array($range)) {
146:             $range = array($range, $range);
147:         }
148:         $len = iconv_strlen($control->getValue(), 'UTF-8');
149:         return ($range[0] === NULL || $len >= $range[0]) && ($range[1] === NULL || $len <= $range[1]);
150:     }
151: 
152: 
153: 
154:     /**
155:      * Email validator: is control's value valid email address?
156:      * @param  TextBase
157:      * @return bool
158:      */
159:     public static function validateEmail(TextBase $control)
160:     {
161:         $atom = "[-a-z0-9!#$%&'*+/=?^_`{|}~]"; // RFC 5322 unquoted characters in local-part
162:         $localPart = "(\"([ !\\x23-\\x5B\\x5D-\\x7E]*|\\\\[ -~])+\"|$atom+(\\.$atom+)*)"; // quoted or unquoted
163:         $chars = "a-z0-9\x80-\xFF"; // superset of IDN
164:         $domain = "[$chars]([-$chars]{0,61}[$chars])"; // RFC 1034 one domain component
165:         return (bool) preg_match("(^$localPart@($domain?\\.)+[-$chars]{2,19}\\z)i", $control->getValue());
166:     }
167: 
168: 
169: 
170:     /**
171:      * URL validator: is control's value valid URL?
172:      * @param  TextBase
173:      * @return bool
174:      */
175:     public static function validateUrl(TextBase $control)
176:     {
177:         return (bool) preg_match('/^.+\.[a-z]{2,6}(\\/.*)?$/i', $control->getValue());
178:     }
179: 
180: 
181: 
182:     /**
183:      * Regular expression validator: matches control's value regular expression?
184:      * @param  TextBase
185:      * @param  string
186:      * @return bool
187:      */
188:     public static function validateRegexp(TextBase $control, $regexp)
189:     {
190:         return (bool) preg_match($regexp, $control->getValue());
191:     }
192: 
193: 
194: 
195:     /**
196:      * Integer validator: is a control's value decimal number?
197:      * @param  TextBase
198:      * @return bool
199:      */
200:     public static function validateInteger(TextBase $control)
201:     {
202:         return (bool) preg_match('/^-?[0-9]+$/', $control->getValue());
203:     }
204: 
205: 
206: 
207:     /**
208:      * Float validator: is a control's value float number?
209:      * @param  TextBase
210:      * @return bool
211:      */
212:     public static function validateFloat(TextBase $control)
213:     {
214:         return (bool) preg_match('/^-?[0-9]*[.,]?[0-9]+$/', $control->getValue());
215:     }
216: 
217: 
218: 
219:     /**
220:      * Rangle validator: is a control's value number in specified range?
221:      * @param  TextBase
222:      * @param  array  min and max value pair
223:      * @return bool
224:      */
225:     public static function validateRange(TextBase $control, $range)
226:     {
227:         return ($range[0] === NULL || $control->getValue() >= $range[0]) && ($range[1] === NULL || $control->getValue() <= $range[1]);
228:     }
229: 
230: 
231: 
232:     /**
233:      * Float string cleanup.
234:      * @param  string
235:      * @return string
236:      */
237:     public static function filterFloat($s)
238:     {
239:         return str_replace(array(' ', ','), array('', '.'), $s);
240:     }
241: 
242: }
243: 
Nette Framework 0.9.7 API documentation generated by ApiGen 2.3.0