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 = $this->mParent->getConfig()->get( MainConfigNames::UseMediaWikiUIEverywhere );
84 
85  $html = '';
86 
87  $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
88  $elementFunc = [ Html::class, $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
89 
90  # @todo Should this produce an unordered list perhaps?
91  foreach ( $options as $label => $info ) {
92  if ( is_array( $info ) ) {
93  $html .= Html::rawElement( 'h1', [], $label ) . "\n";
94  $html .= $this->formatOptions( $info, $value );
95  } else {
96  $id = Sanitizer::escapeIdForAttribute( $this->mID . "-$info" );
97  $classes = [ 'mw-htmlform-flatlist-item' ];
98  if ( $useMediaWikiUIEverywhere || $this->mParent instanceof VFormHTMLForm ) {
99  $classes[] = 'mw-ui-radio';
100  }
101  $radio = Xml::radio( $this->mName, $info, $info === $value, $attribs + [ 'id' => $id ] );
102  $radio .= "\u{00A0}" . call_user_func( $elementFunc, 'label', [ 'for' => $id ], $label );
103 
104  $html .= ' ' . Html::rawElement(
105  'div',
106  [ 'class' => $classes ],
107  $radio
108  );
109  }
110  }
111 
112  return $html;
113  }
114 
115  protected function needsLabel() {
116  return false;
117  }
118 }
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.
This class is a collection of static functions that serve two purposes:
Definition: Html.php:55
A class containing constants representing the names of configuration variables.
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:348