protected function validateRegistration(array $reg)
    {
        $isValid = true;

        // Username format
        try {
            $this->storage->checkValidName($reg["username"]);
        } catch (\RuntimeException $e) {
            $isValid = false;
            $this->session->addFlash("error", $e->getMessage());
        }

        // Username length
        $min = $this->config["nameLenMin"];
        $max = $this->config["nameLenMax"];
        if (strlen($reg["username"]) < $min || strlen($reg["username"]) > $max) {
            $isValid = false;
            $this->session->addFlash(
                "error",
                sprintf("Length of a username must be between %d-%d characters.", $min, $max)
            );
        }

        // Email format
        if (!filter_var($reg["email"], FILTER_VALIDATE_EMAIL)) {
            $isValid = false;
            $this->session->addFlash("error", "Email address does not have a valid format.");
        }

        // Email unique
        if (null !== $this->storage->getUserByEmail($reg["email"])) {
            $isValid = false;
            $this->session->addFlash("error", "This email is already in use.");
        }

        // Password repeat matches
        if ($reg["password"]->get() !== $reg["passwordRepeat"]->get()) {
            $isValid = false;
            $this->session->addFlash("error", "The passwords do not match.");
        }

        // Check password policy
        $localAuth = $this->picoAuth->getContainer()->get('LocalAuth');
        if (!$localAuth->checkPasswordPolicy($reg["password"])) {
            $isValid = false;
        }

        // Username unique
        if ($this->storage->getUserByName($reg["username"]) !== null) {
            $isValid = false;
            $this->session->addFlash("error", "The username is already taken.");
        }

        return $isValid;
    }