PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/www/libs/nette-dev/Forms/Controls/TextInput.php

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