Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy
    • Bridges
      • Nette

Classes

  • BaseControl
  • Button
  • Checkbox
  • CheckboxList
  • ChoiceControl
  • CsrfProtection
  • HiddenField
  • ImageButton
  • MultiChoiceControl
  • MultiSelectBox
  • RadioList
  • SelectBox
  • SubmitButton
  • TextArea
  • TextBase
  • TextInput
  • UploadControl
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  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: use Nette\Forms\IControl;
 12: use Nette\Utils\Html;
 13: use Nette\Forms\Form;
 14: 
 15: 
 16: /**
 17:  * Base class that implements the basic functionality common to form controls.
 18:  *
 19:  * @property-read Form $form
 20:  * @property-read string $htmlName
 21:  * @property   string $htmlId
 22:  * @property-read array $options
 23:  * @property   Nette\Localization\ITranslator|NULL $translator
 24:  * @property   mixed $value
 25:  * @property-read bool $filled
 26:  * @property-write $defaultValue
 27:  * @property   bool $disabled
 28:  * @property   bool $omitted
 29:  * @property-read Html $control
 30:  * @property-read Html $label
 31:  * @property-read Html $controlPrototype
 32:  * @property-read Html $labelPrototype
 33:  * @property-read Nette\Forms\Rules $rules
 34:  * @property   bool $required
 35:  * @property-read array $errors
 36:  */
 37: abstract class BaseControl extends Nette\ComponentModel\Component implements IControl
 38: {
 39:     /** @var string */
 40:     public static $idMask = 'frm-%s';
 41: 
 42:     /** @var string textual caption or label */
 43:     public $caption;
 44: 
 45:     /** @var mixed current control value */
 46:     protected $value;
 47: 
 48:     /** @var Html  control element template */
 49:     protected $control;
 50: 
 51:     /** @var Html  label element template */
 52:     protected $label;
 53: 
 54:     /** @var array */
 55:     private $errors = array();
 56: 
 57:     /** @var bool */
 58:     protected $disabled = FALSE;
 59: 
 60:     /** @var bool */
 61:     private $omitted = FALSE;
 62: 
 63:     /** @var Nette\Forms\Rules */
 64:     private $rules;
 65: 
 66:     /** @var Nette\Localization\ITranslator */
 67:     private $translator = TRUE; // means autodetect
 68: 
 69:     /** @var array user options */
 70:     private $options = array();
 71: 
 72: 
 73:     /**
 74:      * @param  string  caption
 75:      */
 76:     public function __construct($caption = NULL)
 77:     {
 78:         $this->monitor('Nette\Forms\Form');
 79:         parent::__construct();
 80:         $this->control = Html::el('input', array('type' => NULL, 'name' => NULL));
 81:         $this->label = Html::el('label');
 82:         $this->caption = $caption;
 83:         $this->rules = new Nette\Forms\Rules($this);
 84:         $this->setValue(NULL);
 85:     }
 86: 
 87: 
 88:     /**
 89:      * This method will be called when the component becomes attached to Form.
 90:      * @param  Nette\ComponentModel\IComponent
 91:      * @return void
 92:      */
 93:     protected function attached($form)
 94:     {
 95:         if (!$this->isDisabled() && $form instanceof Form && $form->isAnchored() && $form->isSubmitted()) {
 96:             $this->loadHttpData();
 97:         }
 98:     }
 99: 
100: 
101:     /**
102:      * Returns form.
103:      * @param  bool   throw exception if form doesn't exist?
104:      * @return Form
105:      */
106:     public function getForm($need = TRUE)
107:     {
108:         return $this->lookup('Nette\Forms\Form', $need);
109:     }
110: 
111: 
112:     /**
113:      * Loads HTTP data.
114:      * @return void
115:      */
116:     public function loadHttpData()
117:     {
118:         $this->setValue($this->getHttpData(Form::DATA_TEXT));
119:     }
120: 
121: 
122:     /**
123:      * Loads HTTP data.
124:      * @return mixed
125:      */
126:     public function getHttpData($type, $htmlTail = NULL)
127:     {
128:         return $this->getForm()->getHttpData($type, $this->getHtmlName() . $htmlTail);
129:     }
130: 
131: 
132:     /**
133:      * Returns HTML name of control.
134:      * @return string
135:      */
136:     public function getHtmlName()
137:     {
138:         return Nette\Forms\Helpers::generateHtmlName($this->lookupPath('Nette\Forms\Form'));
139:     }
140: 
141: 
142:     /********************* interface IFormControl ****************d*g**/
143: 
144: 
145:     /**
146:      * Sets control's value.
147:      * @return self
148:      */
149:     public function setValue($value)
150:     {
151:         $this->value = $value;
152:         return $this;
153:     }
154: 
155: 
156:     /**
157:      * Returns control's value.
158:      * @return mixed
159:      */
160:     public function getValue()
161:     {
162:         return $this->value;
163:     }
164: 
165: 
166:     /**
167:      * Is control filled?
168:      * @return bool
169:      */
170:     public function isFilled()
171:     {
172:         $value = $this->getValue();
173:         return $value !== NULL && $value !== array() && $value !== '';
174:     }
175: 
176: 
177:     /**
178:      * Sets control's default value.
179:      * @return self
180:      */
181:     public function setDefaultValue($value)
182:     {
183:         $form = $this->getForm(FALSE);
184:         if ($this->isDisabled() || !$form || !$form->isAnchored() || !$form->isSubmitted()) {
185:             $this->setValue($value);
186:         }
187:         return $this;
188:     }
189: 
190: 
191:     /**
192:      * Disables or enables control.
193:      * @param  bool
194:      * @return self
195:      */
196:     public function setDisabled($value = TRUE)
197:     {
198:         if ($this->disabled = (bool) $value) {
199:             $this->omitted = TRUE;
200:             $this->setValue(NULL);
201:         }
202:         return $this;
203:     }
204: 
205: 
206:     /**
207:      * Is control disabled?
208:      * @return bool
209:      */
210:     public function isDisabled()
211:     {
212:         return $this->disabled === TRUE;
213:     }
214: 
215: 
216:     /**
217:      * Sets whether control value is excluded from $form->getValues() result.
218:      * @param  bool
219:      * @return self
220:      */
221:     public function setOmitted($value = TRUE)
222:     {
223:         $this->omitted = (bool) $value;
224:         return $this;
225:     }
226: 
227: 
228:     /**
229:      * Is control value excluded from $form->getValues() result?
230:      * @return bool
231:      */
232:     public function isOmitted()
233:     {
234:         return $this->omitted;
235:     }
236: 
237: 
238:     /********************* rendering ****************d*g**/
239: 
240: 
241:     /**
242:      * Generates control's HTML element.
243:      * @return Html|string
244:      */
245:     public function getControl()
246:     {
247:         $this->setOption('rendered', TRUE);
248:         $el = clone $this->control;
249:         return $el->addAttributes(array(
250:             'name' => $this->getHtmlName(),
251:             'id' => $this->getHtmlId(),
252:             'required' => $this->isRequired(),
253:             'disabled' => $this->isDisabled(),
254:             'data-nette-rules' => Nette\Forms\Helpers::exportRules($this->rules) ?: NULL,
255:         ));
256:     }
257: 
258: 
259:     /**
260:      * Generates label's HTML element.
261:      * @param  string
262:      * @return Html|string
263:      */
264:     public function getLabel($caption = NULL)
265:     {
266:         $label = clone $this->label;
267:         $label->for = $this->getHtmlId();
268:         $label->setText($this->translate($caption === NULL ? $this->caption : $caption));
269:         return $label;
270:     }
271: 
272: 
273:     /**
274:      * Returns control's HTML element template.
275:      * @return Html
276:      */
277:     public function getControlPrototype()
278:     {
279:         return $this->control;
280:     }
281: 
282: 
283:     /**
284:      * Returns label's HTML element template.
285:      * @return Html
286:      */
287:     public function getLabelPrototype()
288:     {
289:         return $this->label;
290:     }
291: 
292: 
293:     /**
294:      * Changes control's HTML id.
295:      * @param  string new ID, or FALSE or NULL
296:      * @return self
297:      */
298:     public function setHtmlId($id)
299:     {
300:         $this->control->id = $id;
301:         return $this;
302:     }
303: 
304: 
305:     /**
306:      * Returns control's HTML id.
307:      * @return string
308:      */
309:     public function getHtmlId()
310:     {
311:         if (!isset($this->control->id)) {
312:             $this->control->id = sprintf(self::$idMask, $this->lookupPath());
313:         }
314:         return $this->control->id;
315:     }
316: 
317: 
318:     /**
319:      * Changes control's HTML attribute.
320:      * @param  string name
321:      * @param  mixed  value
322:      * @return self
323:      */
324:     public function setAttribute($name, $value = TRUE)
325:     {
326:         $this->control->$name = $value;
327:         return $this;
328:     }
329: 
330: 
331:     /********************* translator ****************d*g**/
332: 
333: 
334:     /**
335:      * Sets translate adapter.
336:      * @return self
337:      */
338:     public function setTranslator(Nette\Localization\ITranslator $translator = NULL)
339:     {
340:         $this->translator = $translator;
341:         return $this;
342:     }
343: 
344: 
345:     /**
346:      * Returns translate adapter.
347:      * @return Nette\Localization\ITranslator|NULL
348:      */
349:     public function getTranslator()
350:     {
351:         if ($this->translator === TRUE) {
352:             return $this->getForm(FALSE) ? $this->getForm()->getTranslator() : NULL;
353:         }
354:         return $this->translator;
355:     }
356: 
357: 
358:     /**
359:      * Returns translated string.
360:      * @param  mixed
361:      * @param  int      plural count
362:      * @return string
363:      */
364:     public function translate($value, $count = NULL)
365:     {
366:         if ($translator = $this->getTranslator()) {
367:             $tmp = is_array($value) ? array(& $value) : array(array(& $value));
368:             foreach ($tmp[0] as & $v) {
369:                 if ($v != NULL && !$v instanceof Html) { // intentionally ==
370:                     $v = $translator->translate($v, $count);
371:                 }
372:             }
373:         }
374:         return $value;
375:     }
376: 
377: 
378:     /********************* rules ****************d*g**/
379: 
380: 
381:     /**
382:      * Adds a validation rule.
383:      * @param  mixed      rule type
384:      * @param  string     message to display for invalid data
385:      * @param  mixed      optional rule arguments
386:      * @return self
387:      */
388:     public function addRule($validator, $message = NULL, $arg = NULL)
389:     {
390:         $this->rules->addRule($validator, $message, $arg);
391:         return $this;
392:     }
393: 
394: 
395:     /**
396:      * Adds a validation condition a returns new branch.
397:      * @param  mixed     condition type
398:      * @param  mixed     optional condition arguments
399:      * @return Nette\Forms\Rules      new branch
400:      */
401:     public function addCondition($validator, $value = NULL)
402:     {
403:         return $this->rules->addCondition($validator, $value);
404:     }
405: 
406: 
407:     /**
408:      * Adds a validation condition based on another control a returns new branch.
409:      * @param  IControl form control
410:      * @param  mixed      condition type
411:      * @param  mixed      optional condition arguments
412:      * @return Nette\Forms\Rules      new branch
413:      */
414:     public function addConditionOn(IControl $control, $validator, $value = NULL)
415:     {
416:         return $this->rules->addConditionOn($control, $validator, $value);
417:     }
418: 
419: 
420:     /**
421:      * @return Nette\Forms\Rules
422:      */
423:     public function getRules()
424:     {
425:         return $this->rules;
426:     }
427: 
428: 
429:     /**
430:      * Makes control mandatory.
431:      * @param  mixed  state or error message
432:      * @return self
433:      */
434:     public function setRequired($value = TRUE)
435:     {
436:         $this->rules->setRequired($value);
437:         return $this;
438:     }
439: 
440: 
441:     /**
442:      * Is control mandatory?
443:      * @return bool
444:      */
445:     public function isRequired()
446:     {
447:         return $this->rules->isRequired();
448:     }
449: 
450: 
451:     /**
452:      * Performs the server side validation.
453:      * @return void
454:      */
455:     public function validate()
456:     {
457:         if ($this->isDisabled()) {
458:             return;
459:         }
460:         $this->cleanErrors();
461:         $this->rules->validate();
462:     }
463: 
464: 
465:     /**
466:      * Adds error message to the list.
467:      * @param  string  error message
468:      * @return void
469:      */
470:     public function addError($message)
471:     {
472:         $this->errors[] = $message;
473:     }
474: 
475: 
476:     /**
477:      * Returns errors corresponding to control.
478:      * @return string
479:      */
480:     public function getError()
481:     {
482:         return $this->errors ? implode(' ', array_unique($this->errors)) : NULL;
483:     }
484: 
485: 
486:     /**
487:      * Returns errors corresponding to control.
488:      * @return array
489:      */
490:     public function getErrors()
491:     {
492:         return array_unique($this->errors);
493:     }
494: 
495: 
496:     /**
497:      * @return bool
498:      */
499:     public function hasErrors()
500:     {
501:         return (bool) $this->errors;
502:     }
503: 
504: 
505:     /**
506:      * @return void
507:      */
508:     public function cleanErrors()
509:     {
510:         $this->errors = array();
511:     }
512: 
513: 
514:     /** @deprecated */
515:     protected static function exportRules($rules)
516:     {
517:         trigger_error(__METHOD__ . '() is deprecated; use Nette\Forms\Helpers::exportRules() instead.', E_USER_DEPRECATED);
518:         return Nette\Forms\Helpers::exportRules($rules);
519:     }
520: 
521: 
522:     /********************* user data ****************d*g**/
523: 
524: 
525:     /**
526:      * Sets user-specific option.
527:      * @return self
528:      */
529:     public function setOption($key, $value)
530:     {
531:         if ($value === NULL) {
532:             unset($this->options[$key]);
533:         } else {
534:             $this->options[$key] = $value;
535:         }
536:         return $this;
537:     }
538: 
539: 
540:     /**
541:      * Returns user-specific option.
542:      * @return mixed
543:      */
544:     public function getOption($key, $default = NULL)
545:     {
546:         return isset($this->options[$key]) ? $this->options[$key] : $default;
547:     }
548: 
549: 
550:     /**
551:      * Returns user-specific options.
552:      * @return array
553:      */
554:     public function getOptions()
555:     {
556:         return $this->options;
557:     }
558: 
559: }
560: 
Nette 2.3.4 API API documentation generated by ApiGen 2.8.0