wiki.techinc.nl/includes/htmlform/HTMLSelectField.php
Alexandre Emsenhuber cb9a9a7b47 Add support for "tabindex" in HTMLFormField subclasses
It is already set for some fields in Special:Block, but are
discarded by HTMLForm and its fields.

Some notes:
- fields with multiple inputs (radio, select and other, select
  or other) will have the same tabindex set on all elements
- Some items such as multi-select and check matrix are not yet
  implemented

Change-Id: I3e1ba7f16f3a3183f231afcf60dd392ce6d6eb6b
2014-01-25 04:31:01 +00:00

52 lines
1.3 KiB
PHP

<?php
/**
* A select dropdown field. Basically a wrapper for Xmlselect class
*/
class HTMLSelectField extends HTMLFormField {
function validate( $value, $alldata ) {
$p = parent::validate( $value, $alldata );
if ( $p !== true ) {
return $p;
}
$validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] );
if ( in_array( $value, $validOptions ) ) {
return true;
} else {
return $this->msg( 'htmlform-select-badoption' )->parse();
}
}
function getInputHTML( $value ) {
$select = new XmlSelect( $this->mName, $this->mID, strval( $value ) );
# If one of the options' 'name' is int(0), it is automatically selected.
# because PHP sucks and thinks int(0) == 'some string'.
# Working around this by forcing all of them to strings.
foreach ( $this->mParams['options'] as &$opt ) {
if ( is_int( $opt ) ) {
$opt = strval( $opt );
}
}
unset( $opt ); # PHP keeps $opt around as a reference, which is a bit scary
if ( !empty( $this->mParams['disabled'] ) ) {
$select->setAttribute( 'disabled', 'disabled' );
}
if ( isset( $this->mParams['tabindex'] ) ) {
$select->setAttribute( 'tabindex', $this->mParams['tabindex'] );
}
if ( $this->mClass !== '' ) {
$select->setAttribute( 'class', $this->mClass );
}
$select->addOptions( $this->mParams['options'] );
return $select->getHTML();
}
}