3 use MediaWiki\Widget\TitleInputWidget
;
6 * Implements a text input field for page titles.
7 * Automatically does validation that the title is valid,
8 * as well as autocompletion if using the OOUI display format.
10 * Note: Forms using GET requests will need to make sure the title value is not
13 * Optional parameters:
14 * 'namespace' - Namespace the page must be in
15 * 'relative' - If true and 'namespace' given, strip/add the namespace from/to the title as needed
16 * 'creatable' - Whether to validate the title is creatable (not a special page)
17 * 'exists' - Whether to validate that the title already exists
21 class HTMLTitleTextField
extends HTMLTextField
{
22 public function __construct( $params ) {
28 // This overrides the default from HTMLFormField
32 parent
::__construct( $params );
35 public function validate( $value, $alldata ) {
36 if ( $this->mParent
->getMethod() === 'get' && $value === '' ) {
37 // If the form is a GET form and has no value, assume it hasn't been
38 // submitted yet, and skip validation
39 // TODO This doesn't look right, we should be able to tell the difference
40 // between "not submitted" (null) and "submitted but empty" (empty string).
41 return parent
::validate( $value, $alldata );
44 if ( !$this->mParams
['required'] && $value === '' ) {
45 // If this field is not required and the value is empty, that's okay, skip validation
46 return parent
::validate( $value, $alldata );
50 if ( !$this->mParams
['relative'] ) {
51 $title = Title
::newFromTextThrow( $value );
53 // Can't use Title::makeTitleSafe(), because it doesn't throw useful exceptions
55 $namespaceName = $wgContLang->getNsText( $this->mParams
['namespace'] );
56 $title = Title
::newFromTextThrow( $namespaceName . ':' . $value );
58 } catch ( MalformedTitleException
$e ) {
59 $msg = $this->msg( $e->getErrorMessage() );
60 $params = $e->getErrorMessageParameters();
62 $msg->params( $params );
67 $text = $title->getPrefixedText();
68 if ( $this->mParams
['namespace'] !== false &&
69 !$title->inNamespace( $this->mParams
['namespace'] )
71 return $this->msg( 'htmlform-title-badnamespace', $this->mParams
['namespace'], $text );
74 if ( $this->mParams
['creatable'] && !$title->canExist() ) {
75 return $this->msg( 'htmlform-title-not-creatable', $text );
78 if ( $this->mParams
['exists'] && !$title->exists() ) {
79 return $this->msg( 'htmlform-title-not-exists', $text );
82 return parent
::validate( $value, $alldata );
85 protected function getInputWidget( $params ) {
86 if ( $this->mParams
['namespace'] !== false ) {
87 $params['namespace'] = $this->mParams
['namespace'];
89 $params['relative'] = $this->mParams
['relative'];
90 return new TitleInputWidget( $params );
93 protected function shouldInfuseOOUI() {
97 protected function getOOUIModules() {
98 // FIXME: TitleInputWidget should be in its own module
99 return [ 'mediawiki.widgets' ];
102 public function getInputHtml( $value ) {
103 // add mw-searchInput class to enable search suggestions for non-OOUI, too
104 $this->mClass
.= 'mw-searchInput';
106 // return the HTMLTextField html
107 return parent
::getInputHTML( $value );
110 protected function getDataAttribs() {
112 'data-mw-searchsuggest' => FormatJson
::encode( [
113 'wrapAsLink' => false,