From: Bartosz DziewoƄski Date: Mon, 7 May 2018 18:57:32 +0000 (+0200) Subject: HTMLTitleTextField: Allow this field to not be required X-Git-Tag: 1.34.0-rc.0~5499^2 X-Git-Url: http://git.cyclocoop.org/data/%7B%24admin_url%7Dconfig?a=commitdiff_plain;h=f88a5a18155a0f9fa64683feabefef83bffa85a3;p=lhc%2Fweb%2Fwiklou.git HTMLTitleTextField: Allow this field to not be required Previously we would always run the validation, and it would fail when the field was empty, since empty string is not a valid title. Respect the 'required' option (defined by HTMLFormField) and make it default to true for compatibility with existing forms that might rely on this. Also add a TODO comment about a confusing special case in validation code. I don't want to dig into that. Change-Id: I93ad51ffe7bee597d2d127f4c5d6b2929ffc8f7e --- diff --git a/includes/htmlform/fields/HTMLTitleTextField.php b/includes/htmlform/fields/HTMLTitleTextField.php index 3eb3f5dfa1..602ddee486 100644 --- a/includes/htmlform/fields/HTMLTitleTextField.php +++ b/includes/htmlform/fields/HTMLTitleTextField.php @@ -25,6 +25,8 @@ class HTMLTitleTextField extends HTMLTextField { 'relative' => false, 'creatable' => false, 'exists' => false, + // This overrides the default from HTMLFormField + 'required' => true, ]; parent::__construct( $params ); @@ -34,8 +36,16 @@ class HTMLTitleTextField extends HTMLTextField { if ( $this->mParent->getMethod() === 'get' && $value === '' ) { // If the form is a GET form and has no value, assume it hasn't been // submitted yet, and skip validation + // TODO This doesn't look right, we should be able to tell the difference + // between "not submitted" (null) and "submitted but empty" (empty string). return parent::validate( $value, $alldata ); } + + if ( !$this->mParams['required'] && $value === '' ) { + // If this field is not required and the value is empty, that's okay, skip validation + return parent::validate( $value, $alldata ); + } + try { if ( !$this->mParams['relative'] ) { $title = Title::newFromTextThrow( $value );