Merge "Simplify HTMLTitleTextField::validate"
[lhc/web/wiklou.git] / includes / htmlform / fields / HTMLTitleTextField.php
index a15b90e..dd9f793 100644 (file)
@@ -25,6 +25,8 @@ class HTMLTitleTextField extends HTMLTextField {
                        'relative' => false,
                        'creatable' => false,
                        'exists' => false,
+                       // This overrides the default from HTMLFormField
+                       'required' => true,
                ];
 
                parent::__construct( $params );
@@ -34,39 +36,45 @@ class HTMLTitleTextField extends HTMLTextField {
                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
+                       // TODO This doesn't look right, we should be able to tell the difference
+                       // between "not submitted" (null) and "submitted but empty" (empty string).
                        return parent::validate( $value, $alldata );
                }
+
+               // Default value (from getDefault()) is null, which breaks Title::newFromTextThrow() below
+               if ( $value === null ) {
+                       $value = '';
+               }
+
+               if ( !$this->mParams['required'] && $value === '' ) {
+                       // If this field is not required and the value is empty, that's okay, skip validation
+                       return parent::validate( $value, $alldata );
+               }
+
                try {
                        if ( !$this->mParams['relative'] ) {
                                $title = Title::newFromTextThrow( $value );
                        } else {
                                // Can't use Title::makeTitleSafe(), because it doesn't throw useful exceptions
-                               global $wgContLang;
-                               $namespaceName = $wgContLang->getNsText( $this->mParams['namespace'] );
-                               $title = Title::newFromTextThrow( $namespaceName . ':' . $value );
+                               $title = Title::newFromTextThrow( Title::makeName( $this->mParams['namespace'], $value ) );
                        }
                } catch ( MalformedTitleException $e ) {
-                       $msg = $this->msg( $e->getErrorMessage() );
-                       $params = $e->getErrorMessageParameters();
-                       if ( $params ) {
-                               $msg->params( $params );
-                       }
-                       return $msg->parse();
+                       return $this->msg( $e->getErrorMessage(), $e->getErrorMessageParameters() );
                }
 
                $text = $title->getPrefixedText();
                if ( $this->mParams['namespace'] !== false &&
                        !$title->inNamespace( $this->mParams['namespace'] )
                ) {
-                       return $this->msg( 'htmlform-title-badnamespace', $this->mParams['namespace'], $text )->parse();
+                       return $this->msg( 'htmlform-title-badnamespace', $this->mParams['namespace'], $text );
                }
 
                if ( $this->mParams['creatable'] && !$title->canExist() ) {
-                       return $this->msg( 'htmlform-title-not-creatable', $text )->escaped();
+                       return $this->msg( 'htmlform-title-not-creatable', $text );
                }
 
                if ( $this->mParams['exists'] && !$title->exists() ) {
-                       return $this->msg( 'htmlform-title-not-exists', $text )->parse();
+                       return $this->msg( 'htmlform-title-not-exists', $text );
                }
 
                return parent::validate( $value, $alldata );