Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 115 lines (105 sloc) 3.501 kb
55bdd95 removed carriage returns from files.
qiang.xue authored
1 <?php
2 /**
3 * CMaskedTextField class file.
4 *
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link http://www.yiiframework.com/
4be0af6 changed copyright year.
qiang.xue authored
7 * @copyright Copyright &copy; 2008-2011 Yii Software LLC
55bdd95 removed carriage returns from files.
qiang.xue authored
8 * @license http://www.yiiframework.com/license/
9 */
10
11 /**
12 * CMaskedTextField generates a masked text field.
13 *
14 * CMaskedTextField is similar to {@link CHtml::textField} except that
15 * an input mask will be used to help users enter properly formatted data.
16 * The masked text field is implemented based on the jQuery masked input plugin
17 * (see {@link http://digitalbush.com/projects/masked-input-plugin}).
18 *
19 * @author Qiang Xue <qiang.xue@gmail.com>
20 * @version $Id$
21 * @package system.web.widgets
22 * @since 1.0
23 */
24 class CMaskedTextField extends CInputWidget
25 {
26 /**
27 * @var string the input mask (e.g. '99/99/9999' for date input). The following characters are predefined:
28 * <ul>
f981b43 CMaskedTextField's jquery.maskedinput updated to 1.2.2
alexander.makarow authored
29 * <li>a: represents an alpha character (A-Z,a-z).</li>
30 * <li>9: represents a numeric character (0-9).</li>
55bdd95 removed carriage returns from files.
qiang.xue authored
31 * <li>*: represents an alphanumeric character (A-Z,a-z,0-9).</li>
f981b43 CMaskedTextField's jquery.maskedinput updated to 1.2.2
alexander.makarow authored
32 * <li>?: anything listed after '?' within the mask is considered optional user input.</li>
55bdd95 removed carriage returns from files.
qiang.xue authored
33 * </ul>
34 * Additional characters can be defined by specifying the {@link charMap} property.
35 */
36 public $mask;
37 /**
38 * @var array the mapping between mask characters and the corresponding patterns.
39 * For example, array('~'=>'[+-]') specifies that the '~' character expects '+' or '-' input.
e73778b merge from 1.0 branch.
qiang.xue authored
40 * Defaults to null, meaning using the map as described in {@link mask}.
55bdd95 removed carriage returns from files.
qiang.xue authored
41 */
42 public $charMap;
43 /**
44 * @var string the character prompting for user input. Defaults to underscore '_'.
45 */
46 public $placeholder;
47 /**
48 * @var string a JavaScript function callback that will be invoked when user finishes the input.
49 */
50 public $completed;
51
52 /**
53 * Executes the widget.
54 * This method registers all needed client scripts and renders
55 * the text field.
56 */
57 public function run()
58 {
9bbfabc throw exception if CMaskedTextField.mask is not set. Otherwise, a mys…
qiang.xue authored
59 if($this->mask=='')
60 throw new CException(Yii::t('yii','Property CMaskedTextField.mask cannot be empty.'));
61
55bdd95 removed carriage returns from files.
qiang.xue authored
62 list($name,$id)=$this->resolveNameID();
b6d46ef fixed name and id resolution.
qiang.xue authored
63 if(isset($this->htmlOptions['id']))
64 $id=$this->htmlOptions['id'];
65 else
09ca298 merge from 1.0.
qiang.xue authored
66 $this->htmlOptions['id']=$id;
b6d46ef fixed name and id resolution.
qiang.xue authored
67 if(isset($this->htmlOptions['name']))
f506841 merge
alexander.makarow authored
68 $name=$this->htmlOptions['name'];
e9c5cce merge from 1.0.
qiang.xue authored
69
70 $this->registerClientScript();
71
72 if($this->hasModel())
73 echo CHtml::activeTextField($this->model,$this->attribute,$this->htmlOptions);
74 else
75 echo CHtml::textField($name,$this->value,$this->htmlOptions);
76 }
77
78 /**
79 * Registers the needed CSS and JavaScript.
80 */
81 public function registerClientScript()
82 {
83 $id=$this->htmlOptions['id'];
55bdd95 removed carriage returns from files.
qiang.xue authored
84 $miOptions=$this->getClientOptions();
85 $options=$miOptions!==array() ? ','.CJavaScript::encode($miOptions) : '';
86 $js='';
87 if(is_array($this->charMap))
e73778b merge from 1.0 branch.
qiang.xue authored
88 $js.='jQuery.mask.definitions='.CJavaScript::encode($this->charMap).";\n";
55bdd95 removed carriage returns from files.
qiang.xue authored
89 $js.="jQuery(\"#{$id}\").mask(\"{$this->mask}\"{$options});";
90
e9e08c4
qiang.xue authored
91 $cs=Yii::app()->getClientScript();
55bdd95 removed carriage returns from files.
qiang.xue authored
92 $cs->registerCoreScript('maskedinput');
ebdeb4d removed CClientScript::registerBodyScript and CClientScript::register…
qiang.xue authored
93 $cs->registerScript('Yii.CMaskedTextField#'.$id,$js);
55bdd95 removed carriage returns from files.
qiang.xue authored
94 }
95
96 /**
97 * @return array the options for the text field
98 */
99 protected function getClientOptions()
100 {
101 $options=array();
102 if($this->placeholder!==null)
103 $options['placeholder']=$this->placeholder;
4c4a6f4 @mdomba refactored JS checking, where checking for object inside if_string
mdomba authored
104
c89f31f @mdomba Unify CJavascriptExpression usage
mdomba authored
105 if($this->completed!==null)
106 {
5973d89 @mdomba refactored CJavascriptExpression->_construct to get cleaner code
mdomba authored
107 if($this->completed instanceof CJavaScriptExpression)
c89f31f @mdomba Unify CJavascriptExpression usage
mdomba authored
108 $options['completed']=$this->completed;
109 else
3fd13f9 @mdomba remove second parameter on calls
mdomba authored
110 $options['completed']=new CJavaScriptExpression($this->completed);
c89f31f @mdomba Unify CJavascriptExpression usage
mdomba authored
111 }
4c4a6f4 @mdomba refactored JS checking, where checking for object inside if_string
mdomba authored
112
55bdd95 removed carriage returns from files.
qiang.xue authored
113 return $options;
114 }
662798d
qiang.xue authored
115 }
Something went wrong with that request. Please try again.