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 // Default value (from getDefault()) is null, which breaks Title::newFromTextThrow() below
45 if ( $value === null ) {
49 if ( !$this->mParams
['required'] && $value === '' ) {
50 // If this field is not required and the value is empty, that's okay, skip validation
51 return parent
::validate( $value, $alldata );
55 if ( !$this->mParams
['relative'] ) {
56 $title = Title
::newFromTextThrow( $value );
58 // Can't use Title::makeTitleSafe(), because it doesn't throw useful exceptions
59 $title = Title
::newFromTextThrow( Title
::makeName( $this->mParams
['namespace'], $value ) );
61 } catch ( MalformedTitleException
$e ) {
62 return $this->msg( $e->getErrorMessage(), $e->getErrorMessageParameters() );
65 $text = $title->getPrefixedText();
66 if ( $this->mParams
['namespace'] !== false &&
67 !$title->inNamespace( $this->mParams
['namespace'] )
69 return $this->msg( 'htmlform-title-badnamespace', $this->mParams
['namespace'], $text );
72 if ( $this->mParams
['creatable'] && !$title->canExist() ) {
73 return $this->msg( 'htmlform-title-not-creatable', $text );
76 if ( $this->mParams
['exists'] && !$title->exists() ) {
77 return $this->msg( 'htmlform-title-not-exists', $text );
80 return parent
::validate( $value, $alldata );
83 protected function getInputWidget( $params ) {
84 if ( $this->mParams
['namespace'] !== false ) {
85 $params['namespace'] = $this->mParams
['namespace'];
87 $params['relative'] = $this->mParams
['relative'];
88 return new TitleInputWidget( $params );
91 protected function shouldInfuseOOUI() {
95 protected function getOOUIModules() {
96 // FIXME: TitleInputWidget should be in its own module
97 return [ 'mediawiki.widgets' ];
100 public function getInputHtml( $value ) {
101 // add mw-searchInput class to enable search suggestions for non-OOUI, too
102 $this->mClass
.= 'mw-searchInput';
104 // return the HTMLTextField html
105 return parent
::getInputHTML( $value );
108 protected function getDataAttribs() {
110 'data-mw-searchsuggest' => FormatJson
::encode( [
111 'wrapAsLink' => false,