Packages

  • 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

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