Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / htmlform / fields / HTMLSelectField.php
blob2bcf719bae7f4cc8dd8bddbdfc79afbe5c9ef015
1 <?php
3 namespace MediaWiki\HTMLForm\Field;
5 use MediaWiki\HTMLForm\HTMLFormField;
6 use MediaWiki\Xml\XmlSelect;
8 /**
9 * A select dropdown field. Basically a wrapper for Xmlselect class
11 * @stable to extend
13 class HTMLSelectField extends HTMLFormField {
15 /**
16 * @inheritDoc
17 * @stable to override
19 public function validate( $value, $alldata ) {
20 $p = parent::validate( $value, $alldata );
22 if ( $p !== true ) {
23 return $p;
26 $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
28 if ( in_array( strval( $value ), $validOptions, true ) ) {
29 return true;
30 } else {
31 return $this->msg( 'htmlform-select-badoption' );
35 /**
36 * @inheritDoc
37 * @stable to override
39 public function getInputHTML( $value ) {
40 $select = new XmlSelect( $this->mName, $this->mID, strval( $value ) );
42 if ( !empty( $this->mParams['disabled'] ) ) {
43 $select->setAttribute( 'disabled', 'disabled' );
46 $allowedParams = [ 'tabindex', 'size' ];
47 $customParams = $this->getAttributes( $allowedParams );
48 foreach ( $customParams as $name => $value ) {
49 $select->setAttribute( $name, $value );
52 if ( $this->mClass !== '' ) {
53 $select->setAttribute( 'class', $this->mClass );
56 $select->addOptions( $this->getOptions() );
58 return $select->getHTML();
61 /**
62 * @inheritDoc
63 * @stable to override
65 public function getInputOOUI( $value ) {
66 $disabled = false;
67 $allowedParams = [ 'tabindex' ];
68 $attribs = \OOUI\Element::configFromHtmlAttributes(
69 $this->getAttributes( $allowedParams )
72 if ( $this->mClass !== '' ) {
73 $attribs['classes'] = [ $this->mClass ];
76 if ( !empty( $this->mParams['disabled'] ) ) {
77 $disabled = true;
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 );
89 public function getInputCodex( $value, $hasErrors ) {
90 $select = new XmlSelect( $this->mName, $this->mID, strval( $value ) );
92 if ( !empty( $this->mParams['disabled'] ) ) {
93 $select->setAttribute( 'disabled', 'disabled' );
96 $allowedParams = [ 'tabindex', 'size' ];
97 $customParams = $this->getAttributes( $allowedParams );
98 foreach ( $customParams as $name => $value ) {
99 $select->setAttribute( $name, $value );
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 );
107 $select->addOptions( $this->getOptions() );
109 return $select->getHTML();
113 * @inheritDoc
114 * @stable to override
116 protected function shouldInfuseOOUI() {
117 return true;
121 /** @deprecated class alias since 1.42 */
122 class_alias( HTMLSelectField::class, 'HTMLSelectField' );