Improve HTMLSubmitField return value
authorGergő Tisza <tgr.huwiki@gmail.com>
Fri, 1 Apr 2016 12:12:06 +0000 (15:12 +0300)
committerGergő Tisza <gtisza@wikimedia.org>
Mon, 18 Apr 2016 14:21:33 +0000 (14:21 +0000)
- 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
includes/htmlform/HTMLFormField.php
includes/htmlform/HTMLFormFieldCloner.php
includes/htmlform/HTMLSubmitField.php

index d50fac0..6878399 100644 (file)
@@ -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 );
                        }
                }
 
index e86d4c4..d5f4cc0 100644 (file)
@@ -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'] );
+       }
 }
index 4f2460f..7359092 100644 (file)
@@ -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();
 
index 7f90100..cb98549 100644 (file)
@@ -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 );
+       }
 }