MediaWiki  master
HTMLSelectField.php
Go to the documentation of this file.
1 <?php
2 
9 
14  public function validate( $value, $alldata ) {
15  $p = parent::validate( $value, $alldata );
16 
17  if ( $p !== true ) {
18  return $p;
19  }
20 
21  $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
22 
23  if ( in_array( strval( $value ), $validOptions, true ) ) {
24  return true;
25  } else {
26  return $this->msg( 'htmlform-select-badoption' );
27  }
28  }
29 
34  public function getInputHTML( $value ) {
35  $select = new XmlSelect( $this->mName, $this->mID, strval( $value ) );
36 
37  if ( !empty( $this->mParams['disabled'] ) ) {
38  $select->setAttribute( 'disabled', 'disabled' );
39  }
40 
41  $allowedParams = [ 'tabindex', 'size' ];
42  $customParams = $this->getAttributes( $allowedParams );
43  foreach ( $customParams as $name => $value ) {
44  $select->setAttribute( $name, $value );
45  }
46 
47  if ( $this->mClass !== '' ) {
48  $select->setAttribute( 'class', $this->mClass );
49  }
50 
51  $select->addOptions( $this->getOptions() );
52 
53  return $select->getHTML();
54  }
55 
60  public function getInputOOUI( $value ) {
61  $disabled = false;
62  $allowedParams = [ 'tabindex' ];
63  $attribs = OOUI\Element::configFromHtmlAttributes(
64  $this->getAttributes( $allowedParams )
65  );
66 
67  if ( $this->mClass !== '' ) {
68  $attribs['classes'] = [ $this->mClass ];
69  }
70 
71  if ( !empty( $this->mParams['disabled'] ) ) {
72  $disabled = true;
73  }
74 
75  return new OOUI\DropdownInputWidget( [
76  'name' => $this->mName,
77  'id' => $this->mID,
78  'options' => $this->getOptionsOOUI(),
79  'value' => strval( $value ),
80  'disabled' => $disabled,
81  ] + $attribs );
82  }
83 
88  protected function shouldInfuseOOUI() {
89  return true;
90  }
91 }
HTMLFormField\getOptions
getOptions()
Fetch the array of options from the field's parameters.
Definition: HTMLFormField.php:1095
HTMLSelectField\getInputHTML
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
Definition: HTMLSelectField.php:34
HTMLFormField\$mClass
$mClass
Definition: HTMLFormField.php:19
XmlSelect
Class for generating HTML <select> or <datalist> elements.
Definition: XmlSelect.php:26
HTMLSelectField\shouldInfuseOOUI
shouldInfuseOOUI()
Whether the field should be automatically infused.Note that all OOUI HTMLForm fields are infusable (y...
Definition: HTMLSelectField.php:88
HTMLSelectField
A select dropdown field.
Definition: HTMLSelectField.php:8
HTMLFormField
The parent class to generate form fields.
Definition: HTMLFormField.php:9
HTMLSelectField\getInputOOUI
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
Definition: HTMLSelectField.php:60
HTMLSelectField\validate
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.Don't forget to call pare...
Definition: HTMLSelectField.php:14
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
HTMLFormField\getOptionsOOUI
getOptionsOOUI()
Get options and make them into arrays suitable for OOUI.
Definition: HTMLFormField.php:1118
HTMLFormField\getAttributes
getAttributes(array $list)
Returns the given attributes from the parameters Stable to override.
Definition: HTMLFormField.php:1039