MediaWiki master
HTMLRadioField.php
Go to the documentation of this file.
1<?php
2
4
8use Xml;
9
24 public function __construct( $params ) {
25 parent::__construct( $params );
26
27 if ( isset( $params['flatlist'] ) ) {
28 $this->mClass .= ' mw-htmlform-flatlist';
29 }
30 }
31
32 public function validate( $value, $alldata ) {
33 $p = parent::validate( $value, $alldata );
34
35 if ( $p !== true ) {
36 return $p;
37 }
38
39 if ( !is_string( $value ) && !is_int( $value ) ) {
40 return $this->msg( 'htmlform-required' );
41 }
42
43 $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
44
45 if ( in_array( strval( $value ), $validOptions, true ) ) {
46 return true;
47 } else {
48 return $this->msg( 'htmlform-select-badoption' );
49 }
50 }
51
60 public function getInputHTML( $value ) {
61 $html = $this->formatOptions( $this->getOptions(), strval( $value ) );
62
63 return $html;
64 }
65
66 public function getInputOOUI( $value ) {
67 $options = [];
68 foreach ( $this->getOptions() as $label => $data ) {
69 if ( is_int( $label ) ) {
70 $label = strval( $label );
71 }
72 $options[] = [
73 'data' => $data,
74 // @phan-suppress-next-line SecurityCheck-XSS Labels are raw when not from message
75 'label' => $this->mOptionsLabelsNotFromMessage ? new \OOUI\HtmlSnippet( $label ) : $label,
76 ];
77 }
78
79 return new \OOUI\RadioSelectInputWidget( [
80 'name' => $this->mName,
81 'id' => $this->mID,
82 'value' => $value,
83 'options' => $options,
84 ] + \OOUI\Element::configFromHtmlAttributes(
85 $this->getAttributes( [ 'disabled', 'tabindex' ] )
86 ) );
87 }
88
89 public function formatOptions( $options, $value ) {
90 $html = '';
91
92 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
93 $elementFunc = [ Html::class, $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
94
95 # @todo Should this produce an unordered list perhaps?
96 foreach ( $options as $label => $info ) {
97 if ( is_array( $info ) ) {
98 $html .= Html::rawElement( 'h1', [], $label ) . "\n";
99 $html .= $this->formatOptions( $info, $value );
100 } else {
101 $id = Sanitizer::escapeIdForAttribute( $this->mID . "-$info" );
102 $classes = [ 'mw-htmlform-flatlist-item' ];
103 $radio = Xml::radio( $this->mName, $info, $info === $value, $attribs + [ 'id' => $id ] );
104 $radio .= "\u{00A0}" . call_user_func( $elementFunc, 'label', [ 'for' => $id ], $label );
105
106 $html .= ' ' . Html::rawElement(
107 'div',
108 [ 'class' => $classes ],
109 $radio
110 );
111 }
112 }
113
114 return $html;
115 }
116
117 protected function needsLabel() {
118 return false;
119 }
120}
121
123class_alias( HTMLRadioField::class, 'HTMLRadioField' );
needsLabel()
Should this field have a label, or is there no input element with the appropriate id for the label to...
getInputHTML( $value)
This returns a block of all the radio options, in one cell.
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
The parent class to generate form fields.
static flattenOptions( $options)
flatten an array of options to a single array, for instance, a set of "<options>" inside "<optgroups>...
getOptions()
Fetch the array of options from the field's parameters.
getAttributes(array $list)
Returns the given attributes from the parameters.
msg( $key,... $params)
Get a translated interface message.
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:46
Module of static functions for generating XML.
Definition Xml.php:33