summaryrefslogtreecommitdiff
path: root/includes/htmlform/HTMLUserTextField.php
blob: 9617c0a35af4048945eb7207ddacb3f041adb82b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php

use MediaWiki\Widget\UserInputWidget;

/**
 * Implements a text input field for user names.
 * Automatically auto-completes if using the OOUI display format.
 *
 * FIXME: Does not work for forms that support GET requests.
 *
 * Optional parameters:
 * 'exists' - Whether to validate that the user already exists
 *
 * @since 1.26
 */
class HTMLUserTextField extends HTMLTextField {
	public function __construct( $params ) {
		$params += array(
			'exists' => false,
			'ipallowed' => false,
		);

		parent::__construct( $params );
	}

	public function validate( $value, $alldata ) {
		// check, if a user exists with the given username
		$user = User::newFromName( $value, false );

		if ( !$user ) {
			return $this->msg( 'htmlform-user-not-valid', $value )->parse();
		} elseif (
			( $this->mParams['exists'] && $user->getId() === 0 ) &&
			!( $this->mParams['ipallowed'] && User::isIP( $value ) )
		) {
			return $this->msg( 'htmlform-user-not-exists', $user->getName() )->parse();
		}

		return parent::validate( $value, $alldata );
	}

	protected function getInputWidget( $params ) {
		$this->mParent->getOutput()->addModules( 'mediawiki.widgets.UserInputWidget' );

		return new UserInputWidget( $params );
	}
}