MaskedInput.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\widgets;
  8. use yii\base\InvalidConfigException;
  9. use yii\helpers\Html;
  10. use yii\helpers\Json;
  11. use yii\web\JsExpression;
  12. /**
  13. * MaskedInput generates a masked text input.
  14. *
  15. * MaskedInput is similar to [[Html::textInput()]] except that
  16. * an input mask will be used to force users to enter properly formatted data,
  17. * such as phone numbers, social security numbers.
  18. *
  19. * To use MaskedInput, you must set the [[mask]] property. The following example
  20. * shows how to use MaskedInput to collect phone numbers:
  21. *
  22. * ~~~
  23. * echo MaskedInput::widget([
  24. * 'name' => 'phone',
  25. * 'mask' => '999-999-9999',
  26. * ]);
  27. * ~~~
  28. *
  29. * The masked text field is implemented based on the [jQuery masked input plugin](http://digitalbush.com/projects/masked-input-plugin).
  30. *
  31. * @author Qiang Xue <[email protected]>
  32. * @since 2.0
  33. */
  34. class MaskedInput extends InputWidget
  35. {
  36. /**
  37. * @var string the input mask (e.g. '99/99/9999' for date input). The following characters are predefined:
  38. *
  39. * - `a`: represents an alpha character (A-Z, a-z)
  40. * - `9`: represents a numeric character (0-9)
  41. * - `*`: represents an alphanumeric character (A-Z, a-z, 0-9)
  42. * - `?`: anything listed after '?' within the mask is considered optional user input
  43. *
  44. * Additional characters can be defined by specifying the [[charMap]] property.
  45. */
  46. public $mask;
  47. /**
  48. * @var array the mapping between mask characters and the corresponding patterns.
  49. * For example, `['~' => '[+-]']` specifies that the '~' character expects '+' or '-' input.
  50. * Defaults to null, meaning using the map as described in [[mask]].
  51. */
  52. public $charMap;
  53. /**
  54. * @var string the character prompting for user input. Defaults to underscore '_'.
  55. */
  56. public $placeholder;
  57. /**
  58. * @var string a JavaScript function callback that will be invoked when user finishes the input.
  59. */
  60. public $completed;
  61. /**
  62. * Initializes the widget.
  63. * @throws InvalidConfigException if the "mask" property is not set.
  64. */
  65. public function init()
  66. {
  67. parent::init();
  68. if (empty($this->mask)) {
  69. throw new InvalidConfigException('The "mask" property must be set.');
  70. }
  71. }
  72. /**
  73. * Runs the widget.
  74. */
  75. public function run()
  76. {
  77. if ($this->hasModel()) {
  78. echo Html::activeTextInput($this->model, $this->attribute, $this->options);
  79. } else {
  80. echo Html::textInput($this->name, $this->value, $this->options);
  81. }
  82. $this->registerClientScript();
  83. }
  84. /**
  85. * Registers the needed JavaScript.
  86. */
  87. public function registerClientScript()
  88. {
  89. $options = $this->getClientOptions();
  90. $options = empty($options) ? '' : ',' . Json::encode($options);
  91. $js = '';
  92. if (is_array($this->charMap) && !empty($this->charMap)) {
  93. $js .= 'jQuery.mask.definitions=' . Json::encode($this->charMap) . ";\n";
  94. }
  95. $id = $this->options['id'];
  96. $js .= "jQuery(\"#{$id}\").mask(\"{$this->mask}\"{$options});";
  97. $view = $this->getView();
  98. MaskedInputAsset::register($view);
  99. $view->registerJs($js);
  100. }
  101. /**
  102. * @return array the options for the text field
  103. */
  104. protected function getClientOptions()
  105. {
  106. $options = [];
  107. if ($this->placeholder !== null) {
  108. $options['placeholder'] = $this->placeholder;
  109. }
  110. if ($this->completed !== null) {
  111. if ($this->completed instanceof JsExpression) {
  112. $options['completed'] = $this->completed;
  113. } else {
  114. $options['completed'] = new JsExpression($this->completed);
  115. }
  116. }
  117. return $options;
  118. }
  119. }