Namespaces

  • 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
  • none

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\Forms\Form;
 14: use Nette\Forms\Rule;
 15: 
 16: 
 17: /**
 18:  * Base class that implements the basic functionality common to form controls.
 19:  *
 20:  * @author     David Grudl
 21:  *
 22:  * @property-read Nette\Forms\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 Nette\Utils\Html $control
 33:  * @property-read Nette\Utils\Html $label
 34:  * @property-read Nette\Utils\Html $controlPrototype
 35:  * @property-read Nette\Utils\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 Nette\Utils\Html  control element template */
 52:     protected $control;
 53: 
 54:     /** @var Nette\Utils\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 Nette\Forms\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 IFormControl ****************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 Nette\Utils\Html|string
247:      */
248:     public function getControl()
249:     {
250:         $this->setOption('rendered', TRUE);
251: 
252:         $rules = self::exportRules($this->rules);
253:         $el = clone $this->control;
254:         return $el->addAttributes(array(
255:             'name' => $this->getHtmlName(),
256:             'id' => $this->getHtmlId(),
257:             'required' => $this->isRequired(),
258:             'disabled' => $this->isDisabled(),
259:             'data-nette-rules' => $rules ? Nette\Utils\Json::encode($rules) : NULL,
260:         ));
261:     }
262: 
263: 
264:     /**
265:      * Generates label's HTML element.
266:      * @param  string
267:      * @return Nette\Utils\Html|string
268:      */
269:     public function getLabel($caption = NULL)
270:     {
271:         $label = clone $this->label;
272:         $label->for = $this->getHtmlId();
273:         $label->setText($this->translate($caption === NULL ? $this->caption : $caption));
274:         return $label;
275:     }
276: 
277: 
278:     /**
279:      * Returns control's HTML element template.
280:      * @return Nette\Utils\Html
281:      */
282:     public function getControlPrototype()
283:     {
284:         return $this->control;
285:     }
286: 
287: 
288:     /**
289:      * Returns label's HTML element template.
290:      * @return Nette\Utils\Html
291:      */
292:     public function getLabelPrototype()
293:     {
294:         return $this->label;
295:     }
296: 
297: 
298:     /**
299:      * Changes control's HTML id.
300:      * @param  string new ID, or FALSE or NULL
301:      * @return self
302:      */
303:     public function setHtmlId($id)
304:     {
305:         $this->control->id = $id;
306:         return $this;
307:     }
308: 
309: 
310:     /**
311:      * Returns control's HTML id.
312:      * @return string
313:      */
314:     public function getHtmlId()
315:     {
316:         if (!isset($this->control->id)) {
317:             $this->control->id = sprintf(self::$idMask, $this->lookupPath(NULL));
318:         }
319:         return $this->control->id;
320:     }
321: 
322: 
323:     /**
324:      * Changes control's HTML attribute.
325:      * @param  string name
326:      * @param  mixed  value
327:      * @return self
328:      */
329:     public function setAttribute($name, $value = TRUE)
330:     {
331:         $this->control->$name = $value;
332:         return $this;
333:     }
334: 
335: 
336:     /********************* translator ****************d*g**/
337: 
338: 
339:     /**
340:      * Sets translate adapter.
341:      * @return self
342:      */
343:     public function setTranslator(Nette\Localization\ITranslator $translator = NULL)
344:     {
345:         $this->translator = $translator;
346:         return $this;
347:     }
348: 
349: 
350:     /**
351:      * Returns translate adapter.
352:      * @return Nette\Localization\ITranslator|NULL
353:      */
354:     public function getTranslator()
355:     {
356:         if ($this->translator === TRUE) {
357:             return $this->getForm(FALSE) ? $this->getForm()->getTranslator() : NULL;
358:         }
359:         return $this->translator;
360:     }
361: 
362: 
363:     /**
364:      * Returns translated string.
365:      * @param  mixed
366:      * @param  int      plural count
367:      * @return string
368:      */
369:     public function translate($value, $count = NULL)
370:     {
371:         if ($translator = $this->getTranslator()) {
372:             $tmp = is_array($value) ? array(& $value) : array(array(& $value));
373:             foreach ($tmp[0] as & $v) {
374:                 if ($v != NULL && !$v instanceof Html) { // intentionally ==
375:                     $v = $translator->translate($v, $count);
376:                 }
377:             }
378:         }
379:         return $value;
380:     }
381: 
382: 
383:     /********************* rules ****************d*g**/
384: 
385: 
386:     /**
387:      * Adds a validation rule.
388:      * @param  mixed      rule type
389:      * @param  string     message to display for invalid data
390:      * @param  mixed      optional rule arguments
391:      * @return self
392:      */
393:     public function addRule($operation, $message = NULL, $arg = NULL)
394:     {
395:         $this->rules->addRule($operation, $message, $arg);
396:         return $this;
397:     }
398: 
399: 
400:     /**
401:      * Adds a validation condition a returns new branch.
402:      * @param  mixed     condition type
403:      * @param  mixed     optional condition arguments
404:      * @return Nette\Forms\Rules      new branch
405:      */
406:     public function addCondition($operation, $value = NULL)
407:     {
408:         return $this->rules->addCondition($operation, $value);
409:     }
410: 
411: 
412:     /**
413:      * Adds a validation condition based on another control a returns new branch.
414:      * @param  Nette\Forms\IControl form control
415:      * @param  mixed      condition type
416:      * @param  mixed      optional condition arguments
417:      * @return Nette\Forms\Rules      new branch
418:      */
419:     public function addConditionOn(IControl $control, $operation, $value = NULL)
420:     {
421:         return $this->rules->addConditionOn($control, $operation, $value);
422:     }
423: 
424: 
425:     /**
426:      * @return Nette\Forms\Rules
427:      */
428:     public function getRules()
429:     {
430:         return $this->rules;
431:     }
432: 
433: 
434:     /**
435:      * Makes control mandatory.
436:      * @param  mixed  state or error message
437:      * @return self
438:      */
439:     public function setRequired($value = TRUE)
440:     {
441:         $this->rules->setRequired($value);
442:         return $this;
443:     }
444: 
445: 
446:     /**
447:      * Is control mandatory?
448:      * @return bool
449:      */
450:     public function isRequired()
451:     {
452:         return $this->rules->isRequired();
453:     }
454: 
455: 
456:     /**
457:      * Performs the server side validation.
458:      * @return void
459:      */
460:     public function validate()
461:     {
462:         if ($this->isDisabled()) {
463:             return;
464:         }
465:         $this->cleanErrors();
466:         $this->rules->validate();
467:     }
468: 
469: 
470:     /**
471:      * Adds error message to the list.
472:      * @param  string  error message
473:      * @return void
474:      */
475:     public function addError($message)
476:     {
477:         $this->errors[] = $message;
478:     }
479: 
480: 
481:     /**
482:      * Returns errors corresponding to control.
483:      * @return string
484:      */
485:     public function getError()
486:     {
487:         return $this->errors ? implode(' ', array_unique($this->errors)) : NULL;
488:     }
489: 
490: 
491:     /**
492:      * Returns errors corresponding to control.
493:      * @return array
494:      */
495:     public function getErrors()
496:     {
497:         return array_unique($this->errors);
498:     }
499: 
500: 
501:     /**
502:      * @return bool
503:      */
504:     public function hasErrors()
505:     {
506:         return (bool) $this->errors;
507:     }
508: 
509: 
510:     /**
511:      * @return void
512:      */
513:     public function cleanErrors()
514:     {
515:         $this->errors = array();
516:     }
517: 
518: 
519:     /**
520:      * @return array
521:      */
522:     protected static function exportRules($rules)
523:     {
524:         $payload = array();
525:         foreach ($rules as $rule) {
526:             if (!is_string($op = $rule->operation)) {
527:                 if (!Nette\Utils\Callback::isStatic($op)) {
528:                     continue;
529:                 }
530:                 $op = Nette\Utils\Callback::toString($op);
531:             }
532:             if ($rule->type === Rule::VALIDATOR) {
533:                 $item = array('op' => ($rule->isNegative ? '~' : '') . $op, 'msg' => $rules->formatMessage($rule, FALSE));
534: 
535:             } elseif ($rule->type === Rule::CONDITION) {
536:                 $item = array(
537:                     'op' => ($rule->isNegative ? '~' : '') . $op,
538:                     'rules' => static::exportRules($rule->subRules),
539:                     'control' => $rule->control->getHtmlName()
540:                 );
541:                 if ($rule->subRules->getToggles()) {
542:                     $item['toggle'] = $rule->subRules->getToggles();
543:                 }
544:             }
545: 
546:             if (is_array($rule->arg)) {
547:                 foreach ($rule->arg as $key => $value) {
548:                     $item['arg'][$key] = $value instanceof IControl ? array('control' => $value->getHtmlName()) : $value;
549:                 }
550:             } elseif ($rule->arg !== NULL) {
551:                 $item['arg'] = $rule->arg instanceof IControl ? array('control' => $rule->arg->getHtmlName()) : $rule->arg;
552:             }
553: 
554:             $payload[] = $item;
555:         }
556:         return $payload;
557:     }
558: 
559: 
560:     /********************* validators ****************d*g**/
561: 
562: 
563:     /**
564:      * Is control's value equal with second parameter?
565:      * @return bool
566:      * @internal
567:      */
568:     public static function validateEqual(IControl $control, $arg)
569:     {
570:         $value = $control->getValue();
571:         foreach ((is_array($value) ? $value : array($value)) as $val) {
572:             foreach ((is_array($arg) ? $arg : array($arg)) as $item) {
573:                 if ((string) $val === (string) $item) {
574:                     continue 2;
575:                 }
576:             }
577:             return FALSE;
578:         }
579:         return TRUE;
580:     }
581: 
582: 
583:     /**
584:      * Is control's value not equal with second parameter?
585:      * @return bool
586:      * @internal
587:      */
588:     public static function validateNotEqual(IControl $control, $arg)
589:     {
590:         return !static::validateEqual($control, $arg);
591:     }
592: 
593: 
594:     /**
595:      * Is control filled?
596:      * @return bool
597:      * @internal
598:      */
599:     public static function validateFilled(IControl $control)
600:     {
601:         return $control->isFilled();
602:     }
603: 
604: 
605:     /**
606:      * Is control not filled?
607:      * @return bool
608:      * @internal
609:      */
610:     public static function validateBlank(IControl $control)
611:     {
612:         return !$control->isFilled();
613:     }
614: 
615: 
616:     /**
617:      * Is control valid?
618:      * @return bool
619:      * @internal
620:      */
621:     public static function validateValid(IControl $control)
622:     {
623:         return $control->rules->validate();
624:     }
625: 
626: 
627:     /**
628:      * Is a control's value number in specified range?
629:      * @param  Nette\Forms\IControl
630:      * @param  array  min and max value pair
631:      * @return bool
632:      * @internal
633:      */
634:     public static function validateRange(IControl $control, $range)
635:     {
636:         return Nette\Utils\Validators::isInRange($control->getValue(), $range);
637:     }
638: 
639: 
640:     /**
641:      * Count/length validator. Range is array, min and max length pair.
642:      * @return bool
643:      * @internal
644:      */
645:     public static function validateLength(IControl $control, $range)
646:     {
647:         if (!is_array($range)) {
648:             $range = array($range, $range);
649:         }
650:         $value = $control->getValue();
651:         return Nette\Utils\Validators::isInRange(is_array($value) ? count($value) : Nette\Utils\Strings::length($value), $range);
652:     }
653: 
654: 
655:     /**
656:      * Has control's value minimal count/length?
657:      * @return bool
658:      * @internal
659:      */
660:     public static function validateMinLength(IControl $control, $length)
661:     {
662:         return static::validateLength($control, array($length, NULL));
663:     }
664: 
665: 
666:     /**
667:      * Is control's value count/length in limit?
668:      * @return bool
669:      * @internal
670:      */
671:     public static function validateMaxLength(IControl $control, $length)
672:     {
673:         return static::validateLength($control, array(NULL, $length));
674:     }
675: 
676: 
677:     /********************* user data ****************d*g**/
678: 
679: 
680:     /**
681:      * Sets user-specific option.
682:      * @return self
683:      */
684:     public function setOption($key, $value)
685:     {
686:         if ($value === NULL) {
687:             unset($this->options[$key]);
688:         } else {
689:             $this->options[$key] = $value;
690:         }
691:         return $this;
692:     }
693: 
694: 
695:     /**
696:      * Returns user-specific option.
697:      * @return mixed
698:      */
699:     public function getOption($key, $default = NULL)
700:     {
701:         return isset($this->options[$key]) ? $this->options[$key] : $default;
702:     }
703: 
704: 
705:     /**
706:      * Returns user-specific options.
707:      * @return array
708:      */
709:     public function getOptions()
710:     {
711:         return $this->options;
712:     }
713: 
714: }
715: 
Nette 2.1 API documentation generated by ApiGen 2.8.0