From f301f1582766caf82f4ee0cf5eb24658eb9d0360 Mon Sep 17 00:00:00 2001 From: Happy-melon Date: Wed, 15 Dec 2010 21:14:36 +0000 Subject: [PATCH] Clean up the running mess that is r64866, r65040, and then r67277. Implement and document consistent behaviour for all types of fields: if the 'name' parameter is specified, use it exactly as is, otherwise use "wp{$fieldname}". For hidden fields added with addHiddenField(), continue to use either $attrib['name'] or $name, both unaltered. --- includes/HTMLForm.php | 37 ++++++++++++------------ includes/Preferences.php | 2 +- includes/specials/SpecialEmailuser.php | 1 - includes/specials/SpecialUpload.php | 4 +-- includes/specials/SpecialUploadStash.php | 8 ++++- 5 files changed, 28 insertions(+), 24 deletions(-) diff --git a/includes/HTMLForm.php b/includes/HTMLForm.php index d1a5438ff5..30e62ad27c 100644 --- a/includes/HTMLForm.php +++ b/includes/HTMLForm.php @@ -42,6 +42,10 @@ * 'validation-callback' -- a function name to give you the chance * to impose extra validation on the field input. * @see HTMLForm::validate() + * 'name' -- By default, the 'name' attribute of the input field + * is "wp{$fieldname}". If you want a different name + * (eg one without the "wp" prefix), specify it here and + * it will be used without modification. * * TODO: Document 'section' / 'subsection' stuff */ @@ -117,15 +121,11 @@ class HTMLForm { ? $info['section'] : ''; - $info['name'] = isset( $info['name'] ) - ? $info['name'] - : $fieldname; - if ( isset( $info['type'] ) && $info['type'] == 'file' ) { $this->mUseMultipart = true; } - $field = self::loadInputFromParameters( $info ); + $field = self::loadInputFromParameters( $fieldname, $info ); $field->mParent = $this; $setSection =& $loadedDescriptor; @@ -167,7 +167,7 @@ class HTMLForm { * @param $descriptor input Descriptor, as described above * @return HTMLFormField subclass */ - static function loadInputFromParameters( $descriptor ) { + static function loadInputFromParameters( $fieldname, $descriptor ) { if ( isset( $descriptor['class'] ) ) { $class = $descriptor['class']; } elseif ( isset( $descriptor['type'] ) ) { @@ -178,6 +178,8 @@ class HTMLForm { if ( !$class ) { throw new MWException( "Descriptor with no class: " . print_r( $descriptor, true ) ); } + + $descriptor['fieldname'] = $fieldname; $obj = new $class( $descriptor ); @@ -319,7 +321,7 @@ class HTMLForm { /** * Add a hidden field to the output - * @param $name String field name + * @param $name String field name. This will be used exactly as entered * @param $value String field value * @param $attribs Array */ @@ -794,17 +796,17 @@ abstract class HTMLFormField { $this->mLabel = $params['label']; } + $this->mName = "wp{$params['fieldname']}"; if ( isset( $params['name'] ) ) { - $name = $params['name']; - $validName = Sanitizer::escapeId( $name ); - - if ( $name != $validName ) { - throw new MWException( "Invalid name '$name' passed to " . __METHOD__ ); - } - - $this->mName = 'wp' . $name; - $this->mID = 'mw-input-' . $name; + $this->mName = $params['name']; + } + + $validName = Sanitizer::escapeId( $this->mName ); + if ( $this->mName != $validName && !isset( $params['nodata'] ) ) { + throw new MWException( "Invalid name '{$this->mName}' passed to " . __METHOD__ ); } + + $this->mID = "mw-input-{$this->mName}"; if ( isset( $params['default'] ) ) { $this->mDefault = $params['default']; @@ -1498,9 +1500,6 @@ class HTMLInfoField extends HTMLFormField { class HTMLHiddenField extends HTMLFormField { public function __construct( $params ) { parent::__construct( $params ); - # forcing the 'wp' prefix on hidden field names - # is undesirable - $this->mName = substr( $this->mName, 2 ); # Per HTML5 spec, hidden fields cannot be 'required' # http://dev.w3.org/html5/spec/states-of-the-type-attribute.html#hidden-state diff --git a/includes/Preferences.php b/includes/Preferences.php index 3d68fa90e1..0916040015 100644 --- a/includes/Preferences.php +++ b/includes/Preferences.php @@ -67,7 +67,7 @@ class Preferences { ## Prod in defaults from the user foreach ( $defaultPreferences as $name => &$info ) { $prefFromUser = self::getOptionFromUser( $name, $info, $user ); - $field = HTMLForm::loadInputFromParameters( $info ); // For validation + $field = HTMLForm::loadInputFromParameters( $name, $info ); // For validation $defaultOptions = User::getDefaultOptions(); $globalDefault = isset( $defaultOptions[$name] ) ? $defaultOptions[$name] diff --git a/includes/specials/SpecialEmailuser.php b/includes/specials/SpecialEmailuser.php index c51204491a..7c3d5c1e38 100644 --- a/includes/specials/SpecialEmailuser.php +++ b/includes/specials/SpecialEmailuser.php @@ -57,7 +57,6 @@ class SpecialEmailUser extends UnlistedSpecialPage { 'id' => 'mw-emailuser-recipient', ), 'Target' => array( - 'name' => 'wpTarget', 'type' => 'hidden', 'default' => $this->mTargetObj->getName(), ), diff --git a/includes/specials/SpecialUpload.php b/includes/specials/SpecialUpload.php index 48e700c588..4c7444926c 100644 --- a/includes/specials/SpecialUpload.php +++ b/includes/specials/SpecialUpload.php @@ -1033,14 +1033,14 @@ class UploadForm extends HTMLForm { ); } - $descriptor['wpDestFileWarningAck'] = array( + $descriptor['DestFileWarningAck'] = array( 'type' => 'hidden', 'id' => 'wpDestFileWarningAck', 'default' => $this->mDestWarningAck ? '1' : '', ); if ( $this->mForReUpload ) { - $descriptor['wpForReUpload'] = array( + $descriptor['ForReUpload'] = array( 'type' => 'hidden', 'id' => 'wpForReUpload', 'default' => '1', diff --git a/includes/specials/SpecialUploadStash.php b/includes/specials/SpecialUploadStash.php index 0bf72b9b86..6386240197 100644 --- a/includes/specials/SpecialUploadStash.php +++ b/includes/specials/SpecialUploadStash.php @@ -329,7 +329,13 @@ class SpecialUploadStash extends UnlistedSpecialPage { // create the form, which will also be used to execute a callback to process incoming form data // this design is extremely dubious, but supposedly HTMLForm is our standard now? - $form = new HTMLForm( array( 'clear' => array( 'class' => 'HTMLHiddenField', 'default' => true ) ), 'clearStashedUploads' ); + $form = new HTMLForm( array( + 'Clear' => array( + 'type' => 'hidden', + 'default' => true, + 'name' => 'clear', + ) + ), 'clearStashedUploads' ); $form->setSubmitCallback( array( __CLASS__, 'tryClearStashedUploads' ) ); $form->setTitle( $this->getTitle() ); $form->addHiddenField( 'clear', true, array( 'type' => 'boolean' ) ); -- 2.20.1