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