HTMLSelectField.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * A select dropdown field. Basically a wrapper for Xmlselect class
  4. */
  5. class HTMLSelectField extends HTMLFormField {
  6. public function validate( $value, $alldata ) {
  7. $p = parent::validate( $value, $alldata );
  8. if ( $p !== true ) {
  9. return $p;
  10. }
  11. $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
  12. if ( in_array( strval( $value ), $validOptions, true ) ) {
  13. return true;
  14. } else {
  15. return $this->msg( 'htmlform-select-badoption' );
  16. }
  17. }
  18. public function getInputHTML( $value ) {
  19. $select = new XmlSelect( $this->mName, $this->mID, strval( $value ) );
  20. if ( !empty( $this->mParams['disabled'] ) ) {
  21. $select->setAttribute( 'disabled', 'disabled' );
  22. }
  23. $allowedParams = [ 'tabindex', 'size' ];
  24. $customParams = $this->getAttributes( $allowedParams );
  25. foreach ( $customParams as $name => $value ) {
  26. $select->setAttribute( $name, $value );
  27. }
  28. if ( $this->mClass !== '' ) {
  29. $select->setAttribute( 'class', $this->mClass );
  30. }
  31. $select->addOptions( $this->getOptions() );
  32. return $select->getHTML();
  33. }
  34. public function getInputOOUI( $value ) {
  35. $disabled = false;
  36. $allowedParams = [ 'tabindex' ];
  37. $attribs = OOUI\Element::configFromHtmlAttributes(
  38. $this->getAttributes( $allowedParams )
  39. );
  40. if ( $this->mClass !== '' ) {
  41. $attribs['classes'] = [ $this->mClass ];
  42. }
  43. if ( !empty( $this->mParams['disabled'] ) ) {
  44. $disabled = true;
  45. }
  46. return new OOUI\DropdownInputWidget( [
  47. 'name' => $this->mName,
  48. 'id' => $this->mID,
  49. 'options' => $this->getOptionsOOUI(),
  50. 'value' => strval( $value ),
  51. 'disabled' => $disabled,
  52. ] + $attribs );
  53. }
  54. protected function shouldInfuseOOUI() {
  55. return true;
  56. }
  57. }