3 * Select dropdown field, with an additional "other" textbox.
5 class HTMLSelectOrOtherField
extends HTMLTextField
{
7 function __construct( $params ) {
8 if ( ! in_array( 'other', $params[ 'options' ], true ) ) {
9 $msg = isset( $params[ 'other' ] ) ?
$params[ 'other' ] : wfMessage( 'htmlform-selectorother-other' )->text();
10 $params[ 'options' ][ $msg ] = 'other';
13 parent
::__construct( $params );
16 static function forceToStringRecursive( $array ) {
17 if ( is_array( $array ) ) {
18 return array_map( array( __CLASS__
, 'forceToStringRecursive' ), $array );
20 return strval( $array );
24 function getInputHTML( $value ) {
27 if ( $value !== false ) {
28 $valInSelect = in_array( $value, HTMLFormField
::flattenOptions( $this->mParams
[ 'options' ] ) );
31 $selected = $valInSelect ?
$value : 'other';
33 $opts = self
::forceToStringRecursive( $this->mParams
[ 'options' ] );
35 $select = new XmlSelect( $this->mName
, $this->mID
, $selected );
36 $select->addOptions( $opts );
38 $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
40 $tbAttribs = array( 'id' => $this->mID
. '-other', 'size' => $this->getSize() );
42 if ( ! empty( $this->mParams
[ 'disabled' ] ) ) {
43 $select->setAttribute( 'disabled', 'disabled' );
44 $tbAttribs[ 'disabled' ] = 'disabled';
47 $select = $select->getHTML();
49 if ( isset( $this->mParams
[ 'maxlength' ] ) ) {
50 $tbAttribs[ 'maxlength' ] = $this->mParams
[ 'maxlength' ];
53 if ( $this->mClass
!== '' ) {
54 $tbAttribs[ 'class' ] = $this->mClass
;
57 $textbox = Html
::input( $this->mName
. '-other', $valInSelect ?
'' : $value, 'text', $tbAttribs );
59 return "$select<br />\n$textbox";
63 * @param $request WebRequest
67 function loadDataFromRequest( $request ) {
68 if ( $request->getCheck( $this->mName
) ) {
69 $val = $request->getText( $this->mName
);
71 if ( $val == 'other' ) {
72 $val = $request->getText( $this->mName
. '-other' );
77 return $this->getDefault();