From 5f57d5d657b7bb4b656b99acad1b2e85b9c834a8 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Wed, 5 Mar 2014 15:15:41 -0500 Subject: [PATCH] HTMLForm: Clean up 0 handling PHP, particularly with in_array, really has problems with integer 0 versus non-numeric strings. Let's clean that up by converting values to strings more agressively and using the $strict option to in_array. Oddly enough, the one place where strict in_array was being used already broke when If4175332 stringified the values in one array but not the other. Bug: 62268 Change-Id: Id34e654eb8d0e70d093b11445273e542e491e265 --- includes/htmlform/HTMLMultiSelectField.php | 4 +++- includes/htmlform/HTMLRadioField.php | 6 +++--- includes/htmlform/HTMLSelectAndOtherField.php | 2 +- includes/htmlform/HTMLSelectField.php | 2 +- includes/htmlform/HTMLSelectOrOtherField.php | 7 +++++-- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/includes/htmlform/HTMLMultiSelectField.php b/includes/htmlform/HTMLMultiSelectField.php index 2944c24f86..3cf31885f3 100644 --- a/includes/htmlform/HTMLMultiSelectField.php +++ b/includes/htmlform/HTMLMultiSelectField.php @@ -28,6 +28,7 @@ class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable } function getInputHTML( $value ) { + $value = HTMLFormField::forceToStringRecursive( $value ); $html = $this->formatOptions( $this->getOptions(), $value ); return $html; @@ -103,11 +104,12 @@ class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable } function filterDataForSubmit( $data ) { + $data = HTMLFormField::forceToStringRecursive( $data ); $options = HTMLFormField::flattenOptions( $this->getOptions() ); $res = array(); foreach ( $options as $opt ) { - $res["$opt"] = in_array( $opt, $data ); + $res["$opt"] = in_array( $opt, $data, true ); } return $res; diff --git a/includes/htmlform/HTMLRadioField.php b/includes/htmlform/HTMLRadioField.php index aa0ea5d325..c52f0a8724 100644 --- a/includes/htmlform/HTMLRadioField.php +++ b/includes/htmlform/HTMLRadioField.php @@ -17,7 +17,7 @@ class HTMLRadioField extends HTMLFormField { $validOptions = HTMLFormField::flattenOptions( $this->getOptions() ); - if ( in_array( $value, $validOptions ) ) { + if ( in_array( strval( $value ), $validOptions, true ) ) { return true; } else { return $this->msg( 'htmlform-select-badoption' )->parse(); @@ -33,7 +33,7 @@ class HTMLRadioField extends HTMLFormField { * @return String */ function getInputHTML( $value ) { - $html = $this->formatOptions( $this->getOptions(), $value ); + $html = $this->formatOptions( $this->getOptions(), strval( $value ) ); return $html; } @@ -51,7 +51,7 @@ class HTMLRadioField extends HTMLFormField { $html .= $this->formatOptions( $info, $value ); } else { $id = Sanitizer::escapeId( $this->mID . "-$info" ); - $radio = Xml::radio( $this->mName, $info, $info == $value, $attribs + array( 'id' => $id ) ); + $radio = Xml::radio( $this->mName, $info, $info === $value, $attribs + array( 'id' => $id ) ); $radio .= ' ' . call_user_func( $elementFunc, 'label', array( 'for' => $id ), $label ); $html .= ' ' . Html::rawElement( diff --git a/includes/htmlform/HTMLSelectAndOtherField.php b/includes/htmlform/HTMLSelectAndOtherField.php index 2e91a23a82..564927f7dd 100644 --- a/includes/htmlform/HTMLSelectAndOtherField.php +++ b/includes/htmlform/HTMLSelectAndOtherField.php @@ -72,7 +72,7 @@ class HTMLSelectAndOtherField extends HTMLSelectField { if ( $list == 'other' ) { $final = $text; - } elseif ( !in_array( $list, $this->mFlatOptions ) ) { + } elseif ( !in_array( $list, $this->mFlatOptions, true ) ) { # User has spoofed the select form to give an option which wasn't # in the original offer. Sulk... $final = $text; diff --git a/includes/htmlform/HTMLSelectField.php b/includes/htmlform/HTMLSelectField.php index 043748095c..c32b445157 100644 --- a/includes/htmlform/HTMLSelectField.php +++ b/includes/htmlform/HTMLSelectField.php @@ -13,7 +13,7 @@ class HTMLSelectField extends HTMLFormField { $validOptions = HTMLFormField::flattenOptions( $this->getOptions() ); - if ( in_array( $value, $validOptions ) ) { + if ( in_array( strval( $value ), $validOptions, true ) ) { return true; } else { return $this->msg( 'htmlform-select-badoption' )->parse(); diff --git a/includes/htmlform/HTMLSelectOrOtherField.php b/includes/htmlform/HTMLSelectOrOtherField.php index 045b8dfe54..e8bcb5b626 100644 --- a/includes/htmlform/HTMLSelectOrOtherField.php +++ b/includes/htmlform/HTMLSelectOrOtherField.php @@ -21,7 +21,10 @@ class HTMLSelectOrOtherField extends HTMLTextField { $valInSelect = false; if ( $value !== false ) { - $valInSelect = in_array( $value, HTMLFormField::flattenOptions( $this->getOptions() ) ); + $value = strval( $value ); + $valInSelect = in_array( + $value, HTMLFormField::flattenOptions( $this->getOptions() ), true + ); } $selected = $valInSelect ? $value : 'other'; @@ -67,7 +70,7 @@ class HTMLSelectOrOtherField extends HTMLTextField { if ( $request->getCheck( $this->mName ) ) { $val = $request->getText( $this->mName ); - if ( $val == 'other' ) { + if ( $val === 'other' ) { $val = $request->getText( $this->mName . '-other' ); } -- 2.20.1