MediaWiki  master
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  $options[] = [
66  'data' => $data,
67  // @phan-suppress-next-line SecurityCheck-XSS Labels are raw when not from message
68  'label' => $this->mOptionsLabelsNotFromMessage ? new OOUI\HtmlSnippet( $label ) : $label,
69  ];
70  }
71 
72  return new OOUI\RadioSelectInputWidget( [
73  'name' => $this->mName,
74  'id' => $this->mID,
75  'value' => $value,
76  'options' => $options,
77  ] + OOUI\Element::configFromHtmlAttributes(
78  $this->getAttributes( [ 'disabled', 'tabindex' ] )
79  ) );
80  }
81 
82  public function formatOptions( $options, $value ) {
83  $useMediaWikiUIEverywhere = MediaWikiServices::getInstance()
84  ->getMainConfig()->get( MainConfigNames::UseMediaWikiUIEverywhere );
85 
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  if ( $useMediaWikiUIEverywhere || $this->mParent instanceof VFormHTMLForm ) {
100  $classes[] = 'mw-ui-radio';
101  }
102  $radio = Xml::radio( $this->mName, $info, $info === $value, $attribs + [ 'id' => $id ] );
103  $radio .= "\u{00A0}" . call_user_func( $elementFunc, 'label', [ 'for' => $id ], $label );
104 
105  $html .= ' ' . Html::rawElement(
106  'div',
107  [ 'class' => $classes ],
108  $radio
109  );
110  }
111  }
112 
113  return $html;
114  }
115 
116  protected function needsLabel() {
117  return false;
118  }
119 }
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.
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:214
A class containing constants representing the names of configuration variables.
MediaWikiServices is the service locator for the application scope of MediaWiki.
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:946
Compact stacked vertical format for forms.
static radio( $name, $value, $checked=false, $attribs=[])
Convenience function to build an HTML radio button.
Definition: Xml.php:350