From 1bd5ee1f0600721922ca43ac9daa10dafc19c772 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gerg=C5=91=20Tisza?= Date: Fri, 1 Apr 2016 15:12:06 +0300 Subject: [PATCH] Improve HTMLSubmitField return value - do not return anything when the button was not clicked - return boolean true (instead of the button text) when it was clicked Unbreaks submit fields which currently don't return anything so there is no easy way to tell whether they have been clicked. Change-Id: If4e0dfb6ee6674f0dace80a01850e2d0cbbdb47a --- includes/htmlform/HTMLForm.php | 7 ++++--- includes/htmlform/HTMLFormField.php | 10 ++++++++++ includes/htmlform/HTMLFormFieldCloner.php | 4 ++-- includes/htmlform/HTMLSubmitField.php | 8 ++++++++ 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/includes/htmlform/HTMLForm.php b/includes/htmlform/HTMLForm.php index d50fac0a69..6878399444 100644 --- a/includes/htmlform/HTMLForm.php +++ b/includes/htmlform/HTMLForm.php @@ -1495,7 +1495,7 @@ class HTMLForm extends ContextSource { foreach ( $fields as $key => $value ) { if ( $value instanceof HTMLFormField ) { - $v = empty( $value->mParams['nodata'] ) + $v = isset( $this->mFieldData[$key] ) ? $this->mFieldData[$key] : $value->getDefault(); @@ -1604,12 +1604,13 @@ class HTMLForm extends ContextSource { $fieldData = []; foreach ( $this->mFlatFields as $fieldname => $field ) { - if ( !empty( $field->mParams['nodata'] ) ) { + $request = $this->getRequest(); + if ( $field->skipLoadData( $request ) ) { continue; } elseif ( !empty( $field->mParams['disabled'] ) ) { $fieldData[$fieldname] = $field->getDefault(); } else { - $fieldData[$fieldname] = $field->loadDataFromRequest( $this->getRequest() ); + $fieldData[$fieldname] = $field->loadDataFromRequest( $request ); } } diff --git a/includes/htmlform/HTMLFormField.php b/includes/htmlform/HTMLFormField.php index e86d4c486d..d5f4cc066f 100644 --- a/includes/htmlform/HTMLFormField.php +++ b/includes/htmlform/HTMLFormField.php @@ -1106,4 +1106,14 @@ abstract class HTMLFormField { return $this->msg( $value, [] ); } } + + /** + * Skip this field when collecting data. + * @param WebRequest $request + * @return bool + * @since 1.27 + */ + public function skipLoadData( $request ) { + return !empty( $this->mParams['nodata'] ); + } } diff --git a/includes/htmlform/HTMLFormFieldCloner.php b/includes/htmlform/HTMLFormFieldCloner.php index 4f2460f905..7359092176 100644 --- a/includes/htmlform/HTMLFormFieldCloner.php +++ b/includes/htmlform/HTMLFormFieldCloner.php @@ -150,7 +150,7 @@ class HTMLFormFieldCloner extends HTMLFormField { $subrequest = new DerivativeRequest( $request, $data, $request->wasPosted() ); $row = []; foreach ( $fields as $fieldname => $field ) { - if ( !empty( $field->mParams['nodata'] ) ) { + if ( $field->skipLoadData( $subrequest ) ) { continue; } elseif ( !empty( $field->mParams['disabled'] ) ) { $row[$fieldname] = $field->getDefault(); @@ -271,7 +271,7 @@ class HTMLFormFieldCloner extends HTMLFormField { $fields = $this->createFieldsForKey( $key ); foreach ( $fields as $fieldname => $field ) { - $v = ( empty( $field->mParams['nodata'] ) && $values !== null ) + $v = isset( $values[$fieldname] ) ? $values[$fieldname] : $field->getDefault(); diff --git a/includes/htmlform/HTMLSubmitField.php b/includes/htmlform/HTMLSubmitField.php index 7f90100944..cb98549efe 100644 --- a/includes/htmlform/HTMLSubmitField.php +++ b/includes/htmlform/HTMLSubmitField.php @@ -8,4 +8,12 @@ class HTMLSubmitField extends HTMLButtonField { protected $buttonType = 'submit'; protected $mFlags = [ 'primary', 'constructive' ]; + + public function skipLoadData( $request ) { + return !$request->getCheck( $this->mName ); + } + + public function loadDataFromRequest( $request ) { + return $request->getCheck( $this->mName ); + } } -- 2.20.1