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