HTMLForm: Show more options when incorrect displayFormat is set
[lhc/web/wiklou.git] / includes / htmlform / HTMLForm.php
index 71ccaa3..d4351e0 100644 (file)
@@ -165,6 +165,7 @@ class HTMLForm extends ContextSource {
                'url' => 'HTMLTextField',
                'title' => 'HTMLTitleTextField',
                'user' => 'HTMLUserTextField',
+               'usersmultiselect' => 'HTMLUsersMultiselectField',
        ];
 
        public $mFieldData;
@@ -284,7 +285,7 @@ class HTMLForm extends ContextSource {
                                return ObjectFactory::constructClassInstance( OOUIHTMLForm::class, $arguments );
                        default:
                                /** @var HTMLForm $form */
-                               $form = ObjectFactory::constructClassInstance( HTMLForm::class, $arguments );
+                               $form = ObjectFactory::constructClassInstance( self::class, $arguments );
                                $form->setDisplayFormat( $displayFormat );
                                return $form;
                }
@@ -399,7 +400,13 @@ class HTMLForm extends ContextSource {
 
                if ( !in_array( $format, $this->availableDisplayFormats, true ) ) {
                        throw new MWException( 'Display format must be one of ' .
-                               print_r( $this->availableDisplayFormats, true ) );
+                               print_r(
+                                       array_merge(
+                                               $this->availableDisplayFormats,
+                                               $this->availableSubclassDisplayFormats
+                                       ),
+                                       true
+                               ) );
                }
 
                // Evil hack for mobile :(
@@ -1047,6 +1054,7 @@ class HTMLForm extends ContextSource {
                        : 'application/x-www-form-urlencoded';
                # Attributes
                $attribs = [
+                       'class' => 'mw-htmlform',
                        'action' => $this->getAction(),
                        'method' => $this->getMethod(),
                        'enctype' => $encType,
@@ -1060,6 +1068,9 @@ class HTMLForm extends ContextSource {
                if ( $this->mName ) {
                        $attribs['name'] = $this->mName;
                }
+               if ( $this->needsJSForHtml5FormValidation() ) {
+                       $attribs['novalidate'] = true;
+               }
                return $attribs;
        }
 
@@ -1079,7 +1090,7 @@ class HTMLForm extends ContextSource {
 
                return Html::rawElement(
                        'form',
-                       $this->getFormAttributes() + [ 'class' => 'visualClear' ],
+                       $this->getFormAttributes(),
                        $html
                );
        }
@@ -1254,7 +1265,7 @@ class HTMLForm extends ContextSource {
         *
         * @param string|array|Status $elements The set of errors/warnings to process.
         * @param string $elementsType Should warnings or errors be returned.  This is meant
-        *      for Status objects, all other valid types are always considered as errors.
+        *     for Status objects, all other valid types are always considered as errors.
         * @return string
         */
        public function getErrorsOrWarnings( $elements, $elementsType ) {
@@ -1875,4 +1886,22 @@ class HTMLForm extends ContextSource {
        protected function getMessage( $value ) {
                return Message::newFromSpecifier( $value )->setContext( $this );
        }
+
+       /**
+        * Whether this form, with its current fields, requires the user agent to have JavaScript enabled
+        * for the client-side HTML5 form validation to work correctly. If this function returns true, a
+        * 'novalidate' attribute will be added on the `<form>` element. It will be removed if the user
+        * agent has JavaScript support, in htmlform.js.
+        *
+        * @return boolean
+        * @since 1.29
+        */
+       public function needsJSForHtml5FormValidation() {
+               foreach ( $this->mFlatFields as $fieldname => $field ) {
+                       if ( $field->needsJSForHtml5FormValidation() ) {
+                               return true;
+                       }
+               }
+               return false;
+       }
 }