wiki.techinc.nl/includes/htmlform/HTMLRadioField.php
Brad Jorsch f88fcb7461 HTMLForm: Allow i18n of 'options'
One shortcoming in HTMLForm is that fields that use the 'options'
parameter (e.g. <select>) have no easy way for the individual labels to
be localized.

This change adds a new parameter type, 'options-messages', where the
keys are message keys rather than bare strings (similar to the
difference between 'label' and 'label-message'). It also abstracts out
the fetching of the various option parameters, and changes the necessary
field classes to use it.

Change-Id: If4175332405d26c7ff2e8fbe100bcad61762ce6f
2014-02-19 23:26:23 +00:00

71 lines
1.7 KiB
PHP

<?php
/**
* Radio checkbox fields.
*/
class HTMLRadioField extends HTMLFormField {
function validate( $value, $alldata ) {
$p = parent::validate( $value, $alldata );
if ( $p !== true ) {
return $p;
}
if ( !is_string( $value ) && !is_int( $value ) ) {
return false;
}
$validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
if ( in_array( $value, $validOptions ) ) {
return true;
} else {
return $this->msg( 'htmlform-select-badoption' )->parse();
}
}
/**
* This returns a block of all the radio options, in one cell.
* @see includes/HTMLFormField#getInputHTML()
*
* @param $value String
*
* @return String
*/
function getInputHTML( $value ) {
$html = $this->formatOptions( $this->getOptions(), $value );
return $html;
}
function formatOptions( $options, $value ) {
$html = '';
$attribs = $this->getAttributes( array( 'disabled', 'tabindex' ) );
$elementFunc = array( 'Html', $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' );
# @todo Should this produce an unordered list perhaps?
foreach ( $options as $label => $info ) {
if ( is_array( $info ) ) {
$html .= Html::rawElement( 'h1', array(), $label ) . "\n";
$html .= $this->formatOptions( $info, $value );
} else {
$id = Sanitizer::escapeId( $this->mID . "-$info" );
$radio = Xml::radio( $this->mName, $info, $info == $value, $attribs + array( 'id' => $id ) );
$radio .= '&#160;' . call_user_func( $elementFunc, 'label', array( 'for' => $id ), $label );
$html .= ' ' . Html::rawElement(
'div',
array( 'class' => 'mw-htmlform-flatlist-item' ),
$radio
);
}
}
return $html;
}
protected function needsLabel() {
return false;
}
}