Overview

Namespaces

  • Budovy
  • Kdyby
    • BootstrapFormRenderer
      • DI
      • Latte
  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Diagnostics
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
      • Diagnostics
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • PhpGenerator
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
  • NetteModule
  • Nextras
    • Datagrid
  • None
  • PHP
  • Tester
    • CodeCoverage
    • Runner
      • Output
  • Vodacek
    • Forms
      • Controls
  • WebLoader
    • Filter
    • Nette

Classes

  • BaseControl
  • Button
  • Checkbox
  • CheckboxList
  • ChoiceControl
  • CsrfProtection
  • HiddenField
  • ImageButton
  • MultiChoiceControl
  • 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:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Forms\Controls;
  9: 
 10: use Nette,
 11:     Nette\Forms\Form,
 12:     Nette\Utils\Strings,
 13:     Nette\Utils\Validators;
 14: 
 15: 
 16: /**
 17:  * Implements the basic functionality common to text input controls.
 18:  *
 19:  * @author     David Grudl
 20:  *
 21:  * @property   string $emptyValue
 22:  */
 23: abstract class TextBase extends BaseControl
 24: {
 25:     /** @var string */
 26:     protected $emptyValue = '';
 27: 
 28:     /** @var array */
 29:     protected $filters = array();
 30: 
 31:     /** @var mixed unfiltered submitted value */
 32:     protected $rawValue = '';
 33: 
 34: 
 35:     /**
 36:      * Sets control's value.
 37:      * @param  string
 38:      * @return self
 39:      */
 40:     public function setValue($value)
 41:     {
 42:         if ($value === NULL) {
 43:             $value = '';
 44:         } elseif (!is_scalar($value) && !method_exists($value, '__toString')) {
 45:             throw new Nette\InvalidArgumentException('Value must be scalar or NULL, ' . gettype($value) . " given in field '{$this->name}'.");
 46:         }
 47:         $this->rawValue = $this->value = $value;
 48:         return $this;
 49:     }
 50: 
 51: 
 52:     /**
 53:      * Returns control's value.
 54:      * @return string
 55:      */
 56:     public function getValue()
 57:     {
 58:         $value = $this->value;
 59:         if (!empty($this->control->maxlength)) {
 60:             $value = Nette\Utils\Strings::substring($value, 0, $this->control->maxlength);
 61:         }
 62:         foreach ($this->filters as $filter) {
 63:             $value = (string) call_user_func($filter, $value);
 64:         }
 65:         return $value === $this->translate($this->emptyValue) ? '' : $value;
 66:     }
 67: 
 68: 
 69:     /**
 70:      * Sets the special value which is treated as empty string.
 71:      * @param  string
 72:      * @return self
 73:      */
 74:     public function setEmptyValue($value)
 75:     {
 76:         $this->emptyValue = (string) $value;
 77:         return $this;
 78:     }
 79: 
 80: 
 81:     /**
 82:      * Returns the special value which is treated as empty string.
 83:      * @return string
 84:      */
 85:     public function getEmptyValue()
 86:     {
 87:         return $this->emptyValue;
 88:     }
 89: 
 90: 
 91:     /**
 92:      * Appends input string filter callback.
 93:      * @param  callable
 94:      * @return self
 95:      */
 96:     public function addFilter($filter)
 97:     {
 98:         $this->filters[] = Nette\Utils\Callback::check($filter);
 99:         return $this;
100:     }
101: 
102: 
103:     public function getControl()
104:     {
105:         $el = parent::getControl();
106:         if ($this->emptyValue !== '') {
107:             $el->attrs['data-nette-empty-value'] = $this->translate($this->emptyValue);
108:         }
109:         if (isset($el->placeholder)) {
110:             $el->placeholder = $this->translate($el->placeholder);
111:         }
112:         return $el;
113:     }
114: 
115: 
116:     public function addRule($operation, $message = NULL, $arg = NULL)
117:     {
118:         if ($operation === Form::LENGTH || $operation === Form::MAX_LENGTH) {
119:             $tmp = is_array($arg) ? $arg[1] : $arg;
120:             $this->control->maxlength = is_scalar($tmp) ? $tmp : NULL;
121:         }
122:         return parent::addRule($operation, $message, $arg);
123:     }
124: 
125: 
126:     /********************* validators ****************d*g**/
127: 
128: 
129:     /**
130:      * Email validator: is control's value valid email address?
131:      * @param  TextBase
132:      * @return bool
133:      */
134:     public static function validateEmail(TextBase $control)
135:     {
136:         return Validators::isEmail($control->getValue());
137:     }
138: 
139: 
140:     /**
141:      * URL validator: is control's value valid URL?
142:      * @param  TextBase
143:      * @return bool
144:      */
145:     public static function validateUrl(TextBase $control)
146:     {
147:         if (Validators::isUrl($value = $control->getValue())) {
148:             return TRUE;
149: 
150:         } elseif (Validators::isUrl($value = "http://$value")) {
151:             $control->setValue($value);
152:             return TRUE;
153:         }
154:         return FALSE;
155:     }
156: 
157: 
158:     /**
159:      * URL string cleanup.
160:      * @param  string
161:      * @return string
162:      */
163:     public static function filterUrl($s)
164:     {
165:         return Validators::isUrl('http://' . $s) ? 'http://' . $s : $s;
166:     }
167: 
168: 
169:     /** @deprecated */
170:     public static function validateRegexp(TextBase $control, $regexp)
171:     {
172:         trigger_error('Validator REGEXP is deprecated; use PATTERN instead (which is matched against the entire value and is case sensitive).', E_USER_DEPRECATED);
173:         return (bool) Strings::match($control->getValue(), $regexp);
174:     }
175: 
176: 
177:     /**
178:      * Regular expression validator: matches control's value regular expression?
179:      * @param  TextBase
180:      * @param  string
181:      * @return bool
182:      */
183:     public static function validatePattern(TextBase $control, $pattern)
184:     {
185:         return (bool) Strings::match($control->getValue(), "\x01^($pattern)\\z\x01u");
186:     }
187: 
188: 
189:     /**
190:      * Integer validator: is a control's value decimal number?
191:      * @param  TextBase
192:      * @return bool
193:      */
194:     public static function validateInteger(TextBase $control)
195:     {
196:         if (Validators::isNumericInt($value = $control->getValue())) {
197:             if (!is_float($tmp = $value * 1)) { // bigint leave as string
198:                 $control->setValue($tmp);
199:             }
200:             return TRUE;
201:         }
202:         return FALSE;
203:     }
204: 
205: 
206:     /**
207:      * Float validator: is a control's value float number?
208:      * @param  TextBase
209:      * @return bool
210:      */
211:     public static function validateFloat(TextBase $control)
212:     {
213:         $value = self::filterFloat($control->getValue());
214:         if (Validators::isNumeric($value)) {
215:             $control->setValue((float) $value);
216:             return TRUE;
217:         }
218:         return FALSE;
219:     }
220: 
221: 
222:     /**
223:      * Float string cleanup.
224:      * @param  string
225:      * @return string
226:      */
227:     public static function filterFloat($s)
228:     {
229:         return str_replace(array(' ', ','), array('', '.'), $s);
230:     }
231: 
232: }
233: 
API documentation generated by ApiGen 2.8.0