Allow HTMLTitleTextField to work on GET forms
authorKunal Mehta <legoktm@gmail.com>
Sat, 25 Jul 2015 01:18:49 +0000 (18:18 -0700)
committerKunal Mehta <legoktm@gmail.com>
Sat, 25 Jul 2015 01:19:36 +0000 (18:19 -0700)
Just skip validation if it is a GET form and the current input is an
empty string. Callers will need to check that it is not the empty string
though.

Also make sure HTMLForm::mMethod is always lowercase.

Change-Id: I605f32048fe97eebd7e04b6ffd799759aeb7f31e

includes/htmlform/HTMLForm.php
includes/htmlform/HTMLTitleTextField.php

index 48cc828..a56b398 100644 (file)
@@ -1302,11 +1302,14 @@ class HTMLForm extends ContextSource {
         * @return HTMLForm $this for chaining calls (since 1.20)
         */
        public function setMethod( $method = 'post' ) {
-               $this->mMethod = $method;
+               $this->mMethod = strtolower( $method );
 
                return $this;
        }
 
+       /**
+        * @return string Always lowercase
+        */
        public function getMethod() {
                return $this->mMethod;
        }
index e1bc1a0..2124bb1 100644 (file)
@@ -7,7 +7,8 @@ use MediaWiki\Widget\TitleInputWidget;
  * Automatically does validation that the title is valid,
  * as well as autocompletion if using the OOUI display format.
  *
- * FIXME: Does not work for forms that support GET requests.
+ * Note: Forms using GET requests will need to make sure the title value is not
+ * an empty string.
  *
  * Optional parameters:
  * 'namespace' - Namespace the page must be in
@@ -28,6 +29,11 @@ class HTMLTitleTextField extends HTMLTextField {
        }
 
        public function validate( $value, $alldata ) {
+               if ( $this->mParent->getMethod() === 'get' && $value === '' ) {
+                       // If the form is a GET form and has no value, assume it hasn't been
+                       // submitted yet, and skip validation
+                       return parent::validate( $value, $alldata );
+               }
                try {
                        $title = Title::newFromTextThrow( $value );
                } catch ( MalformedTitleException $e ) {