3 class HTMLTextField
extends HTMLFormField
{
4 protected $mPlaceholder = '';
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)
15 public function __construct( $params ) {
16 parent
::__construct( $params );
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'];
26 return isset( $this->mParams
['size'] ) ?
$this->mParams
['size'] : 45;
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';
38 public function isPersistent() {
39 if ( isset( $this->mParams
['persistent'] ) ) {
40 return $this->mParams
['persistent'];
42 // don't put passwords into the HTML body, they could get cached or otherwise leaked
43 return !( isset( $this->mParams
['type'] ) && $this->mParams
['type'] === 'password' );
46 function getInputHTML( $value ) {
47 if ( !$this->isPersistent() ) {
53 'name' => $this->mName
,
54 'size' => $this->getSize(),
57 'spellcheck' => $this->getSpellCheck(),
58 ] +
$this->getTooltipAndAccessKey() +
$this->getDataAttribs();
60 if ( $this->mClass
!== '' ) {
61 $attribs['class'] = $this->mClass
;
63 if ( $this->mPlaceholder
!== '' ) {
64 $attribs['placeholder'] = $this->mPlaceholder
;
67 # @todo Enforce pattern, step, required, readonly on the server side as
86 $attribs +
= $this->getAttributes( $allowedParams );
89 $type = $this->getType( $attribs );
90 return Html
::input( $this->mName
, $value, $type, $attribs );
93 protected function getType( &$attribs ) {
94 $type = isset( $attribs['type'] ) ?
$attribs['type'] : 'text';
95 unset( $attribs['type'] );
97 # Implement tiny differences between some field variants
98 # here, rather than creating a new class for each one which
99 # is essentially just a clone of this one.
100 if ( isset( $this->mParams
['type'] ) ) {
101 switch ( $this->mParams
['type'] ) {
107 $attribs['step'] = 'any';
114 $type = $this->mParams
['type'];
122 function getInputOOUI( $value ) {
123 if ( !$this->isPersistent() ) {
127 $attribs = $this->getTooltipAndAccessKey();
129 if ( $this->mClass
!== '' ) {
130 $attribs['classes'] = [ $this->mClass
];
132 if ( $this->mPlaceholder
!== '' ) {
133 $attribs['placeholder'] = $this->mPlaceholder
;
136 # @todo Enforce pattern, step, required, readonly on the server side as
151 $attribs +
= OOUI\Element
::configFromHtmlAttributes(
152 $this->getAttributes( $allowedParams )
155 $type = $this->getType( $attribs );
157 return $this->getInputWidget( [
159 'name' => $this->mName
,
165 protected function getInputWidget( $params ) {
166 return new OOUI\
TextInputWidget( $params );
170 * Returns an array of data-* attributes to add to the field.
174 protected function getDataAttribs() {