PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Nette/Forms/Controls/TextInput.php

https://github.com/DocX/nette
PHP | 112 lines | 44 code | 25 blank | 43 comment | 6 complexity | 6960446af8100838f834915d85ded97c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  6. *
  7. * This source file is subject to the "Nette license" that is bundled
  8. * with this package in the file license.txt.
  9. *
  10. * For more information please see http://nettephp.com
  11. *
  12. * @copyright Copyright (c) 2004, 2009 David Grudl
  13. * @license http://nettephp.com/license Nette license
  14. * @link http://nettephp.com
  15. * @category Nette
  16. * @package Nette\Forms
  17. */
  18. /*namespace Nette\Forms;*/
  19. require_once dirname(__FILE__) . '/../../Forms/Controls/TextBase.php';
  20. /**
  21. * Single line text input control.
  22. *
  23. * @author David Grudl
  24. * @copyright Copyright (c) 2004, 2009 David Grudl
  25. * @package Nette\Forms
  26. */
  27. class TextInput extends TextBase
  28. {
  29. /**
  30. * @param string control name
  31. * @param string label
  32. * @param int width of the control
  33. * @param int maximum number of characters the user may enter
  34. */
  35. public function __construct($label = NULL, $cols = NULL, $maxLength = NULL)
  36. {
  37. parent::__construct($label);
  38. $this->control->type = 'text';
  39. $this->control->size = $cols;
  40. $this->control->maxlength = $maxLength;
  41. $this->filters[] = array(/*Nette\*/'String', 'trim');
  42. $this->filters[] = array($this, 'checkMaxLength');
  43. $this->value = '';
  44. }
  45. /**
  46. * Filter: shortens value to control's max length.
  47. * @return string
  48. */
  49. protected function checkMaxLength($value)
  50. {
  51. if ($this->control->maxlength && iconv_strlen($value, 'UTF-8') > $this->control->maxlength) {
  52. $value = iconv_substr($value, 0, $this->control->maxlength, 'UTF-8');
  53. }
  54. return $value;
  55. }
  56. /**
  57. * Sets or unsets the password mode.
  58. * @param bool
  59. * @return TextInput provides a fluent interface
  60. */
  61. public function setPasswordMode($mode = TRUE)
  62. {
  63. $this->control->type = $mode ? 'password' : 'text';
  64. return $this;
  65. }
  66. /**
  67. * Generates control's HTML element.
  68. * @return Nette\Web\Html
  69. */
  70. public function getControl()
  71. {
  72. $control = parent::getControl();
  73. if ($this->control->type !== 'password') {
  74. $control->value = $this->getValue() === '' ? $this->translate($this->emptyValue) : $this->value;
  75. }
  76. return $control;
  77. }
  78. public function notifyRule(Rule $rule)
  79. {
  80. if (is_string($rule->operation) && strcasecmp($rule->operation, ':length') === 0) {
  81. $this->control->maxlength = is_array($rule->arg) ? $rule->arg[1] : $rule->arg;
  82. } elseif (is_string($rule->operation) && strcasecmp($rule->operation, ':maxLength') === 0) {
  83. $this->control->maxlength = $rule->arg;
  84. }
  85. parent::notifyRule($rule);
  86. }
  87. }