Example usage in I2c3d6486e09b3e4d5537b95194e178fd73ac9792 (Special:PageLanguage). Trivial now that we have RadioSelectInputWidget in OOUI (added in fd2815e372f6a4beb4f4e5f2a7d9cbf785d40851). Depends on If68f04a2fa20e7c03763898ab80cf39b5e6cb182 in OOUI. Bug: T98855 Change-Id: I224e591e58534c07af62eebb9ae4b185225edc33
88 lines
2.3 KiB
PHP
88 lines
2.3 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( strval( $value ), $validOptions, true ) ) {
|
|
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 string $value
|
|
*
|
|
* @return string
|
|
*/
|
|
function getInputHTML( $value ) {
|
|
$html = $this->formatOptions( $this->getOptions(), strval( $value ) );
|
|
|
|
return $html;
|
|
}
|
|
|
|
function getInputOOUI( $value ) {
|
|
$options = array();
|
|
foreach ( $this->getOptions() as $label => $value ) {
|
|
$options[] = array(
|
|
'data' => $value,
|
|
'label' => $this->mOptionsLabelsNotFromMessage ? new OOUI\HtmlSnippet( $label ) : $label,
|
|
);
|
|
}
|
|
|
|
return new OOUI\RadioSelectInputWidget( array(
|
|
'name' => $this->mName,
|
|
'value' => $value,
|
|
'options' => $options,
|
|
'classes' => 'mw-htmlform-flatlist-item',
|
|
) + $this->getAttributes( array( 'disabled', 'tabindex' ), array( 'tabindex' => 'tabIndex' ) ) );
|
|
}
|
|
|
|
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 .= ' ' . call_user_func( $elementFunc, 'label', array( 'for' => $id ), $label );
|
|
|
|
$html .= ' ' . Html::rawElement(
|
|
'div',
|
|
array( 'class' => 'mw-htmlform-flatlist-item mw-ui-radio' ),
|
|
$radio
|
|
);
|
|
}
|
|
}
|
|
|
|
return $html;
|
|
}
|
|
|
|
protected function needsLabel() {
|
|
return false;
|
|
}
|
|
}
|