4 * Radio checkbox fields.
6 class HTMLRadioField
extends HTMLFormField
{
9 * In adition to the usual HTMLFormField parameters, this can take the following fields:
10 * - flatlist: If given, the options will be displayed on a single line (wrapping to following
11 * lines if necessary), rather than each one on a line of its own. This is desirable mostly
12 * for very short lists of concisely labelled options.
14 public function __construct( $params ) {
15 parent
::__construct( $params );
17 if ( isset( $params['flatlist'] ) ) {
18 $this->mClass
.= ' mw-htmlform-flatlist';
22 public function validate( $value, $alldata ) {
23 $p = parent
::validate( $value, $alldata );
29 if ( !is_string( $value ) && !is_int( $value ) ) {
30 return $this->msg( 'htmlform-required' );
33 $validOptions = HTMLFormField
::flattenOptions( $this->getOptions() );
35 if ( in_array( strval( $value ), $validOptions, true ) ) {
38 return $this->msg( 'htmlform-select-badoption' );
43 * This returns a block of all the radio options, in one cell.
44 * @see includes/HTMLFormField#getInputHTML()
46 * @param string $value
50 public function getInputHTML( $value ) {
51 $html = $this->formatOptions( $this->getOptions(), strval( $value ) );
56 public function getInputOOUI( $value ) {
58 foreach ( $this->getOptions() as $label => $data ) {
61 'label' => $this->mOptionsLabelsNotFromMessage ?
new OOUI\
HtmlSnippet( $label ) : $label,
65 return new OOUI\
RadioSelectInputWidget( [
66 'name' => $this->mName
,
69 'options' => $options,
70 ] + OOUI\Element
::configFromHtmlAttributes(
71 $this->getAttributes( [ 'disabled', 'tabindex' ] )
75 protected function shouldInfuseOOUI() {
79 public function formatOptions( $options, $value ) {
80 global $wgUseMediaWikiUIEverywhere;
84 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
85 $elementFunc = [ 'Html', $this->mOptionsLabelsNotFromMessage ?
'rawElement' : 'element' ];
87 # @todo Should this produce an unordered list perhaps?
88 foreach ( $options as $label => $info ) {
89 if ( is_array( $info ) ) {
90 $html .= Html
::rawElement( 'h1', [], $label ) . "\n";
91 $html .= $this->formatOptions( $info, $value );
93 $id = Sanitizer
::escapeId( $this->mID
. "-$info" );
94 $classes = [ 'mw-htmlform-flatlist-item' ];
95 if ( $wgUseMediaWikiUIEverywhere ||
$this->mParent
instanceof VFormHTMLForm
) {
96 $classes[] = 'mw-ui-radio';
98 $radio = Xml
::radio( $this->mName
, $info, $info === $value, $attribs +
[ 'id' => $id ] );
99 $radio .= ' ' . call_user_func( $elementFunc, 'label', [ 'for' => $id ], $label );
101 $html .= ' ' . Html
::rawElement(
103 [ 'class' => $classes ],
112 protected function needsLabel() {