Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / htmlform / fields / HTMLUserTextField.php
blob12c09c1d7e71d57662443d105a13f52d9e8d7fbb
1 <?php
3 use MediaWiki\Widget\UserInputWidget;
5 /**
6 * Implements a text input field for user names.
7 * Automatically auto-completes if using the OOUI display format.
9 * FIXME: Does not work for forms that support GET requests.
11 * Optional parameters:
12 * 'exists' - Whether to validate that the user already exists
14 * @since 1.26
16 class HTMLUserTextField extends HTMLTextField {
17 public function __construct( $params ) {
18 $params += [
19 'exists' => false,
20 'ipallowed' => false,
23 parent::__construct( $params );
26 public function validate( $value, $alldata ) {
27 // check, if a user exists with the given username
28 $user = User::newFromName( $value, false );
30 if ( !$user ) {
31 return $this->msg( 'htmlform-user-not-valid', $value );
32 } elseif (
33 ( $this->mParams['exists'] && $user->getId() === 0 ) &&
34 !( $this->mParams['ipallowed'] && User::isIP( $value ) )
35 ) {
36 return $this->msg( 'htmlform-user-not-exists', $user->getName() );
39 return parent::validate( $value, $alldata );
42 protected function getInputWidget( $params ) {
43 return new UserInputWidget( $params );
46 protected function shouldInfuseOOUI() {
47 return true;
50 protected function getOOUIModules() {
51 return [ 'mediawiki.widgets.UserInputWidget' ];
54 public function getInputHtml( $value ) {
55 // add the required module and css class for user suggestions in non-OOUI mode
56 $this->mParent->getOutput()->addModules( 'mediawiki.userSuggest' );
57 $this->mClass .= ' mw-autocomplete-user';
59 // return parent html
60 return parent::getInputHTML( $value );