MediaWiki master
HTMLSelectField.php
Go to the documentation of this file.
1<?php
2
4
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
90 public function getInputCodex( $value, $hasErrors ) {
91 $select = new XmlSelect( $this->mName, $this->mID, strval( $value ) );
92
93 if ( !empty( $this->mParams['disabled'] ) ) {
94 $select->setAttribute( 'disabled', 'disabled' );
95 }
96
97 $allowedParams = [ 'tabindex', 'size' ];
98 $customParams = $this->getAttributes( $allowedParams );
99 foreach ( $customParams as $name => $value ) {
100 $select->setAttribute( $name, $value );
101 }
102
103 // TODO: Add support for error class once it's implemented in the Codex CSS-only Select.
104 $selectClass = 'cdx-select';
105 $selectClass .= $this->mClass !== '' ? ' ' . $this->mClass : '';
106 $select->setAttribute( 'class', $selectClass );
107
108 $select->addOptions( $this->getOptions() );
109
110 return $select->getHTML();
111 }
112
117 protected function shouldInfuseOOUI() {
118 return true;
119 }
120}
121
123class_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.This is called by CodexHTMLForm.If not overridden,...
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:16