Clean up control and data flow in UserFormField::validate()
authorTim Düsterhus <duesterhus@woltlab.com>
Wed, 25 Aug 2021 11:52:15 +0000 (13:52 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Wed, 25 Aug 2021 11:52:15 +0000 (13:52 +0200)
wcfsetup/install/files/lib/system/form/builder/field/user/UserFormField.class.php

index 8709041bbe267cd41f94bd170144b12bd8919ebf..c5607fb5367355f610c09aeddea3f9ce57faca05 100644 (file)
@@ -144,57 +144,66 @@ class UserFormField extends AbstractFormField implements
         }
 
         if ($this->getValue() !== null) {
+            $usernames = [];
             if ($this->allowsMultiple()) {
-                if ($this->getMinimumMultiples() > 0 && \count($this->getValue()) < $this->getMinimumMultiples()) {
+                $usernames = $this->getValue();
+            } elseif ($this->getValue() !== '') {
+                $usernames = [$this->getValue()];
+            }
+
+            // Validate usernames.
+            $this->users = UserProfile::getUserProfilesByUsername($usernames);
+
+            $nonExistentUsernames = [];
+            foreach ($usernames as $username) {
+                if (!isset($this->users[$username])) {
+                    $nonExistentUsernames[] = $username;
+                }
+            }
+
+            if (!empty($nonExistentUsernames)) {
+                if ($this->allowsMultiple()) {
+                    $this->addValidationError(new FormFieldValidationError(
+                        'nonExistent',
+                        'wcf.form.field.user.error.nonExistent',
+                        ['nonExistentUsernames' => $nonExistentUsernames]
+                    ));
+                } else {
+                    $this->addValidationError(new FormFieldValidationError(
+                        'nonExistent',
+                        'wcf.form.field.user.error.nonExistent'
+                    ));
+                }
+            }
+
+            // Validate the number of multiples.
+            if ($this->allowsMultiple()) {
+                if (
+                    $this->getMinimumMultiples() > 0
+                    && \count($usernames) < $this->getMinimumMultiples()
+                ) {
                     $this->addValidationError(new FormFieldValidationError(
                         'minimumMultiples',
                         'wcf.form.field.user.error.minimumMultiples',
                         [
                             'minimumCount' => $this->getMinimumMultiples(),
-                            'count' => \count($this->getValue()),
+                            'count' => \count($usernames),
                         ]
                     ));
-                } elseif (
+                }
+
+                if (
                     $this->getMaximumMultiples() !== IMultipleFormField::NO_MAXIMUM_MULTIPLES
-                    && \count($this->getValue()) > $this->getMaximumMultiples()
+                    && \count($usernames) > $this->getMaximumMultiples()
                 ) {
                     $this->addValidationError(new FormFieldValidationError(
                         'maximumMultiples',
                         'wcf.form.field.user.error.maximumMultiples',
                         [
                             'maximumCount' => $this->getMaximumMultiples(),
-                            'count' => \count($this->getValue()),
+                            'count' => \count($usernames),
                         ]
                     ));
-                } else {
-                    // validate users
-                    $this->users = UserProfile::getUserProfilesByUsername($this->getValue());
-
-                    $nonExistentUsernames = [];
-                    foreach ($this->getValue() as $username) {
-                        if (!isset($this->users[$username])) {
-                            $nonExistentUsernames[] = $username;
-                        }
-                    }
-
-                    if (!empty($nonExistentUsernames)) {
-                        $this->addValidationError(new FormFieldValidationError(
-                            'nonExistent',
-                            'wcf.form.field.user.error.nonExistent',
-                            ['nonExistentUsernames' => $nonExistentUsernames]
-                        ));
-                    }
-                }
-            } elseif ($this->getValue() !== '') {
-                $user = UserProfile::getUserProfileByUsername($this->getValue());
-
-                if ($user === null) {
-                    $this->addValidationError(new FormFieldValidationError(
-                        'nonExistent',
-                        'wcf.form.field.user.error.nonExistent'
-                    ));
-                } else {
-                    $this->users[] = $user;
                 }
             }
         }