* Automatically does validation that the title is valid,
* as well as autocompletion if using the OOUI display format.
*
- * 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
* 'relative' - If true and 'namespace' given, strip/add the namespace from/to the title as needed
'relative' => false,
'creatable' => false,
'exists' => false,
+ // This overrides the default from HTMLFormField
+ 'required' => true,
];
parent::__construct( $params );
}
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
+ // 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;
+ 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 );
+ return $this->msg( 'htmlform-title-badnamespace', $text, $this->mParams['namespace'] );
}
if ( $this->mParams['creatable'] && !$title->canExist() ) {