HTMLTitleTextField: Allow this field to not be required
authorBartosz Dziewoński <matma.rex@gmail.com>
Mon, 7 May 2018 18:57:32 +0000 (20:57 +0200)
committerBartosz Dziewoński <matma.rex@gmail.com>
Mon, 7 May 2018 18:57:51 +0000 (20:57 +0200)
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

includes/htmlform/fields/HTMLTitleTextField.php

index 3eb3f5d..602ddee 100644 (file)
@@ -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 );