5use InvalidArgumentException;
33 parent::__construct( $params );
35 if ( isset( $params[
'flatlist'] ) ) {
36 $this->mClass .=
' mw-htmlform-flatlist';
42 $p = parent::validate( $value, $alldata );
48 if ( !is_string( $value ) && !is_int( $value ) ) {
49 return $this->
msg(
'htmlform-required' );
54 if ( in_array( strval( $value ), $validOptions,
true ) ) {
57 return $this->
msg(
'htmlform-select-badoption' );
71 isset( $this->mParams[
'option-descriptions'] ) ||
72 isset( $this->mParams[
'option-descriptions-messages'] ) ) {
73 throw new InvalidArgumentException(
74 "Non-Codex HTMLForms do not support the 'option-descriptions' parameter for radio buttons"
86 isset( $this->mParams[
'option-descriptions'] ) ||
87 isset( $this->mParams[
'option-descriptions-messages'] ) ) {
88 throw new InvalidArgumentException(
89 "Non-Codex HTMLForms do not support the 'option-descriptions' parameter for radio buttons"
94 foreach ( $this->
getOptions() as $label => $data ) {
95 if ( is_int( $label ) ) {
96 $label = strval( $label );
104 return new \OOUI\RadioSelectInputWidget( [
105 'name' => $this->mName,
108 'options' => $options,
109 ] + \OOUI\Element::configFromHtmlAttributes(
120 foreach ( $this->
getOptions() as $label => $radioValue ) {
121 $description = $optionDescriptions[$radioValue] ??
'';
122 $descriptionID = Sanitizer::escapeIdForAttribute(
123 $this->mID .
"-$radioValue-description"
127 $radioInputClasses = [
'cdx-radio__input' ];
128 $radioInputAttribs = [
129 'id' => Sanitizer::escapeIdForAttribute( $this->mID .
"-$radioValue" ),
132 'class' => $radioInputClasses,
133 'value' => $radioValue
135 $radioInputAttribs += $this->
getAttributes( [
'disabled',
'tabindex' ] );
136 if ( $description ) {
137 $radioInputAttribs[
'aria-describedby'] = $descriptionID;
141 if ( $radioValue === $value ) {
142 $radioInputAttribs[
'checked'] =
true;
146 $radioIconClasses = [
'cdx-radio__icon' ];
147 $radioIconAttribs = [
148 'class' => $radioIconClasses,
152 $radioLabelClasses = [
'cdx-label__label' ];
153 $radioLabelAttribs = [
154 'class' => $radioLabelClasses,
155 'for' => $radioInputAttribs[
'id']
161 $radioLabel = Html::rawElement(
'label', $radioLabelAttribs,
164 $radioDescription =
'';
165 if ( isset( $optionDescriptions[$radioValue] ) ) {
166 $radioDescription = Html::rawElement(
168 [
'id' => $descriptionID,
'class' =>
'cdx-label__description' ],
169 $optionDescriptions[$radioValue]
172 $radioLabelWrapper = Html::rawElement(
174 [
'class' =>
'cdx-radio__label cdx-label' ],
175 $radioLabel . $radioDescription
179 $radio = Html::rawElement(
181 [
'class' =>
'cdx-radio' ],
182 $radioInput . $radioIcon . $radioLabelWrapper
200 $attribs = $this->
getAttributes( [
'disabled',
'tabindex' ] );
202 # @todo Should this produce an unordered list perhaps?
203 foreach ( $options as $label => $info ) {
204 if ( is_array( $info ) ) {
205 $html .= Html::rawElement(
'h1', [], $label ) .
"\n";
208 $id = Sanitizer::escapeIdForAttribute( $this->mID .
"-$info" );
209 $classes = [
'mw-htmlform-flatlist-item' ];
210 $radio = Html::radio(
213 $attribs + [
'value' => $info,
'id' => $id ]
216 $radio .=
"\u{00A0}" .
217 Html::rawElement(
'label', [
'for' => $id ], $this->
escapeLabel( $label ) );
219 $html .=
' ' . Html::rawElement(
221 [
'class' => $classes ],
237 if ( array_key_exists(
'option-descriptions-messages', $this->mParams ) ) {
238 $needsParse = $this->mParams[
'option-descriptions-messages-parse'] ??
false;
239 $optionDescriptions = [];
240 foreach ( $this->mParams[
'option-descriptions-messages'] as $value => $msgKey ) {
241 $msg = $this->
msg( $msgKey );
242 $optionDescriptions[$value] = $needsParse ? $msg->parse() : $msg->escaped();
244 return $optionDescriptions;
245 } elseif ( array_key_exists(
'option-descriptions', $this->mParams ) ) {
246 return $this->mParams[
'option-descriptions'];
263class_alias( HTMLRadioField::class,
'HTMLRadioField' );