4
* This file is part of the Nette Framework (http://nette.org)
6
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
8
* For the full copyright and license information, please view
9
* the file license.txt that was distributed with this source code.
12
namespace Nette\Forms\Controls;
22
* Base class that implements the basic functionality common to form controls.
26
* @property-read Nette\Forms\Form $form
27
* @property-read string $htmlName
28
* @property string $htmlId
29
* @property-read array $options
30
* @property Nette\Localization\ITranslator|NULL $translator
31
* @property mixed $value
32
* @property-read bool $filled
33
* @property-write $defaultValue
34
* @property bool $disabled
35
* @property bool $omitted
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
44
abstract class BaseControl extends Nette\ComponentModel\Component implements IControl
47
public static $idMask = 'frm-%s';
49
/** @var string textual caption or label */
52
/** @var mixed current control value */
55
/** @var Nette\Utils\Html control element template */
58
/** @var Nette\Utils\Html label element template */
62
private $errors = array();
65
protected $disabled = FALSE;
68
private $omitted = FALSE;
70
/** @var Nette\Forms\Rules */
73
/** @var Nette\Localization\ITranslator */
74
private $translator = TRUE; // means autodetect
76
/** @var array user options */
77
private $options = array();
81
* @param string caption
83
public function __construct($caption = NULL)
85
$this->monitor('Nette\Forms\Form');
86
parent::__construct();
87
$this->control = Html::el('input', array('type' => NULL, 'name' => NULL));
88
$this->label = Html::el('label');
89
$this->caption = $caption;
90
$this->rules = new Nette\Forms\Rules($this);
91
$this->setValue(NULL);
96
* This method will be called when the component becomes attached to Form.
97
* @param Nette\ComponentModel\IComponent
100
protected function attached($form)
102
if (!$this->isDisabled() && $form instanceof Form && $form->isAnchored() && $form->isSubmitted()) {
103
$this->loadHttpData();
110
* @param bool throw exception if form doesn't exist?
111
* @return Nette\Forms\Form
113
public function getForm($need = TRUE)
115
return $this->lookup('Nette\Forms\Form', $need);
123
public function loadHttpData()
125
$this->setValue($this->getHttpData());
133
public function getHttpData($type = Form::DATA_TEXT, $htmlTail = NULL)
135
return $this->getForm()->getHttpData($this->getHtmlName() . $htmlTail, $type);
140
* Returns HTML name of control.
143
public function getHtmlName()
145
return Nette\Forms\Helpers::generateHtmlName($this->lookupPath('Nette\Forms\Form'));
149
/********************* interface IFormControl ****************d*g**/
153
* Sets control's value.
156
public function setValue($value)
158
$this->value = $value;
164
* Returns control's value.
167
public function getValue()
177
public function isFilled()
179
$value = $this->getValue();
180
return $value !== NULL && $value !== array() && $value !== '';
185
* Sets control's default value.
188
public function setDefaultValue($value)
190
$form = $this->getForm(FALSE);
191
if ($this->isDisabled() || !$form || !$form->isAnchored() || !$form->isSubmitted()) {
192
$this->setValue($value);
199
* Disables or enables control.
203
public function setDisabled($value = TRUE)
205
if ($this->disabled = (bool) $value) {
206
$this->omitted = TRUE;
207
$this->setValue(NULL);
214
* Is control disabled?
217
public function isDisabled()
219
return $this->disabled === TRUE;
224
* Sets whether control value is excluded from $form->getValues() result.
228
public function setOmitted($value = TRUE)
230
$this->omitted = (bool) $value;
236
* Is control value excluded from $form->getValues() result?
239
public function isOmitted()
241
return $this->omitted;
245
/********************* rendering ****************d*g**/
249
* Generates control's HTML element.
250
* @return Nette\Utils\Html
252
public function getControl()
254
$this->setOption('rendered', TRUE);
256
$rules = self::exportRules($this->rules);
257
$el = clone $this->control;
258
return $el->addAttributes(array(
259
'name' => $this->getHtmlName(),
260
'id' => $this->getHtmlId(),
261
'required' => $this->isRequired(),
262
'disabled' => $this->isDisabled(),
263
))->data('nette-rules', $rules ? Nette\Utils\Json::encode($rules) : NULL);
268
* Generates label's HTML element.
270
* @return Nette\Utils\Html
272
public function getLabel($caption = NULL)
274
$label = clone $this->label;
275
$label->for = $this->getHtmlId();
276
$label->setText($this->translate($caption === NULL ? $this->caption : $caption));
282
* Returns control's HTML element template.
283
* @return Nette\Utils\Html
285
final public function getControlPrototype()
287
return $this->control;
292
* Returns label's HTML element template.
293
* @return Nette\Utils\Html
295
final public function getLabelPrototype()
302
* Changes control's HTML id.
303
* @param string new ID, or FALSE or NULL
306
public function setHtmlId($id)
308
$this->control->id = $id;
314
* Returns control's HTML id.
317
public function getHtmlId()
319
if (!isset($this->control->id)) {
320
$this->control->id = sprintf(self::$idMask, $this->lookupPath(NULL));
322
return $this->control->id;
327
* Changes control's HTML attribute.
332
public function setAttribute($name, $value = TRUE)
334
$this->control->$name = $value;
339
/********************* translator ****************d*g**/
343
* Sets translate adapter.
346
public function setTranslator(Nette\Localization\ITranslator $translator = NULL)
348
$this->translator = $translator;
354
* Returns translate adapter.
355
* @return Nette\Localization\ITranslator|NULL
357
final public function getTranslator()
359
if ($this->translator === TRUE) {
360
return $this->getForm(FALSE) ? $this->getForm()->getTranslator() : NULL;
362
return $this->translator;
367
* Returns translated string.
369
* @param int plural count
372
public function translate($s, $count = NULL)
374
$translator = $this->getTranslator();
375
return $translator === NULL || $s == NULL || $s instanceof Html // intentionally ==
377
: $translator->translate((string) $s, $count);
381
/********************* rules ****************d*g**/
385
* Adds a validation rule.
386
* @param mixed rule type
387
* @param string message to display for invalid data
388
* @param mixed optional rule arguments
391
public function addRule($operation, $message = NULL, $arg = NULL)
393
$this->rules->addRule($operation, $message, $arg);
399
* Adds a validation condition a returns new branch.
400
* @param mixed condition type
401
* @param mixed optional condition arguments
402
* @return Nette\Forms\Rules new branch
404
public function addCondition($operation, $value = NULL)
406
return $this->rules->addCondition($operation, $value);
411
* Adds a validation condition based on another control a returns new branch.
412
* @param Nette\Forms\IControl form control
413
* @param mixed condition type
414
* @param mixed optional condition arguments
415
* @return Nette\Forms\Rules new branch
417
public function addConditionOn(IControl $control, $operation, $value = NULL)
419
return $this->rules->addConditionOn($control, $operation, $value);
424
* @return Nette\Forms\Rules
426
final public function getRules()
433
* Makes control mandatory.
434
* @param mixed state or error message
437
public function setRequired($value = TRUE)
439
$this->rules->setRequired($value);
445
* Is control mandatory?
448
final public function isRequired()
450
return $this->rules->isRequired();
455
* Performs the server side validation.
458
public function validate()
460
if ($this->isDisabled()) {
463
$this->cleanErrors();
464
$this->rules->validate();
469
* Adds error message to the list.
470
* @param string error message
473
public function addError($message)
475
$this->errors[] = $message;
480
* Returns errors corresponding to control.
483
public function getError()
485
return $this->errors ? implode(' ', array_unique($this->errors)) : NULL;
490
* Returns errors corresponding to control.
493
public function getErrors()
495
return array_unique($this->errors);
502
public function hasErrors()
504
return (bool) $this->errors;
511
public function cleanErrors()
513
$this->errors = array();
520
protected static function exportRules($rules)
523
foreach ($rules as $rule) {
524
if (!is_string($op = $rule->operation)) {
525
if (!Nette\Utils\Callback::isStatic($op)) {
528
$op = Nette\Utils\Callback::toString($op);
530
if ($rule->type === Rule::VALIDATOR) {
531
$item = array('op' => ($rule->isNegative ? '~' : '') . $op, 'msg' => $rules->formatMessage($rule, FALSE));
533
} elseif ($rule->type === Rule::CONDITION) {
535
'op' => ($rule->isNegative ? '~' : '') . $op,
536
'rules' => static::exportRules($rule->subRules),
537
'control' => $rule->control->getHtmlName()
539
if ($rule->subRules->getToggles()) {
540
$item['toggle'] = $rule->subRules->getToggles();
544
if (is_array($rule->arg)) {
545
foreach ($rule->arg as $key => $value) {
546
$item['arg'][$key] = $value instanceof IControl ? array('control' => $value->getHtmlName()) : $value;
548
} elseif ($rule->arg !== NULL) {
549
$item['arg'] = $rule->arg instanceof IControl ? array('control' => $rule->arg->getHtmlName()) : $rule->arg;
558
/********************* validators ****************d*g**/
562
* Equal validator: are control's value and second parameter equal?
565
public static function validateEqual(IControl $control, $arg)
567
$value = $control->getValue();
568
foreach ((is_array($value) ? $value : array($value)) as $val) {
569
foreach ((is_array($arg) ? $arg : array($arg)) as $item) {
570
if ((string) $val === (string) $item) {
580
* Is control's value not equal with second parameter?
583
public static function validateNotEqual(IControl $control, $arg)
585
return !static::validateEqual($control, $arg);
590
* Filled validator: is control filled?
593
public static function validateFilled(IControl $control)
595
return $control->isFilled();
600
* Is control not filled?
603
public static function validateBlank(IControl $control)
605
return !$control->isFilled();
610
* Valid validator: is control valid?
613
public static function validateValid(IControl $control)
615
return $control->rules->validate();
620
* Rangle validator: is a control's value number in specified range?
621
* @param Nette\Forms\IControl
622
* @param array min and max value pair
625
public static function validateRange(IControl $control, $range)
627
return Nette\Utils\Validators::isInRange($control->getValue(), $range);
632
* Count/length validator. Range is array, min and max length pair.
635
public static function validateLength(IControl $control, $range)
637
if (!is_array($range)) {
638
$range = array($range, $range);
640
$value = $control->getValue();
641
return Nette\Utils\Validators::isInRange(is_array($value) ? count($value) : Nette\Utils\Strings::length($value), $range);
646
* Min-length validator: has control's value minimal count/length?
649
public static function validateMinLength(IControl $control, $length)
651
return static::validateLength($control, array($length, NULL));
656
* Max-length validator: is control's value count/length in limit?
659
public static function validateMaxLength(IControl $control, $length)
661
return static::validateLength($control, array(NULL, $length));
665
/********************* user data ****************d*g**/
669
* Sets user-specific option.
672
public function setOption($key, $value)
674
if ($value === NULL) {
675
unset($this->options[$key]);
677
$this->options[$key] = $value;
684
* Returns user-specific option.
687
final public function getOption($key, $default = NULL)
689
return isset($this->options[$key]) ? $this->options[$key] : $default;
694
* Returns user-specific options.
697
final public function getOptions()
699
return $this->options;