CMaskedTextField.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * CMaskedTextField class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CMaskedTextField generates a masked text field.
  12. *
  13. * CMaskedTextField is similar to {@link CHtml::textField} except that
  14. * an input mask will be used to help users enter properly formatted data.
  15. * The masked text field is implemented based on the jQuery masked input plugin
  16. * (see {@link http://digitalbush.com/projects/masked-input-plugin}).
  17. *
  18. * @author Qiang Xue <qiang.xue@gmail.com>
  19. * @package system.web.widgets
  20. * @since 1.0
  21. */
  22. class CMaskedTextField extends CInputWidget
  23. {
  24. /**
  25. * @var string the input mask (e.g. '99/99/9999' for date input). The following characters are predefined:
  26. * <ul>
  27. * <li>a: represents an alpha character (A-Z,a-z).</li>
  28. * <li>9: represents a numeric character (0-9).</li>
  29. * <li>*: represents an alphanumeric character (A-Z,a-z,0-9).</li>
  30. * <li>?: anything listed after '?' within the mask is considered optional user input.</li>
  31. * </ul>
  32. * Additional characters can be defined by specifying the {@link charMap} property.
  33. */
  34. public $mask;
  35. /**
  36. * @var array the mapping between mask characters and the corresponding patterns.
  37. * For example, array('~'=>'[+-]') specifies that the '~' character expects '+' or '-' input.
  38. * Defaults to null, meaning using the map as described in {@link mask}.
  39. */
  40. public $charMap;
  41. /**
  42. * @var string the character prompting for user input. Defaults to underscore '_'.
  43. */
  44. public $placeholder;
  45. /**
  46. * @var string a JavaScript function callback that will be invoked when user finishes the input.
  47. */
  48. public $completed;
  49. /**
  50. * Executes the widget.
  51. * This method registers all needed client scripts and renders
  52. * the text field.
  53. */
  54. public function run()
  55. {
  56. if($this->mask=='')
  57. throw new CException(Yii::t('yii','Property CMaskedTextField.mask cannot be empty.'));
  58. list($name,$id)=$this->resolveNameID();
  59. if(isset($this->htmlOptions['id']))
  60. $id=$this->htmlOptions['id'];
  61. else
  62. $this->htmlOptions['id']=$id;
  63. if(isset($this->htmlOptions['name']))
  64. $name=$this->htmlOptions['name'];
  65. $this->registerClientScript();
  66. if($this->hasModel())
  67. echo CHtml::activeTextField($this->model,$this->attribute,$this->htmlOptions);
  68. else
  69. echo CHtml::textField($name,$this->value,$this->htmlOptions);
  70. }
  71. /**
  72. * Registers the needed CSS and JavaScript.
  73. */
  74. public function registerClientScript()
  75. {
  76. $id=$this->htmlOptions['id'];
  77. $miOptions=$this->getClientOptions();
  78. $options=$miOptions!==array() ? ','.CJavaScript::encode($miOptions) : '';
  79. $js='';
  80. if(is_array($this->charMap))
  81. $js.='jQuery.mask.definitions='.CJavaScript::encode($this->charMap).";\n";
  82. $js.="jQuery(\"#{$id}\").mask(\"{$this->mask}\"{$options});";
  83. $cs=Yii::app()->getClientScript();
  84. $cs->registerCoreScript('maskedinput');
  85. $cs->registerScript('Yii.CMaskedTextField#'.$id,$js);
  86. }
  87. /**
  88. * @return array the options for the text field
  89. */
  90. protected function getClientOptions()
  91. {
  92. $options=array();
  93. if($this->placeholder!==null)
  94. $options['placeholder']=$this->placeholder;
  95. if($this->completed!==null)
  96. {
  97. if($this->completed instanceof CJavaScriptExpression)
  98. $options['completed']=$this->completed;
  99. else
  100. $options['completed']=new CJavaScriptExpression($this->completed);
  101. }
  102. return $options;
  103. }
  104. }