My favorites | Sign in
yii
Project Home Downloads Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* CMaskedTextField class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

/**
* CMaskedTextField generates a masked text field.
*
* CMaskedTextField is similar to {@link CHtml::textField} except that
* an input mask will be used to help users enter properly formatted data.
* The masked text field is implemented based on the jQuery masked input plugin
* (see {@link http://digitalbush.com/projects/masked-input-plugin}).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.web.widgets
* @since 1.0
*/
class CMaskedTextField extends CInputWidget
{
/**
* @var string the input mask (e.g. '99/99/9999' for date input). The following characters are predefined:
* <ul>
* <li>a: represents an alpha character (A-Z,a-z).</li>
* <li>9: represents a numeric character (0-9).</li>
* <li>*: represents an alphanumeric character (A-Z,a-z,0-9).</li>
* <li>?: anything listed after '?' within the mask is considered optional user input.</li>
* </ul>
* Additional characters can be defined by specifying the {@link charMap} property.
*/
public $mask;
/**
* @var array the mapping between mask characters and the corresponding patterns.
* For example, array('~'=>'[+-]') specifies that the '~' character expects '+' or '-' input.
* Defaults to null, meaning using the map as described in {@link mask}.
*/
public $charMap;
/**
* @var string the character prompting for user input. Defaults to underscore '_'.
*/
public $placeholder;
/**
* @var string a JavaScript function callback that will be invoked when user finishes the input.
*/
public $completed;

/**
* Executes the widget.
* This method registers all needed client scripts and renders
* the text field.
*/
public function run()
{
if($this->mask=='')
throw new CException(Yii::t('yii','Property CMaskedTextField.mask cannot be empty.'));

list($name,$id)=$this->resolveNameID();
if(isset($this->htmlOptions['id']))
$id=$this->htmlOptions['id'];
else
$this->htmlOptions['id']=$id;
if(isset($this->htmlOptions['name']))
$name=$this->htmlOptions['name'];

$this->registerClientScript();

if($this->hasModel())
echo CHtml::activeTextField($this->model,$this->attribute,$this->htmlOptions);
else
echo CHtml::textField($name,$this->value,$this->htmlOptions);
}

/**
* Registers the needed CSS and JavaScript.
*/
public function registerClientScript()
{
$id=$this->htmlOptions['id'];
$miOptions=$this->getClientOptions();
$options=$miOptions!==array() ? ','.CJavaScript::encode($miOptions) : '';
$js='';
if(is_array($this->charMap))
$js.='jQuery.mask.definitions='.CJavaScript::encode($this->charMap).";\n";
$js.="jQuery(\"#{$id}\").mask(\"{$this->mask}\"{$options});";

$cs=Yii::app()->getClientScript();
$cs->registerCoreScript('maskedinput');
$cs->registerScript('Yii.CMaskedTextField#'.$id,$js);
}

/**
* @return array the options for the text field
*/
protected function getClientOptions()
{
$options=array();
if($this->placeholder!==null)
$options['placeholder']=$this->placeholder;
if(is_string($this->completed))
{
if(strncmp($this->completed,'js:',3))
$options['completed']='js:'.$this->completed;
else
$options['completed']=$this->completed;
}
return $options;
}
}

Change log

r3527 by qiang.xue on Dec 31, 2011   Diff
1.1.9 release.
Go to: 
Sign in to write a code review

Older revisions

r3515 by mdomba on Dec 28, 2011   Diff
removed 1.0.x reference from
documentation
r2872 by qiang.xue on Jan 16, 2011   Diff
throw exception if
CMaskedTextField.mask is not set.
Otherwise, a mysterious js error may
occur.
r2799 by qiang.xue on Jan 1, 2011   Diff
changed copyright year.
All revisions of this file

File info

Size: 3466 bytes, 113 lines

File properties

svn:keywords
Id
Powered by Google Project Hosting