MediaWiki REL1_37
HTMLRadioField.php
Go to the documentation of this file.
1<?php
2
17 public function __construct( $params ) {
18 parent::__construct( $params );
19
20 if ( isset( $params['flatlist'] ) ) {
21 $this->mClass .= ' mw-htmlform-flatlist';
22 }
23 }
24
25 public function validate( $value, $alldata ) {
26 $p = parent::validate( $value, $alldata );
27
28 if ( $p !== true ) {
29 return $p;
30 }
31
32 if ( !is_string( $value ) && !is_int( $value ) ) {
33 return $this->msg( 'htmlform-required' );
34 }
35
36 $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
37
38 if ( in_array( strval( $value ), $validOptions, true ) ) {
39 return true;
40 } else {
41 return $this->msg( 'htmlform-select-badoption' );
42 }
43 }
44
53 public function getInputHTML( $value ) {
54 $html = $this->formatOptions( $this->getOptions(), strval( $value ) );
55
56 return $html;
57 }
58
59 public function getInputOOUI( $value ) {
60 $options = [];
61 foreach ( $this->getOptions() as $label => $data ) {
62 $options[] = [
63 'data' => $data,
64 // @phan-suppress-next-line SecurityCheck-XSS Labels are raw when not from message
65 'label' => $this->mOptionsLabelsNotFromMessage ? new OOUI\HtmlSnippet( $label ) : $label,
66 ];
67 }
68
69 return new OOUI\RadioSelectInputWidget( [
70 'name' => $this->mName,
71 'id' => $this->mID,
72 'value' => $value,
73 'options' => $options,
74 ] + OOUI\Element::configFromHtmlAttributes(
75 $this->getAttributes( [ 'disabled', 'tabindex' ] )
76 ) );
77 }
78
79 public function formatOptions( $options, $value ) {
81
82 $html = '';
83
84 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
85 $elementFunc = [ Html::class, $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
86
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 );
92 } else {
93 $id = Sanitizer::escapeIdForAttribute( $this->mID . "-$info" );
94 $classes = [ 'mw-htmlform-flatlist-item' ];
95 if ( $wgUseMediaWikiUIEverywhere || $this->mParent instanceof VFormHTMLForm ) {
96 $classes[] = 'mw-ui-radio';
97 }
98 $radio = Xml::radio( $this->mName, $info, $info === $value, $attribs + [ 'id' => $id ] );
99 $radio .= "\u{00A0}" . call_user_func( $elementFunc, 'label', [ 'for' => $id ], $label );
100
101 $html .= ' ' . Html::rawElement(
102 'div',
103 [ 'class' => $classes ],
104 $radio
105 );
106 }
107 }
108
109 return $html;
110 }
111
112 protected function needsLabel() {
113 return false;
114 }
115}
$wgUseMediaWikiUIEverywhere
Temporary variable that applies MediaWiki UI wherever it can be supported.
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.
Compact stacked vertical format for forms.