Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • 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

Classes

  • BaseControl
  • Button
  • Checkbox
  • CheckboxList
  • ChoiceControl
  • CsrfProtection
  • HiddenField
  • ImageButton
  • MultiChoiceControl
  • MultiSelectBox
  • RadioList
  • SelectBox
  • SubmitButton
  • TextArea
  • TextBase
  • TextInput
  • UploadControl
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (https://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\Utils\Validators;
 14: use Nette\Forms\Form;
 15: 
 16: 
 17: /**
 18:  * Base class that implements the basic functionality common to form controls.
 19:  *
 20:  * @author     David Grudl
 21:  *
 22:  * @property-read Form $form
 23:  * @property-read string $htmlName
 24:  * @property   string $htmlId
 25:  * @property-read array $options
 26:  * @property   Nette\Localization\ITranslator|NULL $translator
 27:  * @property   mixed $value
 28:  * @property-read bool $filled
 29:  * @property-write $defaultValue
 30:  * @property   bool $disabled
 31:  * @property   bool $omitted
 32:  * @property-read Html $control
 33:  * @property-read Html $label
 34:  * @property-read Html $controlPrototype
 35:  * @property-read Html $labelPrototype
 36:  * @property-read Nette\Forms\Rules $rules
 37:  * @property   bool $required
 38:  * @property-read array $errors
 39:  */
 40: abstract class BaseControl extends Nette\ComponentModel\Component implements IControl
 41: {
 42:     /** @var string */
 43:     public static $idMask = 'frm-%s';
 44: 
 45:     /** @var string textual caption or label */
 46:     public $caption;
 47: 
 48:     /** @var mixed current control value */
 49:     protected $value;
 50: 
 51:     /** @var Html  control element template */
 52:     protected $control;
 53: 
 54:     /** @var Html  label element template */
 55:     protected $label;
 56: 
 57:     /** @var array */
 58:     private $errors = array();
 59: 
 60:     /** @var bool */
 61:     protected $disabled = FALSE;
 62: 
 63:     /** @var bool */
 64:     private $omitted = FALSE;
 65: 
 66:     /** @var Nette\Forms\Rules */
 67:     private $rules;
 68: 
 69:     /** @var Nette\Localization\ITranslator */
 70:     private $translator = TRUE; // means autodetect
 71: 
 72:     /** @var array user options */
 73:     private $options = array();
 74: 
 75: 
 76:     /**
 77:      * @param  string  caption
 78:      */
 79:     public function __construct($caption = NULL)
 80:     {
 81:         $this->monitor('Nette\Forms\Form');
 82:         parent::__construct();
 83:         $this->control = Html::el('input', array('type' => NULL, 'name' => NULL));
 84:         $this->label = Html::el('label');
 85:         $this->caption = $caption;
 86:         $this->rules = new Nette\Forms\Rules($this);
 87:         $this->setValue(NULL);
 88:     }
 89: 
 90: 
 91:     /**
 92:      * This method will be called when the component becomes attached to Form.
 93:      * @param  Nette\ComponentModel\IComponent
 94:      * @return void
 95:      */
 96:     protected function attached($form)
 97:     {
 98:         if (!$this->isDisabled() && $form instanceof Form && $form->isAnchored() && $form->isSubmitted()) {
 99:             $this->loadHttpData();
100:         }
101:     }
102: 
103: 
104:     /**
105:      * Returns form.
106:      * @param  bool   throw exception if form doesn't exist?
107:      * @return Form
108:      */
109:     public function getForm($need = TRUE)
110:     {
111:         return $this->lookup('Nette\Forms\Form', $need);
112:     }
113: 
114: 
115:     /**
116:      * Loads HTTP data.
117:      * @return void
118:      */
119:     public function loadHttpData()
120:     {
121:         $this->setValue($this->getHttpData(Form::DATA_TEXT));
122:     }
123: 
124: 
125:     /**
126:      * Loads HTTP data.
127:      * @return mixed
128:      */
129:     public function getHttpData($type, $htmlTail = NULL)
130:     {
131:         return $this->getForm()->getHttpData($type, $this->getHtmlName() . $htmlTail);
132:     }
133: 
134: 
135:     /**
136:      * Returns HTML name of control.
137:      * @return string
138:      */
139:     public function getHtmlName()
140:     {
141:         return Nette\Forms\Helpers::generateHtmlName($this->lookupPath('Nette\Forms\Form'));
142:     }
143: 
144: 
145:     /********************* interface IControl ****************d*g**/
146: 
147: 
148:     /**
149:      * Sets control's value.
150:      * @return self
151:      */
152:     public function setValue($value)
153:     {
154:         $this->value = $value;
155:         return $this;
156:     }
157: 
158: 
159:     /**
160:      * Returns control's value.
161:      * @return mixed
162:      */
163:     public function getValue()
164:     {
165:         return $this->value;
166:     }
167: 
168: 
169:     /**
170:      * Is control filled?
171:      * @return bool
172:      */
173:     public function isFilled()
174:     {
175:         $value = $this->getValue();
176:         return $value !== NULL && $value !== array() && $value !== '';
177:     }
178: 
179: 
180:     /**
181:      * Sets control's default value.
182:      * @return self
183:      */
184:     public function setDefaultValue($value)
185:     {
186:         $form = $this->getForm(FALSE);
187:         if ($this->isDisabled() || !$form || !$form->isAnchored() || !$form->isSubmitted()) {
188:             $this->setValue($value);
189:         }
190:         return $this;
191:     }
192: 
193: 
194:     /**
195:      * Disables or enables control.
196:      * @param  bool
197:      * @return self
198:      */
199:     public function setDisabled($value = TRUE)
200:     {
201:         if ($this->disabled = (bool) $value) {
202:             $this->omitted = TRUE;
203:             $this->setValue(NULL);
204:         }
205:         return $this;
206:     }
207: 
208: 
209:     /**
210:      * Is control disabled?
211:      * @return bool
212:      */
213:     public function isDisabled()
214:     {
215:         return $this->disabled === TRUE;
216:     }
217: 
218: 
219:     /**
220:      * Sets whether control value is excluded from $form->getValues() result.
221:      * @param  bool
222:      * @return self
223:      */
224:     public function setOmitted($value = TRUE)
225:     {
226:         $this->omitted = (bool) $value;
227:         return $this;
228:     }
229: 
230: 
231:     /**
232:      * Is control value excluded from $form->getValues() result?
233:      * @return bool
234:      */
235:     public function isOmitted()
236:     {
237:         return $this->omitted;
238:     }
239: 
240: 
241:     /********************* rendering ****************d*g**/
242: 
243: 
244:     /**
245:      * Generates control's HTML element.
246:      * @return Html|string
247:      */
248:     public function getControl()
249:     {
250:         $this->setOption('rendered', TRUE);
251:         $el = clone $this->control;
252:         return $el->addAttributes(array(
253:             'name' => $this->getHtmlName(),
254:             'id' => $this->getHtmlId(),
255:             'required' => $this->isRequired(),
256:             'disabled' => $this->isDisabled(),
257:             'data-nette-rules' => self::exportRules($this->rules) ?: NULL,
258:         ));
259:     }
260: 
261: 
262:     /**
263:      * Generates label's HTML element.
264:      * @param  string
265:      * @return Html|string
266:      */
267:     public function getLabel($caption = NULL)
268:     {
269:         $label = clone $this->label;
270:         $label->for = $this->getHtmlId();
271:         $label->setText($this->translate($caption === NULL ? $this->caption : $caption));
272:         return $label;
273:     }
274: 
275: 
276:     /**
277:      * Returns control's HTML element template.
278:      * @return Html
279:      */
280:     public function getControlPrototype()
281:     {
282:         return $this->control;
283:     }
284: 
285: 
286:     /**
287:      * Returns label's HTML element template.
288:      * @return Html
289:      */
290:     public function getLabelPrototype()
291:     {
292:         return $this->label;
293:     }
294: 
295: 
296:     /**
297:      * Changes control's HTML id.
298:      * @param  string new ID, or FALSE or NULL
299:      * @return self
300:      */
301:     public function setHtmlId($id)
302:     {
303:         $this->control->id = $id;
304:         return $this;
305:     }
306: 
307: 
308:     /**
309:      * Returns control's HTML id.
310:      * @return string
311:      */
312:     public function getHtmlId()
313:     {
314:         if (!isset($this->control->id)) {
315:             $this->control->id = sprintf(self::$idMask, $this->lookupPath());
316:         }
317:         return $this->control->id;
318:     }
319: 
320: 
321:     /**
322:      * Changes control's HTML attribute.
323:      * @param  string name
324:      * @param  mixed  value
325:      * @return self
326:      */
327:     public function setAttribute($name, $value = TRUE)
328:     {
329:         $this->control->$name = $value;
330:         return $this;
331:     }
332: 
333: 
334:     /********************* translator ****************d*g**/
335: 
336: 
337:     /**
338:      * Sets translate adapter.
339:      * @return self
340:      */
341:     public function setTranslator(Nette\Localization\ITranslator $translator = NULL)
342:     {
343:         $this->translator = $translator;
344:         return $this;
345:     }
346: 
347: 
348:     /**
349:      * Returns translate adapter.
350:      * @return Nette\Localization\ITranslator|NULL
351:      */
352:     public function getTranslator()
353:     {
354:         if ($this->translator === TRUE) {
355:             return $this->getForm(FALSE) ? $this->getForm()->getTranslator() : NULL;
356:         }
357:         return $this->translator;
358:     }
359: 
360: 
361:     /**
362:      * Returns translated string.
363:      * @param  mixed
364:      * @param  int      plural count
365:      * @return string
366:      */
367:     public function translate($value, $count = NULL)
368:     {
369:         if ($translator = $this->getTranslator()) {
370:             $tmp = is_array($value) ? array(& $value) : array(array(& $value));
371:             foreach ($tmp[0] as & $v) {
372:                 if ($v != NULL && !$v instanceof Html) { // intentionally ==
373:                     $v = $translator->translate($v, $count);
374:                 }
375:             }
376:         }
377:         return $value;
378:     }
379: 
380: 
381:     /********************* rules ****************d*g**/
382: 
383: 
384:     /**
385:      * Adds a validation rule.
386:      * @param  mixed      rule type
387:      * @param  string     message to display for invalid data
388:      * @param  mixed      optional rule arguments
389:      * @return self
390:      */
391:     public function addRule($validator, $message = NULL, $arg = NULL)
392:     {
393:         $this->rules->addRule($validator, $message, $arg);
394:         return $this;
395:     }
396: 
397: 
398:     /**
399:      * Adds a validation condition a returns new branch.
400:      * @param  mixed     condition type
401:      * @param  mixed     optional condition arguments
402:      * @return Nette\Forms\Rules      new branch
403:      */
404:     public function addCondition($validator, $value = NULL)
405:     {
406:         return $this->rules->addCondition($validator, $value);
407:     }
408: 
409: 
410:     /**
411:      * Adds a validation condition based on another control a returns new branch.
412:      * @param  IControl form control
413:      * @param  mixed      condition type
414:      * @param  mixed      optional condition arguments
415:      * @return Nette\Forms\Rules      new branch
416:      */
417:     public function addConditionOn(IControl $control, $validator, $value = NULL)
418:     {
419:         return $this->rules->addConditionOn($control, $validator, $value);
420:     }
421: 
422: 
423:     /**
424:      * @return Nette\Forms\Rules
425:      */
426:     public function getRules()
427:     {
428:         return $this->rules;
429:     }
430: 
431: 
432:     /**
433:      * Makes control mandatory.
434:      * @param  mixed  state or error message
435:      * @return self
436:      */
437:     public function setRequired($value = TRUE)
438:     {
439:         $this->rules->setRequired($value);
440:         return $this;
441:     }
442: 
443: 
444:     /**
445:      * Is control mandatory?
446:      * @return bool
447:      */
448:     public function isRequired()
449:     {
450:         return $this->rules->isRequired();
451:     }
452: 
453: 
454:     /**
455:      * Performs the server side validation.
456:      * @return void
457:      */
458:     public function validate()
459:     {
460:         if ($this->isDisabled()) {
461:             return;
462:         }
463:         $this->cleanErrors();
464:         $this->rules->validate();
465:     }
466: 
467: 
468:     /**
469:      * Adds error message to the list.
470:      * @param  string  error message
471:      * @return void
472:      */
473:     public function addError($message)
474:     {
475:         $this->errors[] = $message;
476:     }
477: 
478: 
479:     /**
480:      * Returns errors corresponding to control.
481:      * @return string
482:      */
483:     public function getError()
484:     {
485:         return $this->errors ? implode(' ', array_unique($this->errors)) : NULL;
486:     }
487: 
488: 
489:     /**
490:      * Returns errors corresponding to control.
491:      * @return array
492:      */
493:     public function getErrors()
494:     {
495:         return array_unique($this->errors);
496:     }
497: 
498: 
499:     /**
500:      * @return bool
501:      */
502:     public function hasErrors()
503:     {
504:         return (bool) $this->errors;
505:     }
506: 
507: 
508:     /**
509:      * @return void
510:      */
511:     public function cleanErrors()
512:     {
513:         $this->errors = array();
514:     }
515: 
516: 
517:     /**
518:      * @return array
519:      */
520:     protected static function exportRules($rules)
521:     {
522:         $payload = array();
523:         foreach ($rules as $rule) {
524:             if (!is_string($op = $rule->validator)) {
525:                 if (!Nette\Utils\Callback::isStatic($op)) {
526:                     continue;
527:                 }
528:                 $op = Nette\Utils\Callback::toString($op);
529:             }
530:             if ($rule->branch) {
531:                 $item = array(
532:                     'op' => ($rule->isNegative ? '~' : '') . $op,
533:                     'rules' => static::exportRules($rule->branch, FALSE),
534:                     'control' => $rule->control->getHtmlName()
535:                 );
536:                 if ($rule->branch->getToggles()) {
537:                     $item['toggle'] = $rule->branch->getToggles();
538:                 }
539:             } else {
540:                 $item = array('op' => ($rule->isNegative ? '~' : '') . $op, 'msg' => $rules->formatMessage($rule, FALSE));
541:             }
542: 
543:             if (is_array($rule->arg)) {
544:                 foreach ($rule->arg as $key => $value) {
545:                     $item['arg'][$key] = $value instanceof IControl ? array('control' => $value->getHtmlName()) : $value;
546:                 }
547:             } elseif ($rule->arg !== NULL) {
548:                 $item['arg'] = $rule->arg instanceof IControl ? array('control' => $rule->arg->getHtmlName()) : $rule->arg;
549:             }
550: 
551:             $payload[] = $item;
552:         }
553:         return $payload;
554:     }
555: 
556: 
557:     /********************* validators ****************d*g**/
558: 
559: 
560:     /**
561:      * Is control's value equal with second parameter?
562:      * @return bool
563:      * @internal
564:      */
565:     public static function validateEqual(IControl $control, $arg)
566:     {
567:         $value = $control->getValue();
568:         foreach ((is_array($value) ? $value : array($value)) as $val) {
569:             foreach ((is_array($arg) ? $arg : array($arg)) as $item) {
570:                 if ((string) $val === (string) $item) {
571:                     continue 2;
572:                 }
573:             }
574:             return FALSE;
575:         }
576:         return TRUE;
577:     }
578: 
579: 
580:     /**
581:      * Is control's value not equal with second parameter?
582:      * @return bool
583:      * @internal
584:      */
585:     public static function validateNotEqual(IControl $control, $arg)
586:     {
587:         return !static::validateEqual($control, $arg);
588:     }
589: 
590: 
591:     /**
592:      * Is control filled?
593:      * @return bool
594:      * @internal
595:      */
596:     public static function validateFilled(IControl $control)
597:     {
598:         return $control->isFilled();
599:     }
600: 
601: 
602:     /**
603:      * Is control not filled?
604:      * @return bool
605:      * @internal
606:      */
607:     public static function validateBlank(IControl $control)
608:     {
609:         return !$control->isFilled();
610:     }
611: 
612: 
613:     /**
614:      * Is control valid?
615:      * @return bool
616:      * @internal
617:      */
618:     public static function validateValid(IControl $control)
619:     {
620:         return $control->getRules()->validate();
621:     }
622: 
623: 
624:     /**
625:      * Is a control's value number in specified range?
626:      * @return bool
627:      * @internal
628:      */
629:     public static function validateRange(IControl $control, $range)
630:     {
631:         return Validators::isInRange($control->getValue(), $range);
632:     }
633: 
634: 
635:     /**
636:      * Is a control's value number greater than or equal to the specified minimum?
637:      * @return bool
638:      * @internal
639:      */
640:     public static function validateMin(IControl $control, $minimum)
641:     {
642:         return Validators::isInRange($control->getValue(), array($minimum, NULL));
643:     }
644: 
645: 
646:     /**
647:      * Is a control's value number less than or equal to the specified maximum?
648:      * @return bool
649:      * @internal
650:      */
651:     public static function validateMax(IControl $control, $maximum)
652:     {
653:         return Validators::isInRange($control->getValue(), array(NULL, $maximum));
654:     }
655: 
656: 
657:     /**
658:      * Count/length validator. Range is array, min and max length pair.
659:      * @return bool
660:      * @internal
661:      */
662:     public static function validateLength(IControl $control, $range)
663:     {
664:         if (!is_array($range)) {
665:             $range = array($range, $range);
666:         }
667:         $value = $control->getValue();
668:         return Validators::isInRange(is_array($value) ? count($value) : Nette\Utils\Strings::length($value), $range);
669:     }
670: 
671: 
672:     /**
673:      * Has control's value minimal count/length?
674:      * @return bool
675:      * @internal
676:      */
677:     public static function validateMinLength(IControl $control, $length)
678:     {
679:         return static::validateLength($control, array($length, NULL));
680:     }
681: 
682: 
683:     /**
684:      * Is control's value count/length in limit?
685:      * @return bool
686:      * @internal
687:      */
688:     public static function validateMaxLength(IControl $control, $length)
689:     {
690:         return static::validateLength($control, array(NULL, $length));
691:     }
692: 
693: 
694:     /********************* user data ****************d*g**/
695: 
696: 
697:     /**
698:      * Sets user-specific option.
699:      * @return self
700:      */
701:     public function setOption($key, $value)
702:     {
703:         if ($value === NULL) {
704:             unset($this->options[$key]);
705:         } else {
706:             $this->options[$key] = $value;
707:         }
708:         return $this;
709:     }
710: 
711: 
712:     /**
713:      * Returns user-specific option.
714:      * @return mixed
715:      */
716:     public function getOption($key, $default = NULL)
717:     {
718:         return isset($this->options[$key]) ? $this->options[$key] : $default;
719:     }
720: 
721: 
722:     /**
723:      * Returns user-specific options.
724:      * @return array
725:      */
726:     public function getOptions()
727:     {
728:         return $this->options;
729:     }
730: 
731: }
732: 
Nette 2.2 API documentation generated by ApiGen 2.8.0