X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fhtmlform%2FHTMLForm.php;h=e627cfdc690303e78c04f7e948fdca4ce22e28a0;hb=ea23bc97ee9144c4575fa0bab81a42faa2b1f29c;hp=3c88594eac7d84e1402f2a88172882ac0fd09df3;hpb=1abe8200881420e6b3d6d2b9fa42afb58038cf7c;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/htmlform/HTMLForm.php b/includes/htmlform/HTMLForm.php index 3c88594eac..e627cfdc69 100644 --- a/includes/htmlform/HTMLForm.php +++ b/includes/htmlform/HTMLForm.php @@ -147,12 +147,16 @@ class HTMLForm extends ContextSource { 'namespaceselect' => 'HTMLSelectNamespace', 'namespaceselectwithbutton' => 'HTMLSelectNamespaceWithButton', 'tagfilter' => 'HTMLTagFilter', + 'sizefilter' => 'HTMLSizeFilterField', 'submit' => 'HTMLSubmitField', 'hidden' => 'HTMLHiddenField', 'edittools' => 'HTMLEditTools', 'checkmatrix' => 'HTMLCheckMatrix', 'cloner' => 'HTMLFormFieldCloner', 'autocompleteselect' => 'HTMLAutoCompleteSelectField', + 'date' => 'HTMLDateTimeField', + 'time' => 'HTMLDateTimeField', + 'datetime' => 'HTMLDateTimeField', // HTMLTextField will output the correct type="" attribute automagically. // There are about four zillion other HTML5 input types, like range, but // we don't use those at the moment, so no point in adding all of them. @@ -173,7 +177,7 @@ class HTMLForm extends ContextSource { protected $mFieldTree; protected $mShowReset = false; protected $mShowSubmit = true; - protected $mSubmitFlags = [ 'constructive', 'primary' ]; + protected $mSubmitFlags = [ 'primary', 'progressive' ]; protected $mShowCancel = false; protected $mCancelTarget; @@ -600,10 +604,14 @@ class HTMLForm extends ContextSource { */ public function trySubmit() { $valid = true; - $hoistedErrors = []; - $hoistedErrors[] = isset( $this->mValidationErrorMessage ) - ? $this->mValidationErrorMessage - : [ 'htmlform-invalid-input' ]; + $hoistedErrors = Status::newGood(); + if ( $this->mValidationErrorMessage ) { + foreach ( (array)$this->mValidationErrorMessage as $error ) { + call_user_func_array( [ $hoistedErrors, 'fatal' ], $error ); + } + } else { + $hoistedErrors->fatal( 'htmlform-invalid-input' ); + } $this->mWasSubmitted = true; @@ -630,15 +638,16 @@ class HTMLForm extends ContextSource { if ( $res !== true ) { $valid = false; if ( $res !== false && !$field->canDisplayErrors() ) { - $hoistedErrors[] = [ 'rawmessage', $res ]; + if ( is_string( $res ) ) { + $hoistedErrors->fatal( 'rawmessage', $res ); + } else { + $hoistedErrors->fatal( $res ); + } } } } if ( !$valid ) { - if ( count( $hoistedErrors ) === 1 ) { - $hoistedErrors = $hoistedErrors[0]; - } return $hoistedErrors; } @@ -1014,7 +1023,8 @@ class HTMLForm extends ContextSource { $this->getOutput()->addModuleStyles( 'mediawiki.htmlform.styles' ); $html = '' - . $this->getErrors( $submitResult ) + . $this->getErrorsOrWarnings( $submitResult, 'error' ) + . $this->getErrorsOrWarnings( $submitResult, 'warning' ) . $this->getHeaderText() . $this->getBody() . $this->getHiddenFields() @@ -1037,6 +1047,7 @@ class HTMLForm extends ContextSource { : 'application/x-www-form-urlencoded'; # Attributes $attribs = [ + 'class' => 'mw-htmlform', 'action' => $this->getAction(), 'method' => $this->getMethod(), 'enctype' => $encType, @@ -1050,6 +1061,9 @@ class HTMLForm extends ContextSource { if ( $this->mName ) { $attribs['name'] = $this->mName; } + if ( $this->needsJSForHtml5FormValidation() ) { + $attribs['novalidate'] = true; + } return $attribs; } @@ -1069,7 +1083,7 @@ class HTMLForm extends ContextSource { return Html::rawElement( 'form', - $this->getFormAttributes() + [ 'class' => 'visualClear' ], + $this->getFormAttributes(), $html ); } @@ -1230,23 +1244,46 @@ class HTMLForm extends ContextSource { * * @param string|array|Status $errors * + * @deprecated since 1.28, use getErrorsOrWarnings() instead + * * @return string */ public function getErrors( $errors ) { - if ( $errors instanceof Status ) { - if ( $errors->isOK() ) { - $errorstr = ''; + wfDeprecated( __METHOD__ ); + return $this->getErrorsOrWarnings( $errors, 'error' ); + } + + /** + * Returns a formatted list of errors or warnings from the given elements. + * + * @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. + * @return string + */ + public function getErrorsOrWarnings( $elements, $elementsType ) { + if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) { + throw new DomainException( $elementsType . ' is not a valid type.' ); + } + $elementstr = false; + if ( $elements instanceof Status ) { + list( $errorStatus, $warningStatus ) = $elements->splitByErrorType(); + $status = $elementsType === 'error' ? $errorStatus : $warningStatus; + if ( $status->isGood() ) { + $elementstr = ''; } else { - $errorstr = $this->getOutput()->parse( $errors->getWikiText() ); + $elementstr = $this->getOutput()->parse( + $status->getWikiText() + ); } - } elseif ( is_array( $errors ) ) { - $errorstr = $this->formatErrors( $errors ); - } else { - $errorstr = $errors; + } elseif ( is_array( $elements ) && $elementsType === 'error' ) { + $elementstr = $this->formatErrors( $elements ); + } elseif ( $elementsType === 'error' ) { + $elementstr = $elements; } - return $errorstr - ? Html::rawElement( 'div', [ 'class' => 'error' ], $errorstr ) + return $elementstr + ? Html::rawElement( 'div', [ 'class' => $elementsType ], $elementstr ) : ''; } @@ -1842,4 +1879,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 `
` 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; + } }