6 * Besides the parameters recognized by HTMLFormField, the following are
8 * autocomplete - HTML autocomplete value (a boolean for on/off or a string according to
9 * https://html.spec.whatwg.org/multipage/forms.html#autofill )
11 class HTMLTextField
extends HTMLFormField
{
12 protected $mPlaceholder = '';
14 /** @var bool HTML autocomplete attribute */
15 protected $autocomplete;
18 * @param array $params
19 * - type: HTML textfield type
20 * - size: field size in characters (defaults to 45)
21 * - placeholder/placeholder-message: set HTML placeholder attribute
22 * - spellcheck: set HTML spellcheck attribute
23 * - persistent: upon unsuccessful requests, retain the value (defaults to true, except
24 * for password fields)
26 public function __construct( $params ) {
27 if ( isset( $params['autocomplete'] ) && is_bool( $params['autocomplete'] ) ) {
28 $params['autocomplete'] = $params['autocomplete'] ?
'on' : 'off';
31 parent
::__construct( $params );
33 if ( isset( $params['placeholder-message'] ) ) {
34 $this->mPlaceholder
= $this->getMessage( $params['placeholder-message'] )->parse();
35 } elseif ( isset( $params['placeholder'] ) ) {
36 $this->mPlaceholder
= $params['placeholder'];
40 public function getSize() {
41 return isset( $this->mParams
['size'] ) ?
$this->mParams
['size'] : 45;
44 public function getSpellCheck() {
45 $val = isset( $this->mParams
['spellcheck'] ) ?
$this->mParams
['spellcheck'] : null;
46 if ( is_bool( $val ) ) {
47 // "spellcheck" attribute literally requires "true" or "false" to work.
48 return $val === true ?
'true' : 'false';
53 public function isPersistent() {
54 if ( isset( $this->mParams
['persistent'] ) ) {
55 return $this->mParams
['persistent'];
57 // don't put passwords into the HTML body, they could get cached or otherwise leaked
58 return !( isset( $this->mParams
['type'] ) && $this->mParams
['type'] === 'password' );
61 public function getInputHTML( $value ) {
62 if ( !$this->isPersistent() ) {
68 'name' => $this->mName
,
69 'size' => $this->getSize(),
72 'spellcheck' => $this->getSpellCheck(),
73 ] +
$this->getTooltipAndAccessKey() +
$this->getDataAttribs();
75 if ( $this->mClass
!== '' ) {
76 $attribs['class'] = $this->mClass
;
78 if ( $this->mPlaceholder
!== '' ) {
79 $attribs['placeholder'] = $this->mPlaceholder
;
82 # @todo Enforce pattern, step, required, readonly on the server side as
102 $attribs +
= $this->getAttributes( $allowedParams );
105 $type = $this->getType( $attribs );
106 return Html
::input( $this->mName
, $value, $type, $attribs );
109 protected function getType( &$attribs ) {
110 $type = isset( $attribs['type'] ) ?
$attribs['type'] : 'text';
111 unset( $attribs['type'] );
113 # Implement tiny differences between some field variants
114 # here, rather than creating a new class for each one which
115 # is essentially just a clone of this one.
116 if ( isset( $this->mParams
['type'] ) ) {
117 switch ( $this->mParams
['type'] ) {
123 $attribs['step'] = 'any';
130 $type = $this->mParams
['type'];
138 public function getInputOOUI( $value ) {
139 if ( !$this->isPersistent() ) {
143 $attribs = $this->getTooltipAndAccessKey();
145 if ( $this->mClass
!== '' ) {
146 $attribs['classes'] = [ $this->mClass
];
148 if ( $this->mPlaceholder
!== '' ) {
149 $attribs['placeholder'] = $this->mPlaceholder
;
152 # @todo Enforce pattern, step, required, readonly on the server side as
168 $attribs +
= OOUI\Element
::configFromHtmlAttributes(
169 $this->getAttributes( $allowedParams )
172 // FIXME T150983 downgrade autocomplete
173 if ( isset( $attribs['autocomplete'] ) ) {
174 if ( $attribs['autocomplete'] === 'on' ) {
175 $attribs['autocomplete'] = true;
176 } elseif ( $attribs['autocomplete'] === 'off' ) {
177 $attribs['autocomplete'] = false;
179 unset( $attribs['autocomplete'] );
183 $type = $this->getType( $attribs );
185 return $this->getInputWidget( [
187 'name' => $this->mName
,
190 'dir' => $this->mDir
,
194 protected function getInputWidget( $params ) {
195 return new OOUI\
TextInputWidget( $params );
199 * Returns an array of data-* attributes to add to the field.
203 protected function getDataAttribs() {