MediaWiki master
HTMLSelectField.php
Go to the documentation of this file.
1<?php
2
4
6use XmlSelect;
7
14
19 public function validate( $value, $alldata ) {
20 $p = parent::validate( $value, $alldata );
21
22 if ( $p !== true ) {
23 return $p;
24 }
25
26 $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
27
28 if ( in_array( strval( $value ), $validOptions, true ) ) {
29 return true;
30 } else {
31 return $this->msg( 'htmlform-select-badoption' );
32 }
33 }
34
39 public function getInputHTML( $value ) {
40 $select = new XmlSelect( $this->mName, $this->mID, strval( $value ) );
41
42 if ( !empty( $this->mParams['disabled'] ) ) {
43 $select->setAttribute( 'disabled', 'disabled' );
44 }
45
46 $allowedParams = [ 'tabindex', 'size' ];
47 $customParams = $this->getAttributes( $allowedParams );
48 foreach ( $customParams as $name => $value ) {
49 $select->setAttribute( $name, $value );
50 }
51
52 if ( $this->mClass !== '' ) {
53 $select->setAttribute( 'class', $this->mClass );
54 }
55
56 $select->addOptions( $this->getOptions() );
57
58 return $select->getHTML();
59 }
60
65 public function getInputOOUI( $value ) {
66 $disabled = false;
67 $allowedParams = [ 'tabindex' ];
68 $attribs = \OOUI\Element::configFromHtmlAttributes(
69 $this->getAttributes( $allowedParams )
70 );
71
72 if ( $this->mClass !== '' ) {
73 $attribs['classes'] = [ $this->mClass ];
74 }
75
76 if ( !empty( $this->mParams['disabled'] ) ) {
77 $disabled = true;
78 }
79
80 return new \OOUI\DropdownInputWidget( [
81 'name' => $this->mName,
82 'id' => $this->mID,
83 'options' => $this->getOptionsOOUI(),
84 'value' => strval( $value ),
85 'disabled' => $disabled,
86 ] + $attribs );
87 }
88
89 public function getInputCodex( $value, $hasErrors ) {
90 $select = new XmlSelect( $this->mName, $this->mID, strval( $value ) );
91
92 if ( !empty( $this->mParams['disabled'] ) ) {
93 $select->setAttribute( 'disabled', 'disabled' );
94 }
95
96 $allowedParams = [ 'tabindex', 'size' ];
97 $customParams = $this->getAttributes( $allowedParams );
98 foreach ( $customParams as $name => $value ) {
99 $select->setAttribute( $name, $value );
100 }
101
102 // TODO: Add support for error class once it's implemented in the Codex CSS-only Select.
103 $selectClass = 'cdx-select';
104 $selectClass .= $this->mClass !== '' ? ' ' . $this->mClass : '';
105 $select->setAttribute( 'class', $selectClass );
106
107 $select->addOptions( $this->getOptions() );
108
109 return $select->getHTML();
110 }
111
116 protected function shouldInfuseOOUI() {
117 return true;
118 }
119}
120
122class_alias( HTMLSelectField::class, 'HTMLSelectField' );
shouldInfuseOOUI()
Whether the field should be automatically infused.Note that all OOUI HTMLForm fields are infusable (y...
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.Don't forget to call pare...
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
getInputCodex( $value, $hasErrors)
Same as getInputHTML, but for Codex.
The parent class to generate form fields.
getOptionsOOUI()
Get options and make them into arrays suitable for OOUI.
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.
getAttributes(array $list)
Returns the given attributes from the parameters.
msg( $key,... $params)
Get a translated interface message.
Class for generating HTML <select> or <datalist> elements.
Definition XmlSelect.php:28