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