Anomie has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/165762

Change subject: HTMLForm: Improve hidden field handling
......................................................................

HTMLForm: Improve hidden field handling

HTMLHiddenField is unable to output a non-default value, which makes it
unsuitable for actually persisting changes across multiple form
submissions. To preserve compatibility with forms that aren't expecting
persistence, fixing this requires a new parameter in the field
definition.

Also, due to the unusual way that hidden fields are added to the HTML
output, they are not being correctly handled by HTMLFormFieldCloner.
Special-case that.

Change-Id: I1fde7372401299b4432d28ac61982d47d5f3bbea
---
M includes/htmlform/HTMLFormFieldCloner.php
M includes/htmlform/HTMLHiddenField.php
2 files changed, 30 insertions(+), 6 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/62/165762/1

diff --git a/includes/htmlform/HTMLFormFieldCloner.php 
b/includes/htmlform/HTMLFormFieldCloner.php
index 029911c..5dadaf8 100644
--- a/includes/htmlform/HTMLFormFieldCloner.php
+++ b/includes/htmlform/HTMLFormFieldCloner.php
@@ -276,6 +276,7 @@
                }
 
                $html = '';
+               $hidden = '';
                $hasLabel = false;
 
                $fields = $this->createFieldsForKey( $key );
@@ -283,11 +284,18 @@
                        $v = ( empty( $field->mParams['nodata'] ) && $values 
!== null )
                                ? $values[$fieldname]
                                : $field->getDefault();
-                       $html .= $field->$getFieldHtmlMethod( $v );
 
-                       $labelValue = trim( $field->getLabel() );
-                       if ( $labelValue != ' ' && $labelValue !== '' ) {
-                               $hasLabel = true;
+                       if ( $field instanceof HTMLHiddenField ) {
+                               // HTMLHiddenField doesn't generate its own HTML
+                               list( $name, $value, $params ) = 
$field->getHiddenFieldData( $v );
+                               $hidden .= Html::hidden( $name, $value, $params 
) . "\n";
+                       } else {
+                               $html .= $field->$getFieldHtmlMethod( $v );
+
+                               $labelValue = trim( $field->getLabel() );
+                               if ( $labelValue != ' ' && $labelValue !== 
'' ) {
+                                       $hasLabel = true;
+                               }
                        }
                }
 
@@ -335,6 +343,8 @@
                        }
                }
 
+               $html .= $hidden;
+
                if ( !empty( $this->mParams['row-legend'] ) ) {
                        $legend = $this->msg( $this->mParams['row-legend'] 
)->text();
                        $html = Xml::fieldset( $legend, $html );
diff --git a/includes/htmlform/HTMLHiddenField.php 
b/includes/htmlform/HTMLHiddenField.php
index e32c0bb..ffde915 100644
--- a/includes/htmlform/HTMLHiddenField.php
+++ b/includes/htmlform/HTMLHiddenField.php
@@ -1,22 +1,36 @@
 <?php
 
 class HTMLHiddenField extends HTMLFormField {
+       protected $outputAsDefault = true;
+
        public function __construct( $params ) {
                parent::__construct( $params );
+
+               if ( isset( $this->mParams['output-as-default'] ) ) {
+                       $this->outputAsDefault = 
(bool)$this->mParams['output-as-default'];
+               }
 
                # Per HTML5 spec, hidden fields cannot be 'required'
                # 
http://www.w3.org/TR/html5/forms.html#hidden-state-%28type=hidden%29
                unset( $this->mParams['required'] );
        }
 
-       public function getTableRow( $value ) {
+       public function getHiddenFieldData( $value ) {
                $params = array();
                if ( $this->mID ) {
                        $params['id'] = $this->mID;
                }
 
-               $this->mParent->addHiddenField( $this->mName, $this->mDefault, 
$params );
+               if ( $this->outputAsDefault ) {
+                       $value = $this->mDefault;
+               }
 
+               return array( $this->mName, $value, $params );
+       }
+
+       public function getTableRow( $value ) {
+               list( $name, $value, $params ) = $this->getHiddenFieldData( 
$value );
+               $this->mParent->addHiddenField( $name, $value, $params );
                return '';
        }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/165762
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1fde7372401299b4432d28ac61982d47d5f3bbea
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Anomie <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to