Allow HTMLFormField placeholder to be a message
[lhc/web/wiklou.git] / includes / htmlform / HTMLTextField.php
1 <?php
2
3 class HTMLTextField extends HTMLFormField {
4 protected $mPlaceholder = '';
5
6 /**
7 * @param array $params
8 * - type: HTML textfield type
9 * - size: field size in characters (defaults to 45)
10 * - placeholder/placeholder-message: set HTML placeholder attribute
11 * - spellcheck: set HTML spellcheck attribute
12 * - persistent: upon unsuccessful requests, retain the value (defaults to true, except
13 * for password fields)
14 */
15 public function __construct( $params ) {
16 parent::__construct( $params );
17
18 if ( isset( $params['placeholder-message'] ) ) {
19 $this->mPlaceholder = $this->getMessage( $params['placeholder-message'] )->parse();
20 } elseif ( isset( $params['placeholder'] ) ) {
21 $this->mPlaceholder = $params['placeholder'];
22 }
23 }
24
25 function getSize() {
26 return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 45;
27 }
28
29 function getSpellCheck() {
30 $val = isset( $this->mParams['spellcheck'] ) ? $this->mParams['spellcheck'] : null;
31 if ( is_bool( $val ) ) {
32 // "spellcheck" attribute literally requires "true" or "false" to work.
33 return $val === true ? 'true' : 'false';
34 }
35 return null;
36 }
37
38 function getInputHTML( $value ) {
39 $attribs = [
40 'id' => $this->mID,
41 'name' => $this->mName,
42 'size' => $this->getSize(),
43 'value' => $value,
44 'dir' => $this->mDir,
45 'spellcheck' => $this->getSpellCheck(),
46 ] + $this->getTooltipAndAccessKey() + $this->getDataAttribs();
47
48 if ( $this->mClass !== '' ) {
49 $attribs['class'] = $this->mClass;
50 }
51 if ( $this->mPlaceholder !== '' ) {
52 $attribs['placeholder'] = $this->mPlaceholder;
53 }
54
55 # @todo Enforce pattern, step, required, readonly on the server side as
56 # well
57 $allowedParams = [
58 'type',
59 'min',
60 'max',
61 'pattern',
62 'title',
63 'step',
64 'list',
65 'maxlength',
66 'tabindex',
67 'disabled',
68 'required',
69 'autofocus',
70 'multiple',
71 'readonly'
72 ];
73
74 $attribs += $this->getAttributes( $allowedParams );
75
76 # Extract 'type'
77 $type = $this->getType( $attribs );
78 return Html::input( $this->mName, $value, $type, $attribs );
79 }
80
81 protected function getType( &$attribs ) {
82 $type = isset( $attribs['type'] ) ? $attribs['type'] : 'text';
83 unset( $attribs['type'] );
84
85 # Implement tiny differences between some field variants
86 # here, rather than creating a new class for each one which
87 # is essentially just a clone of this one.
88 if ( isset( $this->mParams['type'] ) ) {
89 switch ( $this->mParams['type'] ) {
90 case 'int':
91 $type = 'number';
92 break;
93 case 'float':
94 $type = 'number';
95 $attribs['step'] = 'any';
96 break;
97 # Pass through
98 case 'email':
99 case 'password':
100 case 'file':
101 case 'url':
102 $type = $this->mParams['type'];
103 break;
104 }
105 }
106
107 return $type;
108 }
109
110 function getInputOOUI( $value ) {
111 $attribs = $this->getTooltipAndAccessKey();
112
113 if ( $this->mClass !== '' ) {
114 $attribs['classes'] = [ $this->mClass ];
115 }
116 if ( $this->mPlaceholder !== '' ) {
117 $attribs['placeholder'] = $this->mPlaceholder;
118 }
119
120 # @todo Enforce pattern, step, required, readonly on the server side as
121 # well
122 $allowedParams = [
123 'autofocus',
124 'autosize',
125 'disabled',
126 'flags',
127 'indicator',
128 'maxlength',
129 'readonly',
130 'required',
131 'tabindex',
132 'type',
133 ];
134
135 $attribs += OOUI\Element::configFromHtmlAttributes(
136 $this->getAttributes( $allowedParams )
137 );
138
139 $type = $this->getType( $attribs );
140
141 return $this->getInputWidget( [
142 'id' => $this->mID,
143 'name' => $this->mName,
144 'value' => $value,
145 'type' => $type,
146 ] + $attribs );
147 }
148
149 protected function getInputWidget( $params ) {
150 return new OOUI\TextInputWidget( $params );
151 }
152
153 /**
154 * Returns an array of data-* attributes to add to the field.
155 *
156 * @return array
157 */
158 protected function getDataAttribs() {
159 return [];
160 }
161 }