MediaWiki  master
HTMLRadioField.php
Go to the documentation of this file.
1 <?php
2 
14  public function __construct( $params ) {
15  parent::__construct( $params );
16 
17  if ( isset( $params['flatlist'] ) ) {
18  $this->mClass .= ' mw-htmlform-flatlist';
19  }
20  }
21 
22  public function validate( $value, $alldata ) {
23  $p = parent::validate( $value, $alldata );
24 
25  if ( $p !== true ) {
26  return $p;
27  }
28 
29  if ( !is_string( $value ) && !is_int( $value ) ) {
30  return $this->msg( 'htmlform-required' );
31  }
32 
33  $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
34 
35  if ( in_array( strval( $value ), $validOptions, true ) ) {
36  return true;
37  } else {
38  return $this->msg( 'htmlform-select-badoption' );
39  }
40  }
41 
50  public function getInputHTML( $value ) {
51  $html = $this->formatOptions( $this->getOptions(), strval( $value ) );
52 
53  return $html;
54  }
55 
56  public function getInputOOUI( $value ) {
57  $options = [];
58  foreach ( $this->getOptions() as $label => $data ) {
59  $options[] = [
60  'data' => $data,
61  'label' => $this->mOptionsLabelsNotFromMessage ? new OOUI\HtmlSnippet( $label ) : $label,
62  ];
63  }
64 
65  return new OOUI\RadioSelectInputWidget( [
66  'name' => $this->mName,
67  'id' => $this->mID,
68  'value' => $value,
69  'options' => $options,
70  ] + OOUI\Element::configFromHtmlAttributes(
71  $this->getAttributes( [ 'disabled', 'tabindex' ] )
72  ) );
73  }
74 
75  public function formatOptions( $options, $value ) {
77 
78  $html = '';
79 
80  $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
81  $elementFunc = [ Html::class, $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
82 
83  # @todo Should this produce an unordered list perhaps?
84  foreach ( $options as $label => $info ) {
85  if ( is_array( $info ) ) {
86  $html .= Html::rawElement( 'h1', [], $label ) . "\n";
87  $html .= $this->formatOptions( $info, $value );
88  } else {
89  $id = Sanitizer::escapeIdForAttribute( $this->mID . "-$info" );
90  $classes = [ 'mw-htmlform-flatlist-item' ];
91  if ( $wgUseMediaWikiUIEverywhere || $this->mParent instanceof VFormHTMLForm ) {
92  $classes[] = 'mw-ui-radio';
93  }
94  $radio = Xml::radio( $this->mName, $info, $info === $value, $attribs + [ 'id' => $id ] );
95  $radio .= "\u{00A0}" . call_user_func( $elementFunc, 'label', [ 'for' => $id ], $label );
96 
97  $html .= ' ' . Html::rawElement(
98  'div',
99  [ 'class' => $classes ],
100  $radio
101  );
102  }
103  }
104 
105  return $html;
106  }
107 
108  protected function needsLabel() {
109  return false;
110  }
111 }
HTMLFormField\getOptions
getOptions()
Fetch the array of options from the field's parameters.
Definition: HTMLFormField.php:1055
Sanitizer\escapeIdForAttribute
static escapeIdForAttribute( $id, $mode=self::ID_PRIMARY)
Given a section name or other user-generated or otherwise unsafe string, escapes it to be a valid HTM...
Definition: Sanitizer.php:1116
HTMLRadioField\getInputHTML
getInputHTML( $value)
This returns a block of all the radio options, in one cell.
Definition: HTMLRadioField.php:50
$wgUseMediaWikiUIEverywhere
$wgUseMediaWikiUIEverywhere
Temporary variable that applies MediaWiki UI wherever it can be supported.
Definition: DefaultSettings.php:3375
Xml\radio
static radio( $name, $value, $checked=false, $attribs=[])
Convenience function to build an HTML radio button.
Definition: Xml.php:345
HTMLRadioField\needsLabel
needsLabel()
Should this field have a label, or is there no input element with the appropriate id for the label to...
Definition: HTMLRadioField.php:108
VFormHTMLForm
Compact stacked vertical format for forms.
Definition: VFormHTMLForm.php:27
HTMLRadioField\__construct
__construct( $params)
Definition: HTMLRadioField.php:14
HTMLRadioField
Radio checkbox fields.
Definition: HTMLRadioField.php:6
HTMLFormField
The parent class to generate form fields.
Definition: HTMLFormField.php:7
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
HTMLFormField\flattenOptions
static flattenOptions( $options)
flatten an array of options to a single array, for instance, a set of "<options>" inside "<optgroups>...
Definition: HTMLFormField.php:1094
HTMLFormField\msg
msg( $key,... $params)
Get a translated interface message.
Definition: HTMLFormField.php:83
HTMLRadioField\getInputOOUI
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.
Definition: HTMLRadioField.php:56
HTMLRadioField\formatOptions
formatOptions( $options, $value)
Definition: HTMLRadioField.php:75
HTMLRadioField\validate
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
Definition: HTMLRadioField.php:22
HTMLFormField\getAttributes
getAttributes(array $list)
Returns the given attributes from the parameters.
Definition: HTMLFormField.php:999