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