wiki.techinc.nl/includes/htmlform/HTMLSelectField.php
Brad Jorsch 5f57d5d657 HTMLForm: Clean up 0 handling
PHP, particularly with in_array, really has problems with integer 0
versus non-numeric strings. Let's clean that up by converting values to
strings more agressively and using the $strict option to in_array.

Oddly enough, the one place where strict in_array was being used already
broke when If4175332 stringified the values in one array but not the
other.

Bug: 62268
Change-Id: Id34e654eb8d0e70d093b11445273e542e491e265
2014-03-05 15:40:48 -05:00

42 lines
994 B
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->getOptions() );
if ( in_array( strval( $value ), $validOptions, true ) ) {
return true;
} else {
return $this->msg( 'htmlform-select-badoption' )->parse();
}
}
function getInputHTML( $value ) {
$select = new XmlSelect( $this->mName, $this->mID, strval( $value ) );
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->getOptions() );
return $select->getHTML();
}
}