5use InvalidArgumentException;
33 parent::__construct( $params );
35 if ( isset( $params[
'flatlist'] ) ) {
36 $this->mClass .=
' mw-htmlform-flatlist';
41 $p = parent::validate( $value, $alldata );
47 if ( !is_string( $value ) && !is_int( $value ) ) {
48 return $this->
msg(
'htmlform-required' );
53 if ( in_array( strval( $value ), $validOptions,
true ) ) {
56 return $this->
msg(
'htmlform-select-badoption' );
70 isset( $this->mParams[
'option-descriptions'] ) ||
71 isset( $this->mParams[
'option-descriptions-messages'] ) ) {
72 throw new InvalidArgumentException(
73 "Non-Codex HTMLForms do not support the 'option-descriptions' parameter for radio buttons"
84 isset( $this->mParams[
'option-descriptions'] ) ||
85 isset( $this->mParams[
'option-descriptions-messages'] ) ) {
86 throw new InvalidArgumentException(
87 "Non-Codex HTMLForms do not support the 'option-descriptions' parameter for radio buttons"
92 foreach ( $this->
getOptions() as $label => $data ) {
93 if ( is_int( $label ) ) {
94 $label = strval( $label );
102 return new \OOUI\RadioSelectInputWidget( [
103 'name' => $this->mName,
106 'options' => $options,
107 ] + \OOUI\Element::configFromHtmlAttributes(
117 foreach ( $this->
getOptions() as $label => $radioValue ) {
118 $description = $optionDescriptions[$radioValue] ??
'';
119 $descriptionID = Sanitizer::escapeIdForAttribute(
120 $this->mID .
"-$radioValue-description"
124 $radioInputClasses = [
'cdx-radio__input' ];
125 $radioInputAttribs = [
126 'id' => Sanitizer::escapeIdForAttribute( $this->mID .
"-$radioValue" ),
129 'class' => $radioInputClasses,
130 'value' => $radioValue
132 $radioInputAttribs += $this->
getAttributes( [
'disabled',
'tabindex' ] );
133 if ( $description ) {
134 $radioInputAttribs[
'aria-describedby'] = $descriptionID;
138 if ( $radioValue === $value ) {
139 $radioInputAttribs[
'checked'] =
true;
143 $radioIconClasses = [
'cdx-radio__icon' ];
144 $radioIconAttribs = [
145 'class' => $radioIconClasses,
149 $radioLabelClasses = [
'cdx-label__label' ];
150 $radioLabelAttribs = [
151 'class' => $radioLabelClasses,
152 'for' => $radioInputAttribs[
'id']
158 $radioLabel = Html::rawElement(
'label', $radioLabelAttribs,
161 $radioDescription =
'';
162 if ( isset( $optionDescriptions[$radioValue] ) ) {
163 $radioDescription = Html::rawElement(
165 [
'id' => $descriptionID,
'class' =>
'cdx-label__description' ],
166 $optionDescriptions[$radioValue]
169 $radioLabelWrapper = Html::rawElement(
171 [
'class' =>
'cdx-radio__label cdx-label' ],
172 $radioLabel . $radioDescription
176 $radio = Html::rawElement(
178 [
'class' =>
'cdx-radio' ],
179 $radioInput . $radioIcon . $radioLabelWrapper
192 $attribs = $this->
getAttributes( [
'disabled',
'tabindex' ] );
194 # @todo Should this produce an unordered list perhaps?
195 foreach ( $options as $label => $info ) {
196 if ( is_array( $info ) ) {
197 $html .= Html::rawElement(
'h1', [], $label ) .
"\n";
200 $id = Sanitizer::escapeIdForAttribute( $this->mID .
"-$info" );
201 $classes = [
'mw-htmlform-flatlist-item' ];
202 $radio = Html::radio(
205 $attribs + [
'value' => $info,
'id' => $id ]
208 $radio .=
"\u{00A0}" .
209 Html::rawElement(
'label', [
'for' => $id ], $this->
escapeLabel( $label ) );
211 $html .=
' ' . Html::rawElement(
213 [
'class' => $classes ],
229 if ( array_key_exists(
'option-descriptions-messages', $this->mParams ) ) {
230 $needsParse = $this->mParams[
'option-descriptions-messages-parse'] ??
false;
231 $optionDescriptions = [];
232 foreach ( $this->mParams[
'option-descriptions-messages'] as $value => $msgKey ) {
233 $msg = $this->
msg( $msgKey );
234 $optionDescriptions[$value] = $needsParse ? $msg->parse() : $msg->escaped();
236 return $optionDescriptions;
237 } elseif ( array_key_exists(
'option-descriptions', $this->mParams ) ) {
238 return $this->mParams[
'option-descriptions'];
248class_alias( HTMLRadioField::class,
'HTMLRadioField' );