From f4dd596f5cbae8679599d1283e540e92dfe600cd Mon Sep 17 00:00:00 2001 From: Happy-melon Date: Tue, 22 Sep 2009 20:05:28 +0000 Subject: [PATCH] Allow extra buttons and hidden fields to be included in HTMLForm. Accessible either before Form construction (as HTMLFormField subclasses that can be passed in the Descriptor array as for other input) or after (as methods addHiddenField() and addButton(), respectively). --- includes/HTMLForm.php | 63 +++++++++++++++++++++- includes/specials/SpecialCreateAccount.php | 31 +++++------ includes/specials/SpecialUserlogin.php | 30 +++++------ 3 files changed, 88 insertions(+), 36 deletions(-) diff --git a/includes/HTMLForm.php b/includes/HTMLForm.php index 1764f6e88f..25067edd25 100644 --- a/includes/HTMLForm.php +++ b/includes/HTMLForm.php @@ -60,6 +60,9 @@ class HTMLForm { 'float' => 'HTMLFloatField', 'info' => 'HTMLInfoField', 'selectorother' => 'HTMLSelectOrOtherField', + 'submit' => 'HTMLSubmitField', + 'hidden' => 'HTMLHiddenField', + # HTMLTextField will output the correct type="" attribute automagically. # There are about four zillion other HTML 5 input types, like url, but # we don't use those at the moment, so no point in adding all of them. @@ -78,6 +81,9 @@ class HTMLForm { protected $mSubmitID; protected $mSubmitText; protected $mTitle; + + protected $mHiddenFields = array(); + protected $mButtons = array(); /** * Build a new HTMLForm from an array of field attributes @@ -249,6 +255,19 @@ class HTMLForm { function setIntro( $msg ) { $this->mIntro = $msg; } + + /** + * Add a hidden field to the output + * @param $name String field name + * @param $value String field value + */ + public function addHiddenField( $name, $value ){ + $this->mHiddenFields[ $name ] = $value; + } + + public function addButton( $name, $value, $id=null ){ + $this->mButtons[] = compact( 'name', 'value', 'id' ); + } /** * Display the form (sending to wgOut), with an appropriate error @@ -305,6 +324,10 @@ class HTMLForm { $html .= Html::hidden( 'wpEditToken', $wgUser->editToken() ) . "\n"; $html .= Html::hidden( 'title', $this->getTitle() ) . "\n"; + + foreach( $this->mHiddenFields as $name => $value ){ + $html .= Html::hidden( $name, $value ) . "\n"; + } return $html; } @@ -335,6 +358,17 @@ class HTMLForm { ) . "\n"; } + foreach( $this->mButtons as $button ){ + $attrs = array( + 'type' => 'submit', + 'name' => $button['name'], + 'value' => $button['value'] + ); + if( isset( $button['id'] ) ) + $attrs['id'] = $button['id']; + $html .= Html::element( 'input', $attrs ); + } + return $html; } @@ -554,7 +588,7 @@ abstract class HTMLFormField { * that the user-defined callback mValidationCallback is still run * @param $value String the value the field was submitted with * @param $alldata $all the data collected from the form - * @return Bool is the input valid + * @return Mixed Bool true on success, or String error to display. */ function validate( $value, $alldata ) { if ( isset( $this->mValidationCallback ) ) { @@ -1188,3 +1222,30 @@ class HTMLInfoField extends HTMLFormField { return false; } } + +class HTMLHiddenField extends HTMLFormField { + + public function getTableRow( $value ){ + $this->mParent->addHiddenField( + $this->mParams['name'], + $this->mParams['default'] + ); + return ''; + } + + public function getInputHTML( $value ){ return ''; } +} + +class HTMLSubmitField extends HTMLFormField { + + public function getTableRow( $value ){ + $this->mParent->addButton( + $this->mParams['name'], + $this->mParams['default'], + isset($this->mParams['id']) ? $this->mParams['id'] : null + ); + } + + public function getInputHTML( $value ){ return ''; } +} + diff --git a/includes/specials/SpecialCreateAccount.php b/includes/specials/SpecialCreateAccount.php index 6e05f5f362..2d59f2bf0e 100644 --- a/includes/specials/SpecialCreateAccount.php +++ b/includes/specials/SpecialCreateAccount.php @@ -357,21 +357,7 @@ class SpecialCreateAccount extends SpecialPage { array( 'id' => 'languagelinks' ), SpecialUserLogin::makeLanguageSelector( $this->getTitle(), $this->mReturnTo ) ) : ''; - - # Add a 'send password by email' button if available - $buttons = ''; - if( $wgEnableEmail && $wgUser->isLoggedIn() ){ - $buttons = Html::element( - 'input', - array( - 'type' => 'submit', - 'name' => 'wpCreateaccountMail', - 'value' => wfMsg( 'createaccountmail' ), - 'id' => 'wpCreateaccountMail', - ) - ); - } - + # Give authentication and captcha plugins a chance to # modify the form, by hook or by using $wgAuth $wgAuth->modifyUITemplate( $this, 'new' ); @@ -419,6 +405,17 @@ class SpecialCreateAccount extends SpecialPage { $form->setSubmitId( 'wpCreateaccount' ); $form->suppressReset(); $form->loadData(); + $form->addHiddenField( 'returnto', $this->mReturnTo ); + $form->addHiddenField( 'returntoquery', $this->mReturnToQuery ); + + # Add a 'send password by email' button if available + if( $wgEnableEmail && $wgUser->isLoggedIn() ){ + $form->addButton( + 'wpCreateaccountMail', + wfMsg( 'createaccountmail' ), + 'wpCreateaccountMail' + ); + } $formContents = '' . Html::rawElement( 'p', array( 'id' => 'userloginlink' ), @@ -426,10 +423,8 @@ class SpecialCreateAccount extends SpecialPage { . $this->mFormHeader . $langSelector . $form->getBody() + . $form->getHiddenFields() . $form->getButtons() - . $buttons - . Xml::hidden( 'returnto', $this->mReturnTo ) - . Xml::hidden( 'returntoquery', $this->mReturnToQuery ) ; $wgOut->setPageTitle( wfMsg( 'createaccount' ) ); diff --git a/includes/specials/SpecialUserlogin.php b/includes/specials/SpecialUserlogin.php index a9b57d9205..6be40387c1 100644 --- a/includes/specials/SpecialUserlogin.php +++ b/includes/specials/SpecialUserlogin.php @@ -192,20 +192,6 @@ class SpecialUserLogin extends SpecialPage { array( 'id' => 'languagelinks' ), self::makeLanguageSelector( $this->getTitle(), $this->mReturnTo ) ) : ''; - - # Add a 'mail reset' button if available - $buttons = ''; - if( $wgEnableEmail && $wgAuth->allowPasswordChange() ){ - $buttons = Html::element( - 'input', - array( - 'type' => 'submit', - 'name' => 'wpMailmypassword', - 'value' => wfMsg( 'mailmypassword' ), - 'id' => 'wpMailmypassword', - ) - ); - } # Give authentication and captcha plugins a chance to # modify the form, by hook or by using $wgAuth @@ -237,6 +223,18 @@ class SpecialUserLogin extends SpecialPage { $form->setSubmitId( 'wpLoginAttempt' ); $form->suppressReset(); $form->loadData(); + $form->addHiddenField( 'returnto', $this->mReturnTo ); + $form->addHiddenField( 'returntoquery', $this->mReturnToQuery ); + + # Add a 'mail reset' button if available + $buttons = ''; + if( $wgEnableEmail && $wgAuth->allowPasswordChange() ){ + $form->addButton( + 'wpMailmypassword', + wfMsg( 'mailmypassword' ), + 'wpMailmypassword' + ); + } $formContents = '' . Html::rawElement( 'p', array( 'id' => 'userloginlink' ), @@ -246,10 +244,8 @@ class SpecialUserLogin extends SpecialPage { . $this->mFormHeader . $langSelector . $form->getBody() + . $form->getHiddenFields() . $form->getButtons() - . $buttons - . Xml::hidden( 'returnto', $this->mReturnTo ) - . Xml::hidden( 'returntoquery', $this->mReturnToQuery ) ; $wgOut->setPageTitle( wfMsg( 'login' ) ); -- 2.20.1