Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

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