MediaWiki  master
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  'label' => $this->mOptionsLabelsNotFromMessage ? new OOUI\HtmlSnippet( $label ) : $label,
65  ];
66  }
67 
68  return new OOUI\RadioSelectInputWidget( [
69  'name' => $this->mName,
70  'id' => $this->mID,
71  'value' => $value,
72  'options' => $options,
73  ] + OOUI\Element::configFromHtmlAttributes(
74  $this->getAttributes( [ 'disabled', 'tabindex' ] )
75  ) );
76  }
77 
78  public function formatOptions( $options, $value ) {
80 
81  $html = '';
82 
83  $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
84  $elementFunc = [ Html::class, $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
85 
86  # @todo Should this produce an unordered list perhaps?
87  foreach ( $options as $label => $info ) {
88  if ( is_array( $info ) ) {
89  $html .= Html::rawElement( 'h1', [], $label ) . "\n";
90  $html .= $this->formatOptions( $info, $value );
91  } else {
92  $id = Sanitizer::escapeIdForAttribute( $this->mID . "-$info" );
93  $classes = [ 'mw-htmlform-flatlist-item' ];
94  if ( $wgUseMediaWikiUIEverywhere || $this->mParent instanceof VFormHTMLForm ) {
95  $classes[] = 'mw-ui-radio';
96  }
97  $radio = Xml::radio( $this->mName, $info, $info === $value, $attribs + [ 'id' => $id ] );
98  $radio .= "\u{00A0}" . call_user_func( $elementFunc, 'label', [ 'for' => $id ], $label );
99 
100  $html .= ' ' . Html::rawElement(
101  'div',
102  [ 'class' => $classes ],
103  $radio
104  );
105  }
106  }
107 
108  return $html;
109  }
110 
111  protected function needsLabel() {
112  return false;
113  }
114 }
HTMLFormField\getOptions
getOptions()
Fetch the array of options from the field's parameters.
Definition: HTMLFormField.php:1095
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:1072
HTMLRadioField\getInputHTML
getInputHTML( $value)
This returns a block of all the radio options, in one cell.
Definition: HTMLRadioField.php:53
$wgUseMediaWikiUIEverywhere
$wgUseMediaWikiUIEverywhere
Temporary variable that applies MediaWiki UI wherever it can be supported.
Definition: DefaultSettings.php:3413
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:111
VFormHTMLForm
Compact stacked vertical format for forms.
Definition: VFormHTMLForm.php:29
HTMLRadioField\__construct
__construct( $params)
Stable to call.
Definition: HTMLRadioField.php:17
HTMLRadioField
Radio checkbox fields.
Definition: HTMLRadioField.php:8
HTMLFormField
The parent class to generate form fields.
Definition: HTMLFormField.php:9
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:1135
HTMLFormField\msg
msg( $key,... $params)
Get a translated interface message.
Definition: HTMLFormField.php:87
HTMLRadioField\getInputOOUI
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.
Definition: HTMLRadioField.php:59
HTMLRadioField\formatOptions
formatOptions( $options, $value)
Definition: HTMLRadioField.php:78
HTMLRadioField\validate
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
Definition: HTMLRadioField.php:25
HTMLFormField\getAttributes
getAttributes(array $list)
Returns the given attributes from the parameters Stable to override.
Definition: HTMLFormField.php:1039