From: Thalia Date: Thu, 13 Jun 2019 19:04:49 +0000 (+0100) Subject: Fix some issues with HTMLSelectAndOtherField default and validation X-Git-Tag: 1.34.0-rc.0~1377^2 X-Git-Url: http://git.cyclocoop.org/%7B%7B%20url_for%28%27admin_vote_add%27%29%20%7D%7D?a=commitdiff_plain;h=db9ff28e3ebe87ed965561d4458841b7aad095c9;p=lhc%2Fweb%2Fwiklou.git Fix some issues with HTMLSelectAndOtherField default and validation Bring HTMLSelectAndOtherField in line with other HTMLFormFields by ensuring that the default value is of the correct type and passes validation checks. Also make sure HTMLSelectAndOtherField::validate checks for the final value, if the field has required set to true. Bug: T222170 Bug: T225860 Change-Id: I949ee3df2b1f597982cf522b149c54b8e79d59bc --- diff --git a/includes/htmlform/fields/HTMLSelectAndOtherField.php b/includes/htmlform/fields/HTMLSelectAndOtherField.php index f137bf1085..85cbbb1055 100644 --- a/includes/htmlform/fields/HTMLSelectAndOtherField.php +++ b/includes/htmlform/fields/HTMLSelectAndOtherField.php @@ -141,6 +141,35 @@ class HTMLSelectAndOtherField extends HTMLSelectField { return new MediaWiki\Widget\SelectWithInputWidget( $params ); } + /** + * @inheritDoc + */ + public function getDefault() { + $default = parent::getDefault(); + + // Default values of empty form + $final = ''; + $list = 'other'; + $text = ''; + + if ( $default !== null ) { + $final = $default; + // Assume the default is a text value, with the 'other' option selected. + // Then check if that assumption is correct, and update $list and $text if not. + $text = $final; + foreach ( $this->mFlatOptions as $option ) { + $match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text(); + if ( strpos( $final, $match ) === 0 ) { + $list = $option; + $text = substr( $final, strlen( $match ) ); + break; + } + } + } + + return [ $final, $list, $text ]; + } + /** * @param WebRequest $request * @@ -163,22 +192,9 @@ class HTMLSelectAndOtherField extends HTMLSelectField { } else { $final = $list . $this->msg( 'colon-separator' )->inContentLanguage()->text() . $text; } - } else { - $final = $this->getDefault(); - - $list = 'other'; - $text = $final; - foreach ( $this->mFlatOptions as $option ) { - $match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text(); - if ( strpos( $text, $match ) === 0 ) { - $list = $option; - $text = substr( $text, strlen( $match ) ); - break; - } - } + return [ $final, $list, $text ]; } - - return [ $final, $list, $text ]; + return $this->getDefault(); } public function getSize() { @@ -197,7 +213,7 @@ class HTMLSelectAndOtherField extends HTMLSelectField { if ( isset( $this->mParams['required'] ) && $this->mParams['required'] !== false - && $value[1] === '' + && $value[0] === '' ) { return $this->msg( 'htmlform-required' ); }